2025-07-16 15:32:27 -03:00
|
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"net/url"
|
2025-07-22 14:57:05 -03:00
|
|
|
|
"regexp"
|
2025-07-16 15:32:27 -03:00
|
|
|
|
"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 comandohds = IndexerMeta{
|
2025-07-24 01:03:38 -03:00
|
|
|
|
Label: "comandohds",
|
|
|
|
|
|
URL: "https://comandohds.org/",
|
|
|
|
|
|
SearchURL: "?s=",
|
|
|
|
|
|
PagePattern: "page/%s",
|
2025-07-16 15:32:27 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var title_re = regexp.MustCompile(`^[(Filme)|(Série)\s]+`)
|
|
|
|
|
|
|
|
|
|
|
|
func (i *Indexer) HandlerComandoHDsIndexer(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
start := time.Now()
|
2025-07-24 01:03:38 -03:00
|
|
|
|
metadata := comandohds
|
|
|
|
|
|
|
2025-07-16 15:32:27 -03:00
|
|
|
|
defer func() {
|
2025-07-24 01:03:38 -03:00
|
|
|
|
i.metrics.IndexerDuration.WithLabelValues(metadata.Label).Observe(time.Since(start).Seconds())
|
|
|
|
|
|
i.metrics.IndexerRequests.WithLabelValues(metadata.Label).Inc()
|
2025-07-16 15:32:27 -03:00
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
2025-07-24 01:03:38 -03:00
|
|
|
|
url := metadata.URL
|
2025-07-16 15:32:27 -03:00
|
|
|
|
if q != "" {
|
2025-07-24 01:03:38 -03:00
|
|
|
|
url = fmt.Sprintf("%s%s%s", url, metadata.SearchURL, q)
|
2025-07-16 15:32:27 -03:00
|
|
|
|
} else if page != "" {
|
2025-07-24 01:03:38 -03:00
|
|
|
|
url = fmt.Sprintf(fmt.Sprintf("%s%s", url, metadata.PagePattern), page)
|
2025-07-16 15:32:27 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
}
|
2025-07-24 01:03:38 -03:00
|
|
|
|
i.metrics.IndexerErrors.WithLabelValues(metadata.Label).Inc()
|
2025-07-16 15:32:27 -03:00
|
|
|
|
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)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 01:03:38 -03:00
|
|
|
|
i.metrics.IndexerErrors.WithLabelValues(metadata.Label).Inc()
|
2025-07-16 15:32:27 -03:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var links []string
|
|
|
|
|
|
doc.Find(".post").Each(func(i int, s *goquery.Selection) {
|
|
|
|
|
|
link, _ := s.Find("div.title > a").Attr("href")
|
|
|
|
|
|
links = append(links, link)
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2025-07-30 14:16:06 +00:00
|
|
|
|
// if no links were indexed, expire the document in cache
|
|
|
|
|
|
if len(links) == 0 {
|
2025-07-30 14:21:02 +00:00
|
|
|
|
_ = i.requester.ExpireDocument(ctx, url)
|
2025-07-30 14:16:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 01:03:38 -03:00
|
|
|
|
// extract each torrent link
|
2025-07-29 12:34:37 -03:00
|
|
|
|
indexedTorrents := utils.ParallelFlatMap(links, func(link string) ([]schema.IndexedTorrent, error) {
|
2025-07-24 01:03:38 -03:00
|
|
|
|
return getTorrentsComandoHDs(ctx, i, link)
|
2025-07-16 15:32:27 -03:00
|
|
|
|
})
|
|
|
|
|
|
|
2025-07-24 01:03:38 -03:00
|
|
|
|
// Apply post-processors
|
|
|
|
|
|
postProcessedTorrents := indexedTorrents
|
|
|
|
|
|
for _, processor := range i.postProcessors {
|
|
|
|
|
|
postProcessedTorrents = processor(i, r, postProcessedTorrents)
|
|
|
|
|
|
}
|
2025-07-16 15:32:27 -03:00
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(Response{
|
2025-07-24 01:03:38 -03:00
|
|
|
|
Results: postProcessedTorrents,
|
|
|
|
|
|
Count: len(postProcessedTorrents),
|
2025-07-16 15:32:27 -03:00
|
|
|
|
})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func getTorrentsComandoHDs(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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
article := doc.Find("article")
|
|
|
|
|
|
title := title_re.ReplaceAllString(article.Find(".main_title > h1").Text(), "")
|
|
|
|
|
|
textContent := article.Find("div.content")
|
2025-07-22 14:57:05 -03:00
|
|
|
|
date := getPublishedDateFromMeta(doc)
|
2025-07-16 15:32:27 -03:00
|
|
|
|
magnets := textContent.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
|
|
|
|
|
|
article.Find("div.content p").Each(func(i int, s *goquery.Selection) {
|
|
|
|
|
|
// pattern:
|
|
|
|
|
|
// »INFORMAÇÕES«
|
|
|
|
|
|
// Titulo Traduzido: O Guerreiro Banido
|
|
|
|
|
|
// Titulo Original: 天龍八部之喬峰傳
|
|
|
|
|
|
// <picture />: 5.7
|
|
|
|
|
|
// Ano de Lançamento: 2023
|
|
|
|
|
|
// Gênero: Ação
|
|
|
|
|
|
// Formato: MKV
|
|
|
|
|
|
// Qualidade: WEB-DL
|
|
|
|
|
|
// Idioma: Português | Inglês
|
|
|
|
|
|
// Legenda: Português
|
|
|
|
|
|
// Tamanho: – GB
|
|
|
|
|
|
// Qualidade Áudio e Vídeo: 10
|
|
|
|
|
|
// Duração: 130 Min
|
|
|
|
|
|
// Servidor: Torrent
|
|
|
|
|
|
text := s.Text()
|
|
|
|
|
|
|
|
|
|
|
|
audio = append(audio, findAudioFromText(text)...)
|
|
|
|
|
|
y := findYearFromText(text, title)
|
|
|
|
|
|
if y != "" {
|
|
|
|
|
|
year = y
|
|
|
|
|
|
}
|
|
|
|
|
|
size = append(size, findSizesFromText(text)...)
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// find any link from imdb
|
|
|
|
|
|
imdbLink := ""
|
|
|
|
|
|
article.Find("div.content a").Each(func(i int, s *goquery.Selection) {
|
|
|
|
|
|
link, _ := s.Attr("href")
|
|
|
|
|
|
_imdbLink, err := getIMDBLink(link)
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
imdbLink = _imdbLink
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2025-07-24 01:03:38 -03:00
|
|
|
|
size = utils.StableUniq(size)
|
2025-07-16 15:32:27 -03:00
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
}
|
2025-07-22 14:57:05 -03:00
|
|
|
|
releaseTitle := strings.TrimSpace(magnet.DisplayName)
|
2025-07-16 15:32:27 -03:00
|
|
|
|
infoHash := magnet.InfoHash.String()
|
|
|
|
|
|
trackers := magnet.Trackers
|
2025-07-22 14:57:05 -03:00
|
|
|
|
for i, tracker := range trackers {
|
|
|
|
|
|
trackers[i] = strings.TrimSpace(tracker)
|
2025-07-16 15:32:27 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-22 14:57:05 -03:00
|
|
|
|
magnetAudio := getAudioFromTitle(releaseTitle, audio)
|
|
|
|
|
|
|
2025-07-16 15:32:27 -03:00
|
|
|
|
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]
|
|
|
|
|
|
}
|
2025-07-29 12:34:37 -03:00
|
|
|
|
if mySize == "" {
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
_, _ = i.magnetMetadataAPI.FetchMetadata(ctx, magnetLink)
|
|
|
|
|
|
}()
|
|
|
|
|
|
}
|
2025-07-16 15:32:27 -03:00
|
|
|
|
|
|
|
|
|
|
ixt := schema.IndexedTorrent{
|
2025-07-24 01:03:38 -03:00
|
|
|
|
Title: releaseTitle,
|
2025-07-16 15:32:27 -03:00
|
|
|
|
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
|
|
|
|
|
|
}
|