123 lines
2.4 KiB
Go
123 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"html/template"
|
|
"log"
|
|
"net/http"
|
|
"sort"
|
|
"time"
|
|
)
|
|
|
|
type Settings struct {
|
|
UxLang string
|
|
Safe string
|
|
}
|
|
|
|
type TorrentSite interface {
|
|
Name() string
|
|
Search(query string, category string) ([]TorrentResult, error)
|
|
}
|
|
|
|
type TorrentResult struct {
|
|
URL string
|
|
Seeders int
|
|
Leechers int
|
|
Magnet string
|
|
Views int
|
|
Size string
|
|
Title string
|
|
Error string
|
|
}
|
|
|
|
var (
|
|
torrentGalaxy TorrentSite
|
|
nyaa TorrentSite
|
|
thePirateBay TorrentSite
|
|
rutor TorrentSite
|
|
)
|
|
|
|
func initializeTorrentSites() {
|
|
torrentGalaxy = NewTorrentGalaxy()
|
|
// nyaa = NewNyaa()
|
|
// thePirateBay = NewThePirateBay()
|
|
// rutor = NewRutor()
|
|
}
|
|
|
|
func handleFileSearch(w http.ResponseWriter, query, safe, lang string, page int) {
|
|
startTime := time.Now()
|
|
|
|
settings := Settings{UxLang: lang, Safe: safe}
|
|
sites := []TorrentSite{torrentGalaxy, nyaa, thePirateBay, rutor}
|
|
results := []TorrentResult{}
|
|
allErrors := true
|
|
|
|
for _, site := range sites {
|
|
if site == nil {
|
|
continue
|
|
}
|
|
res, err := site.Search(query, "all")
|
|
if err != nil {
|
|
continue
|
|
}
|
|
if len(res) > 0 {
|
|
allErrors = false
|
|
}
|
|
results = append(results, res...)
|
|
}
|
|
|
|
if allErrors {
|
|
results = []TorrentResult{
|
|
{Error: "Results are currently unavailable, sorry. Please try again later."},
|
|
}
|
|
}
|
|
|
|
sort.Slice(results, func(i, j int) bool { return results[i].Seeders > results[j].Seeders })
|
|
|
|
elapsedTime := time.Since(startTime)
|
|
funcMap := template.FuncMap{
|
|
"sub": subtract,
|
|
"add": add,
|
|
}
|
|
tmpl, err := template.New("files.html").Funcs(funcMap).ParseFiles("templates/files.html")
|
|
if err != nil {
|
|
log.Printf("Failed to load template: %v", err)
|
|
http.Error(w, "Failed to load template", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
data := struct {
|
|
Results []TorrentResult
|
|
Query string
|
|
Fetched string
|
|
Category string
|
|
Sort string
|
|
HasPrevPage bool
|
|
HasNextPage bool
|
|
Page int
|
|
Settings Settings
|
|
}{
|
|
Results: results,
|
|
Query: query,
|
|
Fetched: fmt.Sprintf("%.2f", elapsedTime.Seconds()),
|
|
Category: "all",
|
|
Sort: "seed",
|
|
HasPrevPage: page > 1,
|
|
HasNextPage: len(results) > 0,
|
|
Page: page,
|
|
Settings: settings,
|
|
}
|
|
|
|
if err := tmpl.Execute(w, data); err != nil {
|
|
log.Printf("Failed to render template: %v", err)
|
|
http.Error(w, "Failed to render template", http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
func subtract(a, b int) int {
|
|
return a - b
|
|
}
|
|
|
|
func add(a, b int) int {
|
|
return a + b
|
|
}
|