* new: feat: add magnet-metadata-api post processor * chg: fix: lint issue * chg: chore: comment optional containers * chg: fix: remove redundant check
227 lines
6.0 KiB
Go
227 lines
6.0 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
"github.com/felipemarinho97/torrent-indexer/magnet"
|
|
"github.com/felipemarinho97/torrent-indexer/schema"
|
|
goscrape "github.com/felipemarinho97/torrent-indexer/scrape"
|
|
"github.com/felipemarinho97/torrent-indexer/utils"
|
|
)
|
|
|
|
var starck_filmes = IndexerMeta{
|
|
Label: "starck_filmes",
|
|
URL: "https://www.starckfilmes.online/",
|
|
SearchURL: "?s=",
|
|
PagePattern: "page/%s",
|
|
}
|
|
|
|
func (i *Indexer) HandlerStarckFilmesIndexer(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
metadata := starck_filmes
|
|
|
|
defer func() {
|
|
i.metrics.IndexerDuration.WithLabelValues(metadata.Label).Observe(time.Since(start).Seconds())
|
|
i.metrics.IndexerRequests.WithLabelValues(metadata.Label).Inc()
|
|
}()
|
|
|
|
ctx := r.Context()
|
|
// supported query params: q, page, filter_results
|
|
q := r.URL.Query().Get("q")
|
|
page := r.URL.Query().Get("page")
|
|
|
|
// URL encode query param
|
|
q = url.QueryEscape(q)
|
|
url := metadata.URL
|
|
if q != "" {
|
|
url = fmt.Sprintf("%s%s%s", url, metadata.SearchURL, q)
|
|
} else if page != "" {
|
|
url = fmt.Sprintf(fmt.Sprintf("%s%s", url, metadata.PagePattern), page)
|
|
}
|
|
|
|
fmt.Println("URL:>", url)
|
|
resp, err := i.requester.GetDocument(ctx, url)
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
err = json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
i.metrics.IndexerErrors.WithLabelValues(metadata.Label).Inc()
|
|
return
|
|
}
|
|
defer resp.Close()
|
|
|
|
doc, err := goquery.NewDocumentFromReader(resp)
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
err = json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
|
|
i.metrics.IndexerErrors.WithLabelValues(metadata.Label).Inc()
|
|
return
|
|
}
|
|
|
|
var links []string
|
|
doc.Find(".item").Each(func(i int, s *goquery.Selection) {
|
|
link, _ := s.Find("div.sub-item > a").Attr("href")
|
|
links = append(links, link)
|
|
})
|
|
|
|
// extract each torrent link
|
|
indexedTorrents := utils.ParallelFlatMap(links, func(link string) ([]schema.IndexedTorrent, error) {
|
|
return getTorrentStarckFilmes(ctx, i, link)
|
|
})
|
|
|
|
// Apply post-processors
|
|
postProcessedTorrents := indexedTorrents
|
|
for _, processor := range i.postProcessors {
|
|
postProcessedTorrents = processor(i, r, postProcessedTorrents)
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
err = json.NewEncoder(w).Encode(Response{
|
|
Results: postProcessedTorrents,
|
|
Count: len(postProcessedTorrents),
|
|
})
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
}
|
|
|
|
func getTorrentStarckFilmes(ctx context.Context, i *Indexer, link string) ([]schema.IndexedTorrent, error) {
|
|
var indexedTorrents []schema.IndexedTorrent
|
|
doc, err := getDocument(ctx, i, link)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
date := getPublishedDateFromRawString(link)
|
|
|
|
post := doc.Find(".post")
|
|
capa := post.Find(".capa")
|
|
title := capa.Find(".post-description > h2").Text()
|
|
post_buttons := post.Find(".post-buttons")
|
|
magnets := post_buttons.Find("a[href^=\"magnet\"]")
|
|
var magnetLinks []string
|
|
magnets.Each(func(i int, s *goquery.Selection) {
|
|
magnetLink, _ := s.Attr("href")
|
|
magnetLinks = append(magnetLinks, magnetLink)
|
|
})
|
|
|
|
var audio []schema.Audio
|
|
var year string
|
|
var size []string
|
|
capa.Find(".post-description p").Each(func(i int, s *goquery.Selection) {
|
|
// pattern:
|
|
// Nome Original: 28 Weeks Later
|
|
// Lançamento: 2007
|
|
// Duração: 1h 40 min
|
|
// Gênero: Terror, Suspense, Ficção
|
|
// Formato: MKV
|
|
// Tamanho: 2.45 GB
|
|
// Qualidade de Video: 10
|
|
// Qualidade do Audio: 10
|
|
// Idioma: Português | Inglês
|
|
// Legenda: Português, Inglês, Espanhol
|
|
var text strings.Builder
|
|
s.Find("span").Each(func(i int, span *goquery.Selection) {
|
|
text.WriteString(span.Text())
|
|
text.WriteString(" ")
|
|
})
|
|
audio = append(audio, findAudioFromText(text.String())...)
|
|
y := findYearFromText(text.String(), title)
|
|
if y != "" {
|
|
year = y
|
|
}
|
|
size = append(size, findSizesFromText(text.String())...)
|
|
})
|
|
|
|
// TODO: find any link from imdb
|
|
imdbLink := ""
|
|
|
|
size = utils.StableUniq(size)
|
|
|
|
var chanIndexedTorrent = make(chan schema.IndexedTorrent)
|
|
|
|
// for each magnet link, create a new indexed torrent
|
|
for it, magnetLink := range magnetLinks {
|
|
it := it
|
|
go func(it int, magnetLink string) {
|
|
magnet, err := magnet.ParseMagnetUri(magnetLink)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
releaseTitle := strings.TrimSpace(magnet.DisplayName)
|
|
// url decode the title
|
|
releaseTitle, err = url.QueryUnescape(releaseTitle)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
releaseTitle = strings.TrimSpace(magnet.DisplayName)
|
|
}
|
|
infoHash := magnet.InfoHash.String()
|
|
trackers := magnet.Trackers
|
|
for i, tracker := range trackers {
|
|
unescapedTracker, err := url.QueryUnescape(tracker)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
trackers[i] = strings.TrimSpace(unescapedTracker)
|
|
}
|
|
magnetAudio := getAudioFromTitle(releaseTitle, audio)
|
|
|
|
peer, seed, err := goscrape.GetLeechsAndSeeds(ctx, i.redis, i.metrics, infoHash, trackers)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
|
|
title := processTitle(title, magnetAudio)
|
|
|
|
// if the number of sizes is equal to the number of magnets, then assign the size to each indexed torrent in order
|
|
var mySize string
|
|
if len(size) == len(magnetLinks) {
|
|
mySize = size[it]
|
|
}
|
|
if mySize == "" {
|
|
go func() {
|
|
_, _ = i.magnetMetadataAPI.FetchMetadata(ctx, magnetLink)
|
|
}()
|
|
}
|
|
|
|
ixt := schema.IndexedTorrent{
|
|
Title: releaseTitle,
|
|
OriginalTitle: title,
|
|
Details: link,
|
|
Year: year,
|
|
IMDB: imdbLink,
|
|
Audio: magnetAudio,
|
|
MagnetLink: magnetLink,
|
|
Date: date,
|
|
InfoHash: infoHash,
|
|
Trackers: trackers,
|
|
LeechCount: peer,
|
|
SeedCount: seed,
|
|
Size: mySize,
|
|
}
|
|
chanIndexedTorrent <- ixt
|
|
}(it, magnetLink)
|
|
}
|
|
|
|
for i := 0; i < len(magnetLinks); i++ {
|
|
it := <-chanIndexedTorrent
|
|
indexedTorrents = append(indexedTorrents, it)
|
|
}
|
|
|
|
return indexedTorrents, nil
|
|
}
|