2023-10-01 20:27:44 +00:00
|
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"net/url"
|
2024-03-10 14:58:15 -03:00
|
|
|
|
"slices"
|
2023-10-01 20:27:44 +00:00
|
|
|
|
"strings"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
2024-03-10 14:58:15 -03:00
|
|
|
|
"github.com/hbollon/go-edlib"
|
|
|
|
|
|
|
2023-10-01 20:27:44 +00:00
|
|
|
|
"github.com/felipemarinho97/torrent-indexer/magnet"
|
|
|
|
|
|
"github.com/felipemarinho97/torrent-indexer/schema"
|
|
|
|
|
|
goscrape "github.com/felipemarinho97/torrent-indexer/scrape"
|
2024-03-10 14:58:15 -03:00
|
|
|
|
"github.com/felipemarinho97/torrent-indexer/utils"
|
2023-10-01 20:27:44 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var bludv = IndexerMeta{
|
2024-12-11 16:04:48 -03:00
|
|
|
|
URL: "https://bludv.xyz/",
|
2023-10-01 20:27:44 +00:00
|
|
|
|
SearchURL: "?s=",
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (i *Indexer) HandlerBluDVIndexer(w http.ResponseWriter, r *http.Request) {
|
2024-03-10 14:00:07 +00:00
|
|
|
|
start := time.Now()
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
i.metrics.IndexerDuration.WithLabelValues("bludv").Observe(time.Since(start).Seconds())
|
|
|
|
|
|
i.metrics.IndexerRequests.WithLabelValues("bludv").Inc()
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
2023-10-01 20:27:44 +00:00
|
|
|
|
ctx := r.Context()
|
2024-12-11 16:04:48 -03:00
|
|
|
|
// supported query params: q, season, episode, page, filter_results
|
2023-10-01 20:27:44 +00:00
|
|
|
|
q := r.URL.Query().Get("q")
|
2024-12-11 16:04:48 -03:00
|
|
|
|
page := r.URL.Query().Get("page")
|
2023-10-01 20:27:44 +00:00
|
|
|
|
|
|
|
|
|
|
// URL encode query param
|
|
|
|
|
|
q = url.QueryEscape(q)
|
|
|
|
|
|
url := bludv.URL
|
2024-12-13 11:54:55 -03:00
|
|
|
|
if page != "" {
|
2024-12-11 16:04:48 -03:00
|
|
|
|
url = fmt.Sprintf("%spage/%s", url, page)
|
2024-12-13 11:54:55 -03:00
|
|
|
|
} else {
|
|
|
|
|
|
url = fmt.Sprintf("%s%s%s", url, bludv.SearchURL, q)
|
2023-10-01 20:27:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fmt.Println("URL:>", url)
|
2024-09-24 18:31:58 -03:00
|
|
|
|
resp, err := i.requester.GetDocument(ctx, url)
|
2023-10-01 20:27:44 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2024-04-28 11:52:01 -03:00
|
|
|
|
err = json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
|
}
|
2024-03-10 14:00:07 +00:00
|
|
|
|
i.metrics.IndexerErrors.WithLabelValues("bludv").Inc()
|
2023-10-01 20:27:44 +00:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2024-09-24 18:31:58 -03:00
|
|
|
|
defer resp.Close()
|
2023-10-01 20:27:44 +00:00
|
|
|
|
|
2024-09-24 18:31:58 -03:00
|
|
|
|
doc, err := goquery.NewDocumentFromReader(resp)
|
2023-10-01 20:27:44 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2024-04-28 11:52:01 -03:00
|
|
|
|
err = json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-10 14:00:07 +00:00
|
|
|
|
i.metrics.IndexerErrors.WithLabelValues("bludv").Inc()
|
2023-10-01 20:27:44 +00:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var links []string
|
|
|
|
|
|
doc.Find(".post").Each(func(i int, s *goquery.Selection) {
|
|
|
|
|
|
// get link from h2.entry-title > a
|
|
|
|
|
|
link, _ := s.Find("div.title > a").Attr("href")
|
|
|
|
|
|
links = append(links, link)
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2024-12-13 11:54:55 -03:00
|
|
|
|
var itChan = make(chan []schema.IndexedTorrent)
|
2023-10-01 20:27:44 +00:00
|
|
|
|
var errChan = make(chan error)
|
2024-12-13 11:54:55 -03:00
|
|
|
|
indexedTorrents := []schema.IndexedTorrent{}
|
2023-10-01 20:27:44 +00:00
|
|
|
|
for _, link := range links {
|
|
|
|
|
|
go func(link string) {
|
|
|
|
|
|
torrents, err := getTorrentsBluDV(ctx, i, link)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
|
errChan <- err
|
|
|
|
|
|
}
|
|
|
|
|
|
itChan <- torrents
|
|
|
|
|
|
}(link)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for i := 0; i < len(links); i++ {
|
|
|
|
|
|
select {
|
|
|
|
|
|
case torrents := <-itChan:
|
|
|
|
|
|
indexedTorrents = append(indexedTorrents, torrents...)
|
|
|
|
|
|
case err := <-errChan:
|
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-10 14:58:15 -03:00
|
|
|
|
for i, it := range indexedTorrents {
|
|
|
|
|
|
jLower := strings.ReplaceAll(strings.ToLower(fmt.Sprintf("%s %s", it.Title, it.OriginalTitle)), ".", " ")
|
|
|
|
|
|
qLower := strings.ToLower(q)
|
|
|
|
|
|
splitLength := 2
|
|
|
|
|
|
indexedTorrents[i].Similarity = edlib.JaccardSimilarity(jLower, qLower, splitLength)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// remove the ones with zero similarity
|
|
|
|
|
|
if len(indexedTorrents) > 20 && r.URL.Query().Get("filter_results") != "" && r.URL.Query().Get("q") != "" {
|
2024-12-13 11:54:55 -03:00
|
|
|
|
indexedTorrents = utils.Filter(indexedTorrents, func(it schema.IndexedTorrent) bool {
|
2024-03-10 14:58:15 -03:00
|
|
|
|
return it.Similarity > 0
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// sort by similarity
|
2024-12-13 11:54:55 -03:00
|
|
|
|
slices.SortFunc(indexedTorrents, func(i, j schema.IndexedTorrent) int {
|
2024-03-10 14:58:15 -03:00
|
|
|
|
return int((j.Similarity - i.Similarity) * 1000)
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2024-12-13 11:54:55 -03:00
|
|
|
|
// send to search index
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
_ = i.search.IndexTorrents(indexedTorrents)
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
2023-10-01 20:27:44 +00:00
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2024-04-28 11:52:01 -03:00
|
|
|
|
err = json.NewEncoder(w).Encode(Response{
|
2024-02-12 17:04:00 +00:00
|
|
|
|
Results: indexedTorrents,
|
|
|
|
|
|
Count: len(indexedTorrents),
|
|
|
|
|
|
})
|
2024-04-28 11:52:01 -03:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
|
}
|
2023-10-01 20:27:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-13 11:54:55 -03:00
|
|
|
|
func getTorrentsBluDV(ctx context.Context, i *Indexer, link string) ([]schema.IndexedTorrent, error) {
|
|
|
|
|
|
var indexedTorrents []schema.IndexedTorrent
|
2023-10-01 20:27:44 +00:00
|
|
|
|
doc, err := getDocument(ctx, i, link)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
article := doc.Find(".post")
|
|
|
|
|
|
title := strings.Replace(article.Find(".title > h1").Text(), " - Download", "", -1)
|
|
|
|
|
|
textContent := article.Find("div.content")
|
2024-03-10 12:48:48 +00:00
|
|
|
|
date := getPublishedDate(doc)
|
2023-10-01 20:27:44 +00: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:
|
|
|
|
|
|
// Título Traduzido: Fundação
|
|
|
|
|
|
// Título Original: Foundation
|
|
|
|
|
|
// IMDb: 7,5
|
|
|
|
|
|
// Ano de Lançamento: 2023
|
|
|
|
|
|
// Gênero: Ação | Aventura | Ficção
|
|
|
|
|
|
// Formato: MKV
|
|
|
|
|
|
// Qualidade: WEB-DL
|
|
|
|
|
|
// Áudio: Português | Inglês
|
|
|
|
|
|
// Idioma: Português | Inglês
|
|
|
|
|
|
// Legenda: Português
|
|
|
|
|
|
// Tamanho: –
|
|
|
|
|
|
// Qualidade de Áudio: 10
|
|
|
|
|
|
// Qualidade de Vídeo: 10
|
|
|
|
|
|
// Duração: 59 Min.
|
|
|
|
|
|
// Servidor: Torrent
|
|
|
|
|
|
text := s.Text()
|
|
|
|
|
|
|
|
|
|
|
|
audio = append(audio, findAudioFromText(text)...)
|
2024-03-10 12:48:48 +00:00
|
|
|
|
y := findYearFromText(text, title)
|
|
|
|
|
|
if y != "" {
|
|
|
|
|
|
year = y
|
|
|
|
|
|
}
|
2023-10-01 20:27:44 +00:00
|
|
|
|
size = append(size, findSizesFromText(text)...)
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2024-02-12 17:04:00 +00:00
|
|
|
|
// find any link from imdb
|
|
|
|
|
|
imdbLink := ""
|
|
|
|
|
|
article.Find("div.content a").Each(func(i int, s *goquery.Selection) {
|
|
|
|
|
|
link, _ := s.Attr("href")
|
2024-12-13 12:45:17 -03:00
|
|
|
|
_imdbLink, err := getIMDBLink(link)
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
imdbLink = _imdbLink
|
2024-02-12 17:04:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2023-10-01 20:27:44 +00:00
|
|
|
|
size = stableUniq(size)
|
|
|
|
|
|
|
2024-12-13 11:54:55 -03:00
|
|
|
|
var chanIndexedTorrent = make(chan schema.IndexedTorrent)
|
2023-10-01 20:27:44 +00:00
|
|
|
|
|
|
|
|
|
|
// 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 := magnet.DisplayName
|
|
|
|
|
|
infoHash := magnet.InfoHash.String()
|
|
|
|
|
|
trackers := magnet.Trackers
|
|
|
|
|
|
magnetAudio := []schema.Audio{}
|
|
|
|
|
|
if strings.Contains(strings.ToLower(releaseTitle), "dual") || strings.Contains(strings.ToLower(releaseTitle), "dublado") {
|
|
|
|
|
|
magnetAudio = append(magnetAudio, audio...)
|
|
|
|
|
|
} else if len(audio) > 1 {
|
|
|
|
|
|
// remove portuguese audio, and append to magnetAudio
|
|
|
|
|
|
for _, a := range audio {
|
|
|
|
|
|
if a != schema.AudioPortuguese {
|
|
|
|
|
|
magnetAudio = append(magnetAudio, a)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
magnetAudio = append(magnetAudio, audio...)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-10 14:00:07 +00:00
|
|
|
|
peer, seed, err := goscrape.GetLeechsAndSeeds(ctx, i.redis, i.metrics, infoHash, trackers)
|
2023-10-01 20:27:44 +00:00
|
|
|
|
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]
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-13 11:54:55 -03:00
|
|
|
|
ixt := schema.IndexedTorrent{
|
2023-10-01 20:27:44 +00:00
|
|
|
|
Title: appendAudioISO639_2Code(releaseTitle, magnetAudio),
|
|
|
|
|
|
OriginalTitle: title,
|
|
|
|
|
|
Details: link,
|
|
|
|
|
|
Year: year,
|
2024-02-12 17:04:00 +00:00
|
|
|
|
IMDB: imdbLink,
|
2023-10-01 20:27:44 +00:00
|
|
|
|
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
|
|
|
|
|
|
}
|
2024-03-10 12:48:48 +00:00
|
|
|
|
|
|
|
|
|
|
func getPublishedDate(document *goquery.Document) time.Time {
|
|
|
|
|
|
var date time.Time
|
|
|
|
|
|
//<meta property="article:published_time" content="2019-08-23T13:20:57+00:00">
|
|
|
|
|
|
datePublished := strings.TrimSpace(document.Find("meta[property=\"article:published_time\"]").AttrOr("content", ""))
|
|
|
|
|
|
|
|
|
|
|
|
if datePublished != "" {
|
|
|
|
|
|
date, _ = time.Parse(time.RFC3339, datePublished)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return date
|
|
|
|
|
|
}
|