package main import ( "fmt" "html/template" "log" "net/http" "net/url" "sort" "strings" "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 } for _, r := range res { r.Magnet = url.QueryEscape(removeMagnetLink(r.Magnet)) // Remove "magnet:" and encode url results = append(results, r) } } if allErrors || len(results) == 0 || results[len(results)-1].Title == "" || results[len(results)-1].Title == " " { 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, } // Debugging: Print results before rendering template for _, result := range results { fmt.Printf("Title: %s, Magnet: %s\n", result.Title, result.Magnet) } 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) } } //this is so fucking stupid, but it does not work otherwise func removeMagnetLink(magnet string) string { // Remove the magnet: prefix if it exists if strings.HasPrefix(magnet, "magnet:?") { magnet = strings.TrimPrefix(magnet, "magnet:?") } return magnet } func subtract(a, b int) int { return a - b } func add(a, b int) int { return a + b }