Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| dc3cb3be92 | |||
| 88d6d506bf | |||
| 0a702d1893 | |||
| a6c3d3eed1 |
15
README.md
15
README.md
@@ -23,6 +23,21 @@ docker-compose up -d
|
||||
|
||||
The server will be available at [http://localhost:8080/](http://localhost:8080/).
|
||||
|
||||
## Configuration
|
||||
|
||||
You can configure the server using the following environment variables:
|
||||
|
||||
- `PORT`: (optional) The port that the server will listen to. Default: `7006`
|
||||
- `FLARESOLVERR_ADDRESS`: (optional) The address of the FlareSolverr instance. Default: `N/A`
|
||||
- `MEILISEARCH_ADDRESS`: (optional) The address of the MeiliSearch instance. Default: `N/A`
|
||||
- `MEILISEARCH_KEY`: (optional) The API key of the MeiliSearch instance. Default: `N/A`
|
||||
- `REDIS_HOST`: (optional) The address of the Redis instance. Default: `localhost`
|
||||
- `SHORT_LIVED_CACHE_EXPIRATION` (optional) The expiration time of the short-lived cache in duration format. Default: `30m`
|
||||
- This cache is used to cache homepage or search results.
|
||||
- Example: `30m`, `1h`, `1h30m`, `1h30m30s`
|
||||
- `LONG_LIVED_CACHE_EXPIRATION` (optional) The expiration time of the long-lived cache in duration format. Default: `7d`
|
||||
- This cache is used to store the torrent webpages (posts). You can set it to a higher value because the torrent pages are not updated frequently.
|
||||
|
||||
## Integrating with Jackett
|
||||
|
||||
You can integrate this indexer with Jackett by adding a new Torznab custom indexer. Here is an example of how to do it for the `bludv` indexer:
|
||||
|
||||
38
api/bludv.go
38
api/bludv.go
@@ -6,7 +6,6 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -21,7 +20,7 @@ import (
|
||||
)
|
||||
|
||||
var bludv = IndexerMeta{
|
||||
URL: "https://bludvfilmes.tv/",
|
||||
URL: "https://bludv.xyz/",
|
||||
SearchURL: "?s=",
|
||||
}
|
||||
|
||||
@@ -33,13 +32,16 @@ func (i *Indexer) HandlerBluDVIndexer(w http.ResponseWriter, r *http.Request) {
|
||||
}()
|
||||
|
||||
ctx := r.Context()
|
||||
// supported query params: q, season, episode, filter_results
|
||||
// supported query params: q, season, episode, page, filter_results
|
||||
q := r.URL.Query().Get("q")
|
||||
page := r.URL.Query().Get("page")
|
||||
|
||||
// URL encode query param
|
||||
q = url.QueryEscape(q)
|
||||
url := bludv.URL
|
||||
if q != "" {
|
||||
if page != "" {
|
||||
url = fmt.Sprintf("%spage/%s", url, page)
|
||||
} else {
|
||||
url = fmt.Sprintf("%s%s%s", url, bludv.SearchURL, q)
|
||||
}
|
||||
|
||||
@@ -75,9 +77,9 @@ func (i *Indexer) HandlerBluDVIndexer(w http.ResponseWriter, r *http.Request) {
|
||||
links = append(links, link)
|
||||
})
|
||||
|
||||
var itChan = make(chan []IndexedTorrent)
|
||||
var itChan = make(chan []schema.IndexedTorrent)
|
||||
var errChan = make(chan error)
|
||||
indexedTorrents := []IndexedTorrent{}
|
||||
indexedTorrents := []schema.IndexedTorrent{}
|
||||
for _, link := range links {
|
||||
go func(link string) {
|
||||
torrents, err := getTorrentsBluDV(ctx, i, link)
|
||||
@@ -107,16 +109,21 @@ func (i *Indexer) HandlerBluDVIndexer(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// remove the ones with zero similarity
|
||||
if len(indexedTorrents) > 20 && r.URL.Query().Get("filter_results") != "" && r.URL.Query().Get("q") != "" {
|
||||
indexedTorrents = utils.Filter(indexedTorrents, func(it IndexedTorrent) bool {
|
||||
indexedTorrents = utils.Filter(indexedTorrents, func(it schema.IndexedTorrent) bool {
|
||||
return it.Similarity > 0
|
||||
})
|
||||
}
|
||||
|
||||
// sort by similarity
|
||||
slices.SortFunc(indexedTorrents, func(i, j IndexedTorrent) int {
|
||||
slices.SortFunc(indexedTorrents, func(i, j schema.IndexedTorrent) int {
|
||||
return int((j.Similarity - i.Similarity) * 1000)
|
||||
})
|
||||
|
||||
// send to search index
|
||||
go func() {
|
||||
_ = i.search.IndexTorrents(indexedTorrents)
|
||||
}()
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
err = json.NewEncoder(w).Encode(Response{
|
||||
Results: indexedTorrents,
|
||||
@@ -127,8 +134,8 @@ func (i *Indexer) HandlerBluDVIndexer(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func getTorrentsBluDV(ctx context.Context, i *Indexer, link string) ([]IndexedTorrent, error) {
|
||||
var indexedTorrents []IndexedTorrent
|
||||
func getTorrentsBluDV(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
|
||||
@@ -179,16 +186,15 @@ func getTorrentsBluDV(ctx context.Context, i *Indexer, link string) ([]IndexedTo
|
||||
imdbLink := ""
|
||||
article.Find("div.content a").Each(func(i int, s *goquery.Selection) {
|
||||
link, _ := s.Attr("href")
|
||||
re := regexp.MustCompile(`https://www.imdb.com/title/(tt\d+)`)
|
||||
matches := re.FindStringSubmatch(link)
|
||||
if len(matches) > 0 {
|
||||
imdbLink = matches[0]
|
||||
_imdbLink, err := getIMDBLink(link)
|
||||
if err == nil {
|
||||
imdbLink = _imdbLink
|
||||
}
|
||||
})
|
||||
|
||||
size = stableUniq(size)
|
||||
|
||||
var chanIndexedTorrent = make(chan IndexedTorrent)
|
||||
var chanIndexedTorrent = make(chan schema.IndexedTorrent)
|
||||
|
||||
// for each magnet link, create a new indexed torrent
|
||||
for it, magnetLink := range magnetLinks {
|
||||
@@ -228,7 +234,7 @@ func getTorrentsBluDV(ctx context.Context, i *Indexer, link string) ([]IndexedTo
|
||||
mySize = size[it]
|
||||
}
|
||||
|
||||
ixt := IndexedTorrent{
|
||||
ixt := schema.IndexedTorrent{
|
||||
Title: appendAudioISO639_2Code(releaseTitle, magnetAudio),
|
||||
OriginalTitle: title,
|
||||
Details: link,
|
||||
|
||||
@@ -49,14 +49,17 @@ func (i *Indexer) HandlerComandoIndexer(w http.ResponseWriter, r *http.Request)
|
||||
}()
|
||||
|
||||
ctx := r.Context()
|
||||
// supported query params: q, season, episode
|
||||
// supported query params: q, season, episode, page, filter_results
|
||||
q := r.URL.Query().Get("q")
|
||||
page := r.URL.Query().Get("page")
|
||||
|
||||
// URL encode query param
|
||||
q = url.QueryEscape(q)
|
||||
url := comando.URL
|
||||
if q != "" {
|
||||
url = fmt.Sprintf("%s%s%s", url, comando.SearchURL, q)
|
||||
} else if page != "" {
|
||||
url = fmt.Sprintf("%spage/%s", url, page)
|
||||
}
|
||||
|
||||
fmt.Println("URL:>", url)
|
||||
@@ -90,9 +93,9 @@ func (i *Indexer) HandlerComandoIndexer(w http.ResponseWriter, r *http.Request)
|
||||
links = append(links, link)
|
||||
})
|
||||
|
||||
var itChan = make(chan []IndexedTorrent)
|
||||
var itChan = make(chan []schema.IndexedTorrent)
|
||||
var errChan = make(chan error)
|
||||
indexedTorrents := []IndexedTorrent{}
|
||||
indexedTorrents := []schema.IndexedTorrent{}
|
||||
for _, link := range links {
|
||||
go func(link string) {
|
||||
torrents, err := getTorrents(ctx, i, link)
|
||||
@@ -122,16 +125,21 @@ func (i *Indexer) HandlerComandoIndexer(w http.ResponseWriter, r *http.Request)
|
||||
|
||||
// remove the ones with zero similarity
|
||||
if len(indexedTorrents) > 20 && r.URL.Query().Get("filter_results") != "" && r.URL.Query().Get("q") != "" {
|
||||
indexedTorrents = utils.Filter(indexedTorrents, func(it IndexedTorrent) bool {
|
||||
indexedTorrents = utils.Filter(indexedTorrents, func(it schema.IndexedTorrent) bool {
|
||||
return it.Similarity > 0
|
||||
})
|
||||
}
|
||||
|
||||
// sort by similarity
|
||||
slices.SortFunc(indexedTorrents, func(i, j IndexedTorrent) int {
|
||||
slices.SortFunc(indexedTorrents, func(i, j schema.IndexedTorrent) int {
|
||||
return int((j.Similarity - i.Similarity) * 1000)
|
||||
})
|
||||
|
||||
// send to search index
|
||||
go func() {
|
||||
_ = i.search.IndexTorrents(indexedTorrents)
|
||||
}()
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
err = json.NewEncoder(w).Encode(Response{
|
||||
Results: indexedTorrents,
|
||||
@@ -142,8 +150,8 @@ func (i *Indexer) HandlerComandoIndexer(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
}
|
||||
|
||||
func getTorrents(ctx context.Context, i *Indexer, link string) ([]IndexedTorrent, error) {
|
||||
var indexedTorrents []IndexedTorrent
|
||||
func getTorrents(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
|
||||
@@ -155,19 +163,11 @@ func getTorrents(ctx context.Context, i *Indexer, link string) ([]IndexedTorrent
|
||||
// div itemprop="datePublished"
|
||||
datePublished := strings.TrimSpace(article.Find("div[itemprop=\"datePublished\"]").Text())
|
||||
// pattern: 10 de setembro de 2021
|
||||
re := regexp.MustCompile(`(\d{2}) de (\w+) de (\d{4})`)
|
||||
matches := re.FindStringSubmatch(datePublished)
|
||||
var date time.Time
|
||||
if len(matches) > 0 {
|
||||
day := matches[1]
|
||||
month := matches[2]
|
||||
year := matches[3]
|
||||
datePublished = fmt.Sprintf("%s-%s-%s", year, replacer.Replace(month), day)
|
||||
date, err = time.Parse("2006-01-02", datePublished)
|
||||
date, err := parseLocalizedDate(datePublished)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
magnets := textContent.Find("a[href^=\"magnet\"]")
|
||||
var magnetLinks []string
|
||||
magnets.Each(func(i int, s *goquery.Selection) {
|
||||
@@ -209,16 +209,15 @@ func getTorrents(ctx context.Context, i *Indexer, link string) ([]IndexedTorrent
|
||||
imdbLink := ""
|
||||
article.Find("a").Each(func(i int, s *goquery.Selection) {
|
||||
link, _ := s.Attr("href")
|
||||
re := regexp.MustCompile(`https://www.imdb.com/title/(tt\d+)`)
|
||||
matches := re.FindStringSubmatch(link)
|
||||
if len(matches) > 0 {
|
||||
imdbLink = matches[0]
|
||||
_imdbLink, err := getIMDBLink(link)
|
||||
if err == nil {
|
||||
imdbLink = _imdbLink
|
||||
}
|
||||
})
|
||||
|
||||
size = stableUniq(size)
|
||||
|
||||
var chanIndexedTorrent = make(chan IndexedTorrent)
|
||||
var chanIndexedTorrent = make(chan schema.IndexedTorrent)
|
||||
|
||||
// for each magnet link, create a new indexed torrent
|
||||
for it, magnetLink := range magnetLinks {
|
||||
@@ -258,7 +257,7 @@ func getTorrents(ctx context.Context, i *Indexer, link string) ([]IndexedTorrent
|
||||
mySize = size[it]
|
||||
}
|
||||
|
||||
ixt := IndexedTorrent{
|
||||
ixt := schema.IndexedTorrent{
|
||||
Title: appendAudioISO639_2Code(releaseTitle, magnetAudio),
|
||||
OriginalTitle: title,
|
||||
Details: link,
|
||||
@@ -285,6 +284,40 @@ func getTorrents(ctx context.Context, i *Indexer, link string) ([]IndexedTorrent
|
||||
return indexedTorrents, nil
|
||||
}
|
||||
|
||||
func getIMDBLink(link string) (string, error) {
|
||||
var imdbLink string
|
||||
re := regexp.MustCompile(`https://www.imdb.com(/[a-z]{2})?/title/(tt\d+)/?`)
|
||||
|
||||
matches := re.FindStringSubmatch(link)
|
||||
if len(matches) > 0 {
|
||||
imdbLink = matches[0]
|
||||
} else {
|
||||
return "", fmt.Errorf("no imdb link found")
|
||||
}
|
||||
return imdbLink, nil
|
||||
}
|
||||
|
||||
func parseLocalizedDate(datePublished string) (time.Time, error) {
|
||||
re := regexp.MustCompile(`(\d{1,2}) de (\w+) de (\d{4})`)
|
||||
matches := re.FindStringSubmatch(datePublished)
|
||||
if len(matches) > 0 {
|
||||
day := matches[1]
|
||||
// append 0 to single digit day
|
||||
if len(day) == 1 {
|
||||
day = fmt.Sprintf("0%s", day)
|
||||
}
|
||||
month := matches[2]
|
||||
year := matches[3]
|
||||
datePublished = fmt.Sprintf("%s-%s-%s", year, replacer.Replace(month), day)
|
||||
date, err := time.Parse("2006-01-02", datePublished)
|
||||
if err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
return date, nil
|
||||
}
|
||||
return time.Time{}, nil
|
||||
}
|
||||
|
||||
func stableUniq(s []string) []string {
|
||||
var uniq []map[string]interface{}
|
||||
m := make(map[string]map[string]interface{})
|
||||
|
||||
@@ -3,6 +3,7 @@ package handler
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/felipemarinho97/torrent-indexer/schema"
|
||||
)
|
||||
@@ -77,3 +78,115 @@ Servidor Via: Torrent
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_parseLocalizedDate(t *testing.T) {
|
||||
type args struct {
|
||||
datePublished string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want time.Time
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "should return date",
|
||||
args: args{
|
||||
datePublished: "12 de outubro de 2022",
|
||||
},
|
||||
want: time.Date(2022, 10, 12, 0, 0, 0, 0, time.UTC),
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "should return date single digit",
|
||||
args: args{
|
||||
datePublished: "1 de outubro de 2022",
|
||||
},
|
||||
want: time.Date(2022, 10, 1, 0, 0, 0, 0, time.UTC),
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := parseLocalizedDate(tt.args.datePublished)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("parseDate() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("parseDate() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_getIMDBLink(t *testing.T) {
|
||||
type args struct {
|
||||
link string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "should return imdb link",
|
||||
args: args{
|
||||
link: "https://www.imdb.com/title/tt1234567",
|
||||
},
|
||||
want: "https://www.imdb.com/title/tt1234567",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "should return imdb link when end with /",
|
||||
args: args{
|
||||
link: "https://www.imdb.com/title/tt1234567/",
|
||||
},
|
||||
want: "https://www.imdb.com/title/tt1234567/",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "should return imdb link when end with /",
|
||||
args: args{
|
||||
link: "https://www.imdb.com/title/tt1234567/",
|
||||
},
|
||||
want: "https://www.imdb.com/title/tt1234567/",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "should return imdb link when it has a language",
|
||||
args: args{
|
||||
link: "https://www.imdb.com/pt/title/tt18722864/",
|
||||
},
|
||||
want: "https://www.imdb.com/pt/title/tt18722864/",
|
||||
},
|
||||
{
|
||||
name: "should return imdb link when it has a language",
|
||||
args: args{
|
||||
link: "https://www.imdb.com/pt/title/tt34608980/",
|
||||
},
|
||||
want: "https://www.imdb.com/pt/title/tt34608980/",
|
||||
},
|
||||
{
|
||||
name: "should return error when link is invalid",
|
||||
args: args{
|
||||
link: "https://www.google.com",
|
||||
},
|
||||
want: "",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := getIMDBLink(tt.args.link)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("getIMDBLink() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Errorf("getIMDBLink() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
36
api/index.go
36
api/index.go
@@ -9,12 +9,14 @@ import (
|
||||
"github.com/felipemarinho97/torrent-indexer/monitoring"
|
||||
"github.com/felipemarinho97/torrent-indexer/requester"
|
||||
"github.com/felipemarinho97/torrent-indexer/schema"
|
||||
meilisearch "github.com/felipemarinho97/torrent-indexer/search"
|
||||
)
|
||||
|
||||
type Indexer struct {
|
||||
redis *cache.Redis
|
||||
metrics *monitoring.Metrics
|
||||
requester *requester.Requster
|
||||
search *meilisearch.SearchIndexer
|
||||
}
|
||||
|
||||
type IndexerMeta struct {
|
||||
@@ -23,32 +25,16 @@ type IndexerMeta struct {
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Results []IndexedTorrent `json:"results"`
|
||||
Results []schema.IndexedTorrent `json:"results"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
type IndexedTorrent struct {
|
||||
Title string `json:"title"`
|
||||
OriginalTitle string `json:"original_title"`
|
||||
Details string `json:"details"`
|
||||
Year string `json:"year"`
|
||||
IMDB string `json:"imdb"`
|
||||
Audio []schema.Audio `json:"audio"`
|
||||
MagnetLink string `json:"magnet_link"`
|
||||
Date time.Time `json:"date"`
|
||||
InfoHash string `json:"info_hash"`
|
||||
Trackers []string `json:"trackers"`
|
||||
Size string `json:"size"`
|
||||
LeechCount int `json:"leech_count"`
|
||||
SeedCount int `json:"seed_count"`
|
||||
Similarity float32 `json:"similarity"`
|
||||
}
|
||||
|
||||
func NewIndexers(redis *cache.Redis, metrics *monitoring.Metrics, req *requester.Requster) *Indexer {
|
||||
func NewIndexers(redis *cache.Redis, metrics *monitoring.Metrics, req *requester.Requster, si *meilisearch.SearchIndexer) *Indexer {
|
||||
return &Indexer{
|
||||
redis: redis,
|
||||
metrics: metrics,
|
||||
requester: req,
|
||||
search: si,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,6 +51,7 @@ func HandlerIndex(w http.ResponseWriter, r *http.Request) {
|
||||
"description": "Indexer for comando torrents",
|
||||
"query_params": map[string]string{
|
||||
"q": "search query",
|
||||
"page": "page number",
|
||||
"filter_results": "if results with similarity equals to zero should be filtered (true/false)",
|
||||
},
|
||||
},
|
||||
@@ -75,12 +62,14 @@ func HandlerIndex(w http.ResponseWriter, r *http.Request) {
|
||||
"description": "Indexer for bludv",
|
||||
"query_params": map[string]string{
|
||||
"q": "search query",
|
||||
"page": "page number",
|
||||
"filter_results": "if results with similarity equals to zero should be filtered (true/false)",
|
||||
}},
|
||||
},
|
||||
"/indexers/torrent-dos-filmes": []map[string]interface{}{
|
||||
{
|
||||
"method": "GET",
|
||||
"page": "page number",
|
||||
"description": "Indexer for Torrent dos Filmes",
|
||||
"query_params": map[string]string{
|
||||
"q": "search query",
|
||||
@@ -100,6 +89,15 @@ func HandlerIndex(w http.ResponseWriter, r *http.Request) {
|
||||
"description": "Get all manual torrents",
|
||||
},
|
||||
},
|
||||
"/search": []map[string]interface{}{
|
||||
{
|
||||
"method": "GET",
|
||||
"description": "Search for cached torrents across all indexers",
|
||||
"query_params": map[string]string{
|
||||
"q": "search query",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -26,7 +26,7 @@ type ManualIndexerRequest struct {
|
||||
func (i *Indexer) HandlerManualIndexer(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
var req ManualIndexerRequest
|
||||
indexedTorrents := []IndexedTorrent{}
|
||||
indexedTorrents := []schema.IndexedTorrent{}
|
||||
|
||||
// fetch from redis
|
||||
out, err := i.redis.Get(ctx, manualTorrentsRedisKey)
|
||||
@@ -97,7 +97,7 @@ func (i *Indexer) HandlerManualIndexer(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
title := processTitle(releaseTitle, magnetAudio)
|
||||
|
||||
ixt := IndexedTorrent{
|
||||
ixt := schema.IndexedTorrent{
|
||||
Title: appendAudioISO639_2Code(releaseTitle, magnetAudio),
|
||||
OriginalTitle: title,
|
||||
Audio: magnetAudio,
|
||||
|
||||
55
api/search.go
Normal file
55
api/search.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
meilisearch "github.com/felipemarinho97/torrent-indexer/search"
|
||||
)
|
||||
|
||||
// MeilisearchHandler handles HTTP requests for Meilisearch integration.
|
||||
type MeilisearchHandler struct {
|
||||
Module *meilisearch.SearchIndexer
|
||||
}
|
||||
|
||||
// NewMeilisearchHandler creates a new instance of MeilisearchHandler.
|
||||
func NewMeilisearchHandler(module *meilisearch.SearchIndexer) *MeilisearchHandler {
|
||||
return &MeilisearchHandler{Module: module}
|
||||
}
|
||||
|
||||
// SearchTorrentHandler handles the searching of torrent items.
|
||||
func (h *MeilisearchHandler) SearchTorrentHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
query := r.URL.Query().Get("q")
|
||||
if query == "" {
|
||||
http.Error(w, "Query parameter 'q' is required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
limitStr := r.URL.Query().Get("limit")
|
||||
limit := 10 // Default limit
|
||||
if limitStr != "" {
|
||||
var err error
|
||||
limit, err = strconv.Atoi(limitStr)
|
||||
if err != nil || limit <= 0 {
|
||||
http.Error(w, "Invalid limit parameter", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
results, err := h.Module.SearchTorrent(query, limit)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to search torrents", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(w).Encode(results); err != nil {
|
||||
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -33,14 +32,17 @@ func (i *Indexer) HandlerTorrentDosFilmesIndexer(w http.ResponseWriter, r *http.
|
||||
}()
|
||||
|
||||
ctx := r.Context()
|
||||
// supported query params: q, season, episode, filter_results
|
||||
// supported query params: q, season, episode, page, filter_results
|
||||
q := r.URL.Query().Get("q")
|
||||
page := r.URL.Query().Get("page")
|
||||
|
||||
// URL encode query param
|
||||
q = url.QueryEscape(q)
|
||||
url := torrent_dos_filmes.URL
|
||||
if q != "" {
|
||||
url = fmt.Sprintf("%s%s%s", url, torrent_dos_filmes.SearchURL, q)
|
||||
} else if page != "" {
|
||||
url = fmt.Sprintf("%spage/%s", url, page)
|
||||
}
|
||||
|
||||
fmt.Println("URL:>", url)
|
||||
@@ -74,9 +76,9 @@ func (i *Indexer) HandlerTorrentDosFilmesIndexer(w http.ResponseWriter, r *http.
|
||||
links = append(links, link)
|
||||
})
|
||||
|
||||
var itChan = make(chan []IndexedTorrent)
|
||||
var itChan = make(chan []schema.IndexedTorrent)
|
||||
var errChan = make(chan error)
|
||||
indexedTorrents := []IndexedTorrent{}
|
||||
indexedTorrents := []schema.IndexedTorrent{}
|
||||
for _, link := range links {
|
||||
go func(link string) {
|
||||
torrents, err := getTorrentsTorrentDosFilmes(ctx, i, link)
|
||||
@@ -106,16 +108,21 @@ func (i *Indexer) HandlerTorrentDosFilmesIndexer(w http.ResponseWriter, r *http.
|
||||
|
||||
// remove the ones with zero similarity
|
||||
if len(indexedTorrents) > 20 && r.URL.Query().Get("filter_results") != "" && r.URL.Query().Get("q") != "" {
|
||||
indexedTorrents = utils.Filter(indexedTorrents, func(it IndexedTorrent) bool {
|
||||
indexedTorrents = utils.Filter(indexedTorrents, func(it schema.IndexedTorrent) bool {
|
||||
return it.Similarity > 0
|
||||
})
|
||||
}
|
||||
|
||||
// sort by similarity
|
||||
slices.SortFunc(indexedTorrents, func(i, j IndexedTorrent) int {
|
||||
slices.SortFunc(indexedTorrents, func(i, j schema.IndexedTorrent) int {
|
||||
return int((j.Similarity - i.Similarity) * 1000)
|
||||
})
|
||||
|
||||
// send to search index
|
||||
go func() {
|
||||
_ = i.search.IndexTorrents(indexedTorrents)
|
||||
}()
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
err = json.NewEncoder(w).Encode(Response{
|
||||
Results: indexedTorrents,
|
||||
@@ -126,8 +133,8 @@ func (i *Indexer) HandlerTorrentDosFilmesIndexer(w http.ResponseWriter, r *http.
|
||||
}
|
||||
}
|
||||
|
||||
func getTorrentsTorrentDosFilmes(ctx context.Context, i *Indexer, link string) ([]IndexedTorrent, error) {
|
||||
var indexedTorrents []IndexedTorrent
|
||||
func getTorrentsTorrentDosFilmes(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
|
||||
@@ -178,16 +185,15 @@ func getTorrentsTorrentDosFilmes(ctx context.Context, i *Indexer, link string) (
|
||||
imdbLink := ""
|
||||
article.Find("div.content a").Each(func(i int, s *goquery.Selection) {
|
||||
link, _ := s.Attr("href")
|
||||
re := regexp.MustCompile(`https://www.imdb.com/title/(tt\d+)`)
|
||||
matches := re.FindStringSubmatch(link)
|
||||
if len(matches) > 0 {
|
||||
imdbLink = matches[0]
|
||||
_imdbLink, err := getIMDBLink(link)
|
||||
if err == nil {
|
||||
imdbLink = _imdbLink
|
||||
}
|
||||
})
|
||||
|
||||
size = stableUniq(size)
|
||||
|
||||
var chanIndexedTorrent = make(chan IndexedTorrent)
|
||||
var chanIndexedTorrent = make(chan schema.IndexedTorrent)
|
||||
|
||||
// for each magnet link, create a new indexed torrent
|
||||
for it, magnetLink := range magnetLinks {
|
||||
@@ -227,7 +233,7 @@ func getTorrentsTorrentDosFilmes(ctx context.Context, i *Indexer, link string) (
|
||||
mySize = size[it]
|
||||
}
|
||||
|
||||
ixt := IndexedTorrent{
|
||||
ixt := schema.IndexedTorrent{
|
||||
Title: appendAudioISO639_2Code(releaseTitle, magnetAudio),
|
||||
OriginalTitle: title,
|
||||
Details: link,
|
||||
|
||||
10
cache/redis.go
vendored
10
cache/redis.go
vendored
@@ -9,13 +9,14 @@ import (
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var (
|
||||
const (
|
||||
DefaultExpiration = 24 * time.Hour * 7 // 7 days
|
||||
IndexerComandoTorrents = "indexer:comando_torrents"
|
||||
)
|
||||
|
||||
type Redis struct {
|
||||
client *redis.Client
|
||||
defaultExpiration time.Duration
|
||||
}
|
||||
|
||||
func NewRedis() *Redis {
|
||||
@@ -28,15 +29,20 @@ func NewRedis() *Redis {
|
||||
Addr: fmt.Sprintf("%s:6379", redisHost),
|
||||
Password: "",
|
||||
}),
|
||||
defaultExpiration: DefaultExpiration,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Redis) SetDefaultExpiration(expiration time.Duration) {
|
||||
r.defaultExpiration = expiration
|
||||
}
|
||||
|
||||
func (r *Redis) Get(ctx context.Context, key string) ([]byte, error) {
|
||||
return r.client.Get(ctx, key).Bytes()
|
||||
}
|
||||
|
||||
func (r *Redis) Set(ctx context.Context, key string, value []byte) error {
|
||||
return r.client.Set(ctx, key, value, DefaultExpiration).Err()
|
||||
return r.client.Set(ctx, key, value, r.defaultExpiration).Err()
|
||||
}
|
||||
|
||||
func (r *Redis) SetWithExpiration(ctx context.Context, key string, value []byte, expiration time.Duration) error {
|
||||
|
||||
@@ -11,6 +11,8 @@ services:
|
||||
- indexer
|
||||
environment:
|
||||
- REDIS_HOST=redis
|
||||
- MEILISEARCH_ADDRESS=http://meilisearch:7700
|
||||
- MEILISEARCH_KEY=my-secret-key
|
||||
- FLARESOLVERR_ADDRESS=http://flaresolverr:8191
|
||||
|
||||
redis:
|
||||
@@ -20,5 +22,17 @@ services:
|
||||
networks:
|
||||
- indexer
|
||||
|
||||
# This container is not necessary for the indexer to work,
|
||||
# deploy if you want to use the search feature
|
||||
meilisearch:
|
||||
image: getmeili/meilisearch:latest
|
||||
container_name: meilisearch
|
||||
restart: unless-stopped
|
||||
networks:
|
||||
- indexer
|
||||
environment:
|
||||
- MEILI_NO_ANALYTICS=true
|
||||
- MEILI_MASTER_KEY=my-secret-key
|
||||
|
||||
networks:
|
||||
indexer:
|
||||
|
||||
1
go.mod
1
go.mod
@@ -21,4 +21,5 @@ require (
|
||||
github.com/PuerkitoBio/goquery v1.9.1
|
||||
github.com/hbollon/go-edlib v1.6.0
|
||||
github.com/prometheus/client_golang v1.19.0
|
||||
github.com/xhit/go-str2duration/v2 v2.1.0
|
||||
)
|
||||
|
||||
2
go.sum
2
go.sum
@@ -28,6 +28,8 @@ github.com/prometheus/procfs v0.13.0 h1:GqzLlQyfsPbaEHaQkO7tbDlriv/4o5Hudv6OXHGK
|
||||
github.com/prometheus/procfs v0.13.0/go.mod h1:cd4PFCR54QLnGKPaKGA6l+cfuNXtht43ZKY6tow0Y1g=
|
||||
github.com/redis/go-redis/v9 v9.5.1 h1:H1X4D3yHPaYrkL5X06Wh6xNVM/pX0Ft4RV0vMGvLBh8=
|
||||
github.com/redis/go-redis/v9 v9.5.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
|
||||
github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc=
|
||||
github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
|
||||
35
main.go
35
main.go
@@ -8,18 +8,39 @@ import (
|
||||
handler "github.com/felipemarinho97/torrent-indexer/api"
|
||||
"github.com/felipemarinho97/torrent-indexer/cache"
|
||||
"github.com/felipemarinho97/torrent-indexer/monitoring"
|
||||
"github.com/felipemarinho97/torrent-indexer/public"
|
||||
"github.com/felipemarinho97/torrent-indexer/requester"
|
||||
meilisearch "github.com/felipemarinho97/torrent-indexer/search"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
|
||||
str2duration "github.com/xhit/go-str2duration/v2"
|
||||
)
|
||||
|
||||
func main() {
|
||||
redis := cache.NewRedis()
|
||||
searchIndex := meilisearch.NewSearchIndexer(os.Getenv("MEILISEARCH_ADDRESS"), os.Getenv("MEILISEARCH_KEY"), "torrents")
|
||||
metrics := monitoring.NewMetrics()
|
||||
metrics.Register()
|
||||
|
||||
flaresolverr := requester.NewFlareSolverr(os.Getenv("FLARESOLVERR_ADDRESS"), 60000)
|
||||
req := requester.NewRequester(flaresolverr, redis)
|
||||
indexers := handler.NewIndexers(redis, metrics, req)
|
||||
|
||||
// get shot-lived and long-lived cache expiration from env
|
||||
shortLivedCacheExpiration, err := str2duration.ParseDuration(os.Getenv("SHORT_LIVED_CACHE_EXPIRATION"))
|
||||
if err == nil {
|
||||
fmt.Printf("Setting short-lived cache expiration to %s\n", shortLivedCacheExpiration)
|
||||
req.SetShortLivedCacheExpiration(shortLivedCacheExpiration)
|
||||
}
|
||||
longLivedCacheExpiration, err := str2duration.ParseDuration(os.Getenv("LONG_LIVED_CACHE_EXPIRATION"))
|
||||
if err == nil {
|
||||
fmt.Printf("Setting long-lived cache expiration to %s\n", longLivedCacheExpiration)
|
||||
redis.SetDefaultExpiration(longLivedCacheExpiration)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
indexers := handler.NewIndexers(redis, metrics, req, searchIndex)
|
||||
search := handler.NewMeilisearchHandler(searchIndex)
|
||||
|
||||
indexerMux := http.NewServeMux()
|
||||
metricsMux := http.NewServeMux()
|
||||
@@ -29,6 +50,8 @@ func main() {
|
||||
indexerMux.HandleFunc("/indexers/torrent-dos-filmes", indexers.HandlerTorrentDosFilmesIndexer)
|
||||
indexerMux.HandleFunc("/indexers/bludv", indexers.HandlerBluDVIndexer)
|
||||
indexerMux.HandleFunc("/indexers/manual", indexers.HandlerManualIndexer)
|
||||
indexerMux.HandleFunc("/search", search.SearchTorrentHandler)
|
||||
indexerMux.Handle("/ui/", http.StripPrefix("/ui/", http.FileServer(http.FS(public.UIFiles))))
|
||||
|
||||
metricsMux.Handle("/metrics", promhttp.Handler())
|
||||
|
||||
@@ -38,8 +61,14 @@ func main() {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
fmt.Println("Server listening on :7006")
|
||||
err := http.ListenAndServe(":7006", indexerMux)
|
||||
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
port = "7006"
|
||||
}
|
||||
|
||||
fmt.Printf("Server listening on :%s\n", port)
|
||||
err = http.ListenAndServe(":"+port, indexerMux)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
6
public/index.go
Normal file
6
public/index.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package public
|
||||
|
||||
import "embed"
|
||||
|
||||
//go:embed *
|
||||
var UIFiles embed.FS
|
||||
127
public/index.html
Normal file
127
public/index.html
Normal file
@@ -0,0 +1,127 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Torrent Indexer</title>
|
||||
<script src="https://cdn.tailwindcss.com"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/@heroicons/react/solid@2.0.0/dist/index.umd.js"></script>
|
||||
</head>
|
||||
|
||||
<body class="bg-gray-900 text-white font-sans">
|
||||
<div class="container mx-auto p-6">
|
||||
<!-- Header -->
|
||||
<header class="text-center mb-10">
|
||||
<h1 class="text-4xl font-bold text-blue-400">Torrent Indexer 🇧🇷</h1>
|
||||
<p class="text-gray-400 mt-2">Find torrents with detailed information from torrent-indexer cache</p>
|
||||
</header>
|
||||
|
||||
<!-- Search Bar -->
|
||||
<div class="flex justify-center mb-10">
|
||||
<input id="search-query" type="text" placeholder="Enter search query"
|
||||
class="w-full max-w-lg px-4 py-2 rounded-md border border-gray-600 bg-gray-800 text-white focus:ring focus:ring-blue-500">
|
||||
<button id="search-btn"
|
||||
class="ml-4 px-6 py-2 bg-blue-600 hover:bg-blue-700 rounded-md font-bold text-white">Search</button>
|
||||
</div>
|
||||
|
||||
<!-- Results Section -->
|
||||
<div id="results" class="space-y-6">
|
||||
<!-- Dynamic content will be injected here -->
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// Function to render a single torrent result
|
||||
function renderTorrent(torrent) {
|
||||
return `
|
||||
<div class="p-6 bg-gray-800 rounded-lg shadow-md flex flex-col md:flex-row gap-6">
|
||||
<!-- Torrent Title and Details -->
|
||||
<div class="flex-grow">
|
||||
<h2 class="text-2xl font-bold text-blue-400 flex items-center gap-2">
|
||||
<span>${torrent.title}</span>
|
||||
<span class="text-sm text-gray-400">(${torrent.year})</span>
|
||||
</h2>
|
||||
<p class="text-gray-500 italic mt-1">${torrent.original_title}</p>
|
||||
<div class="mt-4 grid grid-cols-1 sm:grid-cols-2 gap-4">
|
||||
<p><strong>Audio:</strong> ${torrent.audio.join(', ')}</p>
|
||||
<p><strong>Size:</strong> ${torrent.size}</p>
|
||||
<p><strong>Seeds:</strong> ${torrent.seed_count} | <strong>Leeches:</strong> ${torrent.leech_count}</p>
|
||||
<p><strong>Info Hash:</strong> <span class="text-sm break-all text-gray-300">${torrent.info_hash}</span></p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Actions -->
|
||||
<div class="flex flex-col justify-between items-start md:items-end">
|
||||
<div>
|
||||
<a href="${torrent.imdb}" target="_blank"
|
||||
class="flex items-center gap-2 text-blue-500 hover:text-blue-400 font-medium">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
||||
d="M8 16l-4-4m0 0l4-4m-4 4h16" />
|
||||
</svg>
|
||||
View on IMDB
|
||||
</a>
|
||||
<a href="${torrent.details}" target="_blank"
|
||||
class="flex items-center gap-2 text-blue-500 hover:text-blue-400 font-medium mt-2">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
||||
d="M13 16h-1v-4h-.01M9 20h6a2 2 0 002-2v-5a2 2 0 00-2-2h-3.5a2 2 0 00-1.85 1.19M13 10V6a3 3 0 00-6 0v4" />
|
||||
</svg>
|
||||
View Details
|
||||
</a>
|
||||
</div>
|
||||
<a href="${torrent.magnet_link}" target="_blank"
|
||||
class="px-4 py-2 bg-green-600 hover:bg-green-700 text-white font-bold rounded-md flex items-center gap-2 mt-4">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
||||
d="M9 17v-6m6 6v-6m-6 6l-2-2m8 0l2-2M5 9l7-7 7 7" />
|
||||
</svg>
|
||||
Download Magnet
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
// Handle search
|
||||
async function onSearch() {
|
||||
const query = document.getElementById('search-query').value.trim();
|
||||
if (!query) {
|
||||
alert('Please enter a search query!');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`/search?q=${encodeURIComponent(query)}`);
|
||||
if (!response.ok) {
|
||||
throw new Error('Search failed');
|
||||
}
|
||||
|
||||
const results = await response.json();
|
||||
const resultsContainer = document.getElementById('results');
|
||||
resultsContainer.innerHTML = results.map(renderTorrent).join('');
|
||||
} catch (error) {
|
||||
// add error element
|
||||
document.getElementById('results').innerHTML = `
|
||||
<div class="p-6 bg-red-800 rounded-lg shadow-md text-center">
|
||||
<p class="text-xl font-bold text-red-400">Error fetching search results</p>
|
||||
<p class="text-gray-400 mt-2">Please try again later.</p>
|
||||
</div>
|
||||
`;
|
||||
//alert('Error fetching search results. Please try again.');
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
document.getElementById('search-btn').addEventListener('click', onSearch);
|
||||
// on enter press
|
||||
document.getElementById('search-query').addEventListener('keypress', (e) => {
|
||||
if (e.key === 'Enter') {
|
||||
onSearch();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -10,6 +10,8 @@ import (
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/felipemarinho97/torrent-indexer/utils"
|
||||
)
|
||||
|
||||
type FlareSolverr struct {
|
||||
@@ -243,8 +245,15 @@ func (f *FlareSolverr) Get(_url string) (io.ReadCloser, error) {
|
||||
return nil, fmt.Errorf("under attack")
|
||||
}
|
||||
|
||||
// check if the response is valid HTML
|
||||
if !utils.IsValidHTML(response.Solution.Response) {
|
||||
fmt.Printf("[FlareSolverr] Invalid HTML response from %s\n", _url)
|
||||
response.Solution.Response = ""
|
||||
}
|
||||
|
||||
// If the response body is empty but cookies are present, make a new request
|
||||
if response.Solution.Response == "" && len(response.Solution.Cookies) > 0 {
|
||||
fmt.Printf("[FlareSolverr] Making a new request to %s with cookies\n", _url)
|
||||
// Create a new request with cookies
|
||||
client := &http.Client{}
|
||||
cookieJar, err := cookiejar.New(&cookiejar.Options{})
|
||||
@@ -268,13 +277,22 @@ func (f *FlareSolverr) Get(_url string) (io.ReadCloser, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// use the same user returned by the FlareSolverr
|
||||
secondReq.Header.Set("User-Agent", response.Solution.UserAgent)
|
||||
|
||||
secondResp, err := client.Do(secondReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
respByte := new(bytes.Buffer)
|
||||
_, err = respByte.ReadFrom(secondResp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Return the body of the second request
|
||||
return secondResp.Body, nil
|
||||
return io.NopCloser(bytes.NewReader(respByte.Bytes())), nil
|
||||
}
|
||||
|
||||
// Return the original response body
|
||||
|
||||
@@ -13,7 +13,6 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
shortLivedCacheExpiration = 30 * time.Minute
|
||||
cacheKey = "shortLivedCache"
|
||||
)
|
||||
|
||||
@@ -23,10 +22,15 @@ type Requster struct {
|
||||
fs *FlareSolverr
|
||||
c *cache.Redis
|
||||
httpClient *http.Client
|
||||
shortLivedCacheExpiration time.Duration
|
||||
}
|
||||
|
||||
func NewRequester(fs *FlareSolverr, c *cache.Redis) *Requster {
|
||||
return &Requster{fs: fs, httpClient: &http.Client{}, c: c}
|
||||
return &Requster{fs: fs, httpClient: &http.Client{}, c: c, shortLivedCacheExpiration: 30 * time.Minute}
|
||||
}
|
||||
|
||||
func (i *Requster) SetShortLivedCacheExpiration(expiration time.Duration) {
|
||||
i.shortLivedCacheExpiration = expiration
|
||||
}
|
||||
|
||||
func (i *Requster) GetDocument(ctx context.Context, url string) (io.ReadCloser, error) {
|
||||
@@ -75,7 +79,7 @@ func (i *Requster) GetDocument(ctx context.Context, url string) (io.ReadCloser,
|
||||
|
||||
// save response to cache if it's not a challange and body is not empty
|
||||
if !hasChallange(bodyByte) && len(bodyByte) > 0 {
|
||||
err = i.c.SetWithExpiration(ctx, key, bodyByte, shortLivedCacheExpiration)
|
||||
err = i.c.SetWithExpiration(ctx, key, bodyByte, i.shortLivedCacheExpiration)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to save response to cache: %v\n", err)
|
||||
}
|
||||
|
||||
@@ -33,6 +33,10 @@ const (
|
||||
AudioThai2 = "Tailandes"
|
||||
AudioTurkish = "Turco"
|
||||
AudioHindi = "Hindi"
|
||||
AudioFarsi = "Persa"
|
||||
AudioMalay = "Malaio"
|
||||
AudioDutch = "Holandês"
|
||||
AudioDutch2 = "Holandes"
|
||||
)
|
||||
|
||||
var AudioList = []Audio{
|
||||
@@ -64,6 +68,10 @@ var AudioList = []Audio{
|
||||
AudioThai2,
|
||||
AudioTurkish,
|
||||
AudioHindi,
|
||||
AudioFarsi,
|
||||
AudioMalay,
|
||||
AudioDutch,
|
||||
AudioDutch2,
|
||||
}
|
||||
|
||||
func (a Audio) String() string {
|
||||
@@ -137,6 +145,14 @@ func (a Audio) toTag() string {
|
||||
return "tur"
|
||||
case AudioHindi:
|
||||
return "hin"
|
||||
case AudioFarsi:
|
||||
return "fas"
|
||||
case AudioMalay:
|
||||
return "msa"
|
||||
case AudioDutch:
|
||||
return "nld"
|
||||
case AudioDutch2:
|
||||
return "nld"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
|
||||
20
schema/indexed_torrent.go
Normal file
20
schema/indexed_torrent.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package schema
|
||||
|
||||
import "time"
|
||||
|
||||
type IndexedTorrent struct {
|
||||
Title string `json:"title"`
|
||||
OriginalTitle string `json:"original_title"`
|
||||
Details string `json:"details"`
|
||||
Year string `json:"year"`
|
||||
IMDB string `json:"imdb"`
|
||||
Audio []Audio `json:"audio"`
|
||||
MagnetLink string `json:"magnet_link"`
|
||||
Date time.Time `json:"date"`
|
||||
InfoHash string `json:"info_hash"`
|
||||
Trackers []string `json:"trackers"`
|
||||
Size string `json:"size"`
|
||||
LeechCount int `json:"leech_count"`
|
||||
SeedCount int `json:"seed_count"`
|
||||
Similarity float32 `json:"similarity"`
|
||||
}
|
||||
147
search/meilisearch.go
Normal file
147
search/meilisearch.go
Normal file
@@ -0,0 +1,147 @@
|
||||
package meilisearch
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/felipemarinho97/torrent-indexer/schema"
|
||||
)
|
||||
|
||||
// SearchIndexer integrates with Meilisearch to index and search torrent items.
|
||||
type SearchIndexer struct {
|
||||
Client *http.Client
|
||||
BaseURL string
|
||||
APIKey string
|
||||
IndexName string
|
||||
}
|
||||
|
||||
// NewSearchIndexer creates a new instance of SearchIndexer.
|
||||
func NewSearchIndexer(baseURL, apiKey, indexName string) *SearchIndexer {
|
||||
return &SearchIndexer{
|
||||
Client: &http.Client{Timeout: 10 * time.Second},
|
||||
BaseURL: baseURL,
|
||||
APIKey: apiKey,
|
||||
IndexName: indexName,
|
||||
}
|
||||
}
|
||||
|
||||
// IndexTorrent indexes a single torrent item in Meilisearch.
|
||||
func (t *SearchIndexer) IndexTorrent(torrent schema.IndexedTorrent) error {
|
||||
url := fmt.Sprintf("%s/indexes/%s/documents", t.BaseURL, t.IndexName)
|
||||
|
||||
torrentWithKey := struct {
|
||||
Hash string `json:"id"`
|
||||
schema.IndexedTorrent
|
||||
}{
|
||||
Hash: torrent.InfoHash,
|
||||
IndexedTorrent: torrent,
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(torrentWithKey)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal torrent data: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if t.APIKey != "" {
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", t.APIKey))
|
||||
}
|
||||
|
||||
resp, err := t.Client.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to execute request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *SearchIndexer) IndexTorrents(torrents []schema.IndexedTorrent) error {
|
||||
url := fmt.Sprintf("%s/indexes/%s/documents", t.BaseURL, t.IndexName)
|
||||
|
||||
torrentsWithKey := make([]struct {
|
||||
Hash string `json:"id"`
|
||||
schema.IndexedTorrent
|
||||
}, 0, len(torrents))
|
||||
for _, torrent := range torrents {
|
||||
torrentWithKey := struct {
|
||||
Hash string `json:"id"`
|
||||
schema.IndexedTorrent
|
||||
}{
|
||||
Hash: torrent.InfoHash,
|
||||
IndexedTorrent: torrent,
|
||||
}
|
||||
torrentsWithKey = append(torrentsWithKey, torrentWithKey)
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(torrentsWithKey)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal torrent data: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if t.APIKey != "" {
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", t.APIKey))
|
||||
}
|
||||
|
||||
resp, err := t.Client.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to execute request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SearchTorrent searches indexed torrents in Meilisearch based on the query.
|
||||
func (t *SearchIndexer) SearchTorrent(query string, limit int) ([]schema.IndexedTorrent, error) {
|
||||
url := fmt.Sprintf("%s/indexes/%s/search", t.BaseURL, t.IndexName)
|
||||
requestBody := map[string]string{
|
||||
"q": query,
|
||||
}
|
||||
jsonData, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal search query: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if t.APIKey != "" {
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", t.APIKey))
|
||||
}
|
||||
|
||||
resp, err := t.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
return nil, fmt.Errorf("search failed: %s", body)
|
||||
}
|
||||
|
||||
var result struct {
|
||||
Hits []schema.IndexedTorrent `json:"hits"`
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse search response: %w", err)
|
||||
}
|
||||
|
||||
return result.Hits, nil
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package utils
|
||||
|
||||
import "regexp"
|
||||
|
||||
func Filter[A any](arr []A, f func(A) bool) []A {
|
||||
var res []A
|
||||
res = make([]A, 0)
|
||||
@@ -10,3 +12,21 @@ func Filter[A any](arr []A, f func(A) bool) []A {
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func IsValidHTML(input string) bool {
|
||||
// Check for <!DOCTYPE> declaration (case-insensitive)
|
||||
doctypeRegex := regexp.MustCompile(`(?i)<!DOCTYPE\s+html>`)
|
||||
if !doctypeRegex.MatchString(input) {
|
||||
return false
|
||||
}
|
||||
|
||||
// Check for <html> and </html> tags (case-insensitive)
|
||||
htmlTagRegex := regexp.MustCompile(`(?i)<html[\s\S]*?>[\s\S]*?</html>`)
|
||||
if !htmlTagRegex.MatchString(input) {
|
||||
return false
|
||||
}
|
||||
|
||||
// Check for <body> and </body> tags (case-insensitive)
|
||||
bodyTagRegex := regexp.MustCompile(`(?i)<body[\s\S]*?>[\s\S]*?</body>`)
|
||||
return bodyTagRegex.MatchString(input)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user