Albirew/nyaa-pantsu
Albirew
/
nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/main.go

253 lignes
6.3 KiB
Go
Brut Vue normale Historique

2017-05-02 12:39:53 +02:00
package main
import (
"encoding/json"
2017-05-04 21:48:40 +02:00
"github.com/gorilla/feeds"
2017-05-02 12:39:53 +02:00
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
2017-05-02 12:39:53 +02:00
"html"
"html/template"
"log"
"net/http"
"strconv"
2017-05-03 06:59:27 +02:00
"strings"
2017-05-02 12:39:53 +02:00
"time"
)
var db *gorm.DB
2017-05-04 01:15:20 +02:00
var templates = template.Must(template.ParseFiles("index.html", "FAQ.html"))
2017-05-02 12:39:53 +02:00
var debugLogger *log.Logger
2017-05-03 09:33:39 +02:00
var trackers = "&tr=udp://zer0day.to:1337/announce&tr=udp://tracker.leechers-paradise.org:6969&tr=udp://explodie.org:6969&tr=udp://tracker.opentrackr.org:1337&tr=udp://tracker.coppersurfer.tk:6969"
2017-05-02 12:39:53 +02:00
func getDBHandle() *gorm.DB {
dbInit, err := gorm.Open("sqlite3", "./nyaa.db")
2017-05-02 12:39:53 +02:00
// Migrate the schema of Torrents
dbInit.AutoMigrate(&Torrents{}, &Categories{}, &Sub_Categories{}, &Statuses{})
2017-05-02 12:39:53 +02:00
checkErr(err)
return dbInit
2017-05-02 12:39:53 +02:00
}
func checkErr(err error) {
if err != nil {
debugLogger.Println(" " + err.Error())
}
}
func apiHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
page := vars["page"]
pagenum, _ := strconv.Atoi(html.EscapeString(page))
b := CategoryJson{Torrents: []TorrentsJson{}}
maxPerPage := 50
nbTorrents := 0
torrents := getAllTorrents(maxPerPage, maxPerPage*(pagenum-1))
for i, _ := range torrents {
nbTorrents++
res := torrents[i].toJson()
2017-05-02 12:39:53 +02:00
b.Torrents = append(b.Torrents, res)
2017-05-02 12:39:53 +02:00
}
b.QueryRecordCount = maxPerPage
b.TotalRecordCount = nbTorrents
2017-05-02 12:39:53 +02:00
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(b)
2017-05-02 12:39:53 +02:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
2017-05-02 12:39:53 +02:00
func singleapiHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
b := CategoryJson{Torrents: []TorrentsJson{}}
torrent, err := getTorrentById(id)
res := torrent.toJson()
b.Torrents = append(b.Torrents, res)
2017-05-02 12:39:53 +02:00
b.QueryRecordCount = 1
b.TotalRecordCount = 1
2017-05-02 12:39:53 +02:00
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(b)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func searchHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
page := vars["page"]
// db params url
2017-05-04 00:20:50 +02:00
maxPerPage, errConv := strconv.Atoi(r.URL.Query().Get("max"))
if errConv != nil {
maxPerPage = 50 // default Value maxPerPage
2017-05-04 00:20:50 +02:00
}
2017-05-02 12:39:53 +02:00
pagenum, _ := strconv.Atoi(html.EscapeString(page))
searchQuery := r.URL.Query().Get("q")
2017-05-03 06:59:27 +02:00
cat := r.URL.Query().Get("c")
2017-05-04 14:01:07 +02:00
stat := r.URL.Query().Get("s")
2017-05-04 08:53:21 +02:00
catsSplit := strings.Split(cat, "_")
// need this to prevent out of index panics
var searchCatId, searchSubCatId string
if len(catsSplit) == 2 {
2017-05-04 08:53:21 +02:00
searchCatId = html.EscapeString(catsSplit[0])
searchSubCatId = html.EscapeString(catsSplit[1])
}
nbTorrents := 0
b := []TorrentsJson{}
2017-05-04 00:54:07 +02:00
2017-05-04 14:01:07 +02:00
torrents := getTorrents(createWhereParams("torrent_name LIKE ? AND status_id LIKE ? AND category_id LIKE ? AND sub_category_id LIKE ?",
"%"+searchQuery+"%", stat+"%", searchCatId+"%", searchSubCatId+"%"), maxPerPage, maxPerPage*(pagenum-1))
for i, _ := range torrents {
nbTorrents++
res := torrents[i].toJson()
b = append(b, res)
2017-05-02 12:39:53 +02:00
}
2017-05-04 14:01:07 +02:00
htv := HomeTemplateVariables{b, getAllCategories(false), searchQuery, stat, cat, maxPerPage, nbTorrents}
err := templates.ExecuteTemplate(w, "index.html", htv)
2017-05-02 12:39:53 +02:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
2017-05-03 09:33:39 +02:00
func safe(s string) template.URL {
return template.URL(s)
}
2017-05-02 12:39:53 +02:00
2017-05-04 01:15:20 +02:00
func faqHandler(w http.ResponseWriter, r *http.Request) {
2017-05-04 04:29:22 +02:00
err := templates.ExecuteTemplate(w, "FAQ.html", "")
2017-05-04 01:15:20 +02:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
2017-05-04 21:48:40 +02:00
func rssHandler(w http.ResponseWriter, r *http.Request) {
//vars := mux.Vars(r)
//category := vars["c"]
// db params url
//maxPerPage := 50 // default Value maxPerPage
torrents := getFeeds()
created := time.Now().String()
if ( len(torrents) > 0 ) {
created = torrents[0].Timestamp
}
created_as_time, err := time.Parse("2006-01-02 15:04:05", created)
if err == nil {
;
}
feed := &feeds.Feed{
Title: "Nyaa Pantsu",
Link: &feeds.Link{Href: "https://nyaa.pantsu.cat/"},
Created: created_as_time,
}
feed.Items = []*feeds.Item{}
feed.Items = make( []*feeds.Item, len(torrents))
for i, _ := range torrents {
timestamp_as_time, err := time.Parse("2006-01-02 15:04:05", torrents[i].Timestamp)
if err == nil {
feed.Items[i] = &feeds.Item{
// need a torrent view first
//Id: URL + torrents[i].Hash,
Title: torrents[i].Name,
Link: &feeds.Link{Href: string(torrents[i].Magnet)},
Description: "",
Created: timestamp_as_time,
Updated: timestamp_as_time,
}
}
}
rss, err := feed.ToRss()
if err == nil {
w.Write( []byte( rss ) )
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
2017-05-02 12:39:53 +02:00
func rootHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
page := vars["page"]
2017-05-04 00:20:50 +02:00
// db params url
maxPerPage, errConv := strconv.Atoi(r.URL.Query().Get("max"))
if errConv != nil {
maxPerPage = 50 // default Value maxPerPage
2017-05-04 00:20:50 +02:00
}
nbTorrents := 0
2017-05-02 12:39:53 +02:00
pagenum, _ := strconv.Atoi(html.EscapeString(page))
b := []TorrentsJson{}
torrents := getAllTorrents(maxPerPage, maxPerPage*(pagenum-1))
for i, _ := range torrents {
nbTorrents++
res := torrents[i].toJson()
b = append(b, res)
2017-05-02 12:39:53 +02:00
}
2017-05-04 14:14:29 +02:00
htv := HomeTemplateVariables{b, getAllCategories(false), "", "", "_", maxPerPage, nbTorrents}
err := templates.ExecuteTemplate(w, "index.html", htv)
2017-05-02 12:39:53 +02:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func main() {
db = getDBHandle()
2017-05-02 12:39:53 +02:00
router := mux.NewRouter()
cssHandler := http.FileServer(http.Dir("./css/"))
jsHandler := http.FileServer(http.Dir("./js/"))
2017-05-04 00:20:50 +02:00
http.Handle("/css/", http.StripPrefix("/css/", cssHandler))
http.Handle("/js/", http.StripPrefix("/js/", jsHandler))
2017-05-02 12:39:53 +02:00
// Routes,
router.HandleFunc("/", rootHandler)
router.HandleFunc("/page/{page}", rootHandler)
router.HandleFunc("/search", searchHandler)
2017-05-02 13:07:04 +02:00
router.HandleFunc("/search/{page}", searchHandler)
2017-05-02 12:39:53 +02:00
router.HandleFunc("/api/{page}", apiHandler).Methods("GET")
router.HandleFunc("/api/torrent/{id}", singleapiHandler).Methods("GET")
2017-05-04 01:15:20 +02:00
router.HandleFunc("/faq", faqHandler)
2017-05-04 21:48:40 +02:00
router.HandleFunc("/feed.xml", rssHandler)
http.Handle("/", router)
2017-05-02 12:39:53 +02:00
// Set up server,
srv := &http.Server{
Addr: "localhost:9999",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
err := srv.ListenAndServe()
checkErr(err)
}