Skip to content

Commit

Permalink
Feat/manual indexer (#9)
Browse files Browse the repository at this point in the history
* new: feat: add manual torrents support

* chg: docs: update documentation

* chg: chore: fix lint ci

---------

Co-authored-by: Felipe Marinho <felipe.marinho@ifood.com.br>
  • Loading branch information
felipemarinho97 and felipemarinho97 authored Jun 18, 2024
1 parent 2812c20 commit ced533c
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 15 deletions.
7 changes: 4 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
run:
skip-files:
- scrape.go
issues:
exclude-files:
- scrape.go
- infohash.go
39 changes: 27 additions & 12 deletions api/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,35 @@ func HandlerIndex(w http.ResponseWriter, r *http.Request) {
err := json.NewEncoder(w).Encode(map[string]interface{}{
"time": currentTime,
"endpoints": map[string]interface{}{
"/indexers/comando_torrents": map[string]interface{}{
"method": "GET",
"description": "Indexer for comando torrents",
"query_params": map[string]string{
"q": "search query",
"filter_results": "if results with similarity equals to zero should be filtered (true/false)",
"/indexers/comando_torrents": []map[string]interface{}{
{
"method": "GET",
"description": "Indexer for comando torrents",
"query_params": map[string]string{
"q": "search query",
"filter_results": "if results with similarity equals to zero should be filtered (true/false)",
},
},
},
"/indexers/bludv": map[string]interface{}{
"method": "GET",
"description": "Indexer for bludv",
"query_params": map[string]string{
"q": "search query",
"filter_results": "if results with similarity equals to zero should be filtered (true/false)",
"/indexers/bludv": []map[string]interface{}{
{
"method": "GET",
"description": "Indexer for bludv",
"query_params": map[string]string{
"q": "search query",
"filter_results": "if results with similarity equals to zero should be filtered (true/false)",
}},
},
"/indexers/manual": []map[string]interface{}{
{
"method": "POST",
"description": "Add a manual torrent entry to the indexer for 12 hours",
"body": map[string]interface{}{
"magnetLink": "magnet link",
}},
{
"method": "GET",
"description": "Get all manual torrents",
},
},
},
Expand Down
138 changes: 138 additions & 0 deletions api/manual.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package handler

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"time"

"github.com/felipemarinho97/torrent-indexer/magnet"
"github.com/felipemarinho97/torrent-indexer/schema"
goscrape "github.com/felipemarinho97/torrent-indexer/scrape"
"github.com/redis/go-redis/v9"
)

const manualTorrentsRedisKey = "manual:torrents"

var manualTorrentExpiration = 8 * time.Hour

type ManualIndexerRequest struct {
MagnetLink string `json:"magnetLink"`
}

func (i *Indexer) HandlerManualIndexer(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var req ManualIndexerRequest
indexedTorrents := []IndexedTorrent{}

// fetch from redis
out, err := i.redis.Get(ctx, manualTorrentsRedisKey)
if err != nil && !errors.Is(err, redis.Nil) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Println(err)
err = json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
if err != nil {
fmt.Println(err)
}
i.metrics.IndexerErrors.WithLabelValues("manual").Inc()
return
} else if errors.Is(err, redis.Nil) {
out = bytes.NewBufferString("[]").Bytes()
}

err = json.Unmarshal([]byte(out), &indexedTorrents)
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("manual").Inc()
return
}

// check if the request is a POST
if r.Method == http.MethodPost {
// decode the request body
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
err = json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
if err != nil {
fmt.Println(err)
}
i.metrics.IndexerErrors.WithLabelValues("manual").Inc()
return
}

magnet, err := magnet.ParseMagnetUri(req.MagnetLink)
if err != nil {
fmt.Println(err)
}
var audio []schema.Audio
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...)
}

peer, seed, err := goscrape.GetLeechsAndSeeds(ctx, i.redis, i.metrics, infoHash, trackers)
if err != nil {
fmt.Println(err)
}

title := processTitle(releaseTitle, magnetAudio)

ixt := IndexedTorrent{
Title: appendAudioISO639_2Code(releaseTitle, magnetAudio),
OriginalTitle: title,
Audio: magnetAudio,
MagnetLink: req.MagnetLink,
InfoHash: infoHash,
Trackers: trackers,
LeechCount: peer,
SeedCount: seed,
}

// write to redis
indexedTorrents = append(indexedTorrents, ixt)
out, err := json.Marshal(indexedTorrents)
if err != nil {
fmt.Println(err)
}

err = i.redis.SetWithExpiration(ctx, manualTorrentsRedisKey, out, manualTorrentExpiration)
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("manual").Inc()
return
}
}

w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(Response{
Results: indexedTorrents,
Count: len(indexedTorrents),
})
if err != nil {
fmt.Println(err)
}
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func main() {
indexerMux.HandleFunc("/", handler.HandlerIndex)
indexerMux.HandleFunc("/indexers/comando_torrents", indexers.HandlerComandoIndexer)
indexerMux.HandleFunc("/indexers/bludv", indexers.HandlerBluDVIndexer)
indexerMux.HandleFunc("/indexers/manual", indexers.HandlerManualIndexer)

metricsMux.Handle("/metrics", promhttp.Handler())

Expand Down

0 comments on commit ced533c

Please sign in to comment.