Search/files.go

168 lines
4.1 KiB
Go

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)
}
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()
cacheKey := CacheKey{Query: query, Page: page, Safe: safe == "true", Lang: lang, Type: "file"}
combinedResults := getFileResultsFromCacheOrFetch(cacheKey, query, safe, lang, page)
sort.Slice(combinedResults, func(i, j int) bool { return combinedResults[i].Seeders > combinedResults[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: combinedResults,
Query: query,
Fetched: fmt.Sprintf("%.2f", elapsedTime.Seconds()),
Category: "all",
Sort: "seed",
HasPrevPage: page > 1,
HasNextPage: len(combinedResults) > 0,
Page: page,
Settings: Settings{UxLang: lang, Safe: safe},
}
// Debugging: Print results before rendering template
for _, result := range combinedResults {
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)
}
}
func getFileResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string, page int) []TorrentResult {
cacheChan := make(chan []SearchResult)
var combinedResults []TorrentResult
go func() {
results, exists := resultsCache.Get(cacheKey)
if exists {
log.Println("Cache hit")
cacheChan <- results
} else {
log.Println("Cache miss")
cacheChan <- nil
}
}()
select {
case results := <-cacheChan:
if results == nil {
combinedResults = fetchAndCacheFileResults(query, safe, lang, page)
} else {
_, torrentResults, _ := convertToSpecificResults(results)
combinedResults = torrentResults
}
case <-time.After(2 * time.Second):
log.Println("Cache check timeout")
combinedResults = fetchAndCacheFileResults(query, safe, lang, page)
}
return combinedResults
}
func fetchAndCacheFileResults(query, safe, lang string, page int) []TorrentResult {
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 == " " {
return []TorrentResult{
{Error: "Results are currently unavailable, sorry. Please try again later."},
}
}
// Cache the valid results
cacheKey := CacheKey{Query: query, Page: page, Safe: safe == "true", Lang: lang, Type: "file"}
resultsCache.Set(cacheKey, convertToSearchResults(results))
return results
}
func removeMagnetLink(magnet string) string {
// Remove the magnet: prefix unconditionally
return strings.TrimPrefix(magnet, "magnet:?")
}
func subtract(a, b int) int {
return a - b
}
func add(a, b int) int {
return a + b
}