new: feat: add comando torrents indexer
This commit is contained in:
164
api/indexers/comando_torrents.go
Normal file
164
api/indexers/comando_torrents.go
Normal file
@@ -0,0 +1,164 @@
|
||||
package indexers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
)
|
||||
|
||||
const (
|
||||
URL = "https://comando.la/"
|
||||
queryFilter = "?s="
|
||||
)
|
||||
|
||||
type Audio string
|
||||
|
||||
const (
|
||||
AudioPortuguese = "Português"
|
||||
AudioEnglish = "Inglês"
|
||||
AudioSpanish = "Espanhol"
|
||||
)
|
||||
|
||||
type IndexedTorrent struct {
|
||||
Title string `json:"title"`
|
||||
OriginalTitle string `json:"original_title"`
|
||||
Details string `json:"details"`
|
||||
Year string `json:"year"`
|
||||
Audio []Audio `json:"audio"`
|
||||
MagnetLink string `json:"magnet_link"`
|
||||
}
|
||||
|
||||
func HandlerComandoIndexer(w http.ResponseWriter, r *http.Request) {
|
||||
// supported query params: q, season, episode
|
||||
q := r.URL.Query().Get("q")
|
||||
if q == "" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": "param q is required"})
|
||||
return
|
||||
}
|
||||
|
||||
// URL encode query param
|
||||
q = url.QueryEscape(q)
|
||||
url := URL + queryFilter + q
|
||||
|
||||
fmt.Println("URL:>", url)
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
doc, err := goquery.NewDocumentFromReader(resp.Body)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
var links []string
|
||||
doc.Find("article").Each(func(i int, s *goquery.Selection) {
|
||||
// get link from h2.entry-title > a
|
||||
link, _ := s.Find("h2.entry-title > a").Attr("href")
|
||||
links = append(links, link)
|
||||
})
|
||||
fmt.Println(links)
|
||||
fmt.Println(doc.Text())
|
||||
|
||||
var indexedTorrents []IndexedTorrent
|
||||
for _, link := range links {
|
||||
torrents, err := getTorrents(link)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
indexedTorrents = append(indexedTorrents, torrents...)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(indexedTorrents)
|
||||
}
|
||||
|
||||
func getTorrents(link string) ([]IndexedTorrent, error) {
|
||||
var indexedTorrents []IndexedTorrent
|
||||
resp, err := http.Get(link)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
doc, err := goquery.NewDocumentFromReader(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
article := doc.Find("article")
|
||||
title := article.Find("h2.entry-title > a").Text()
|
||||
textContent := article.Find("div.entry-content")
|
||||
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 []Audio
|
||||
var year string
|
||||
article.Find("div.entry-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
|
||||
// Legenda: Português
|
||||
// Tamanho: –
|
||||
// Qualidade de Áudio: 10
|
||||
// Qualidade de Vídeo: 10
|
||||
// Duração: 59 Min.
|
||||
// Servidor: Torrent
|
||||
|
||||
re := regexp.MustCompile(`Áudio: (.*)`)
|
||||
audioMatch := re.FindStringSubmatch(s.Text())
|
||||
if len(audioMatch) > 0 {
|
||||
audio = append(audio, Audio(audioMatch[1]))
|
||||
}
|
||||
|
||||
re = regexp.MustCompile(`Ano de Lançamento: (.*)`)
|
||||
yearMatch := re.FindStringSubmatch(s.Text())
|
||||
if len(yearMatch) > 0 {
|
||||
year = yearMatch[1]
|
||||
}
|
||||
})
|
||||
|
||||
// for each magnet link, create a new indexed torrent
|
||||
for _, magnetLink := range magnetLinks {
|
||||
indexedTorrents = append(indexedTorrents, IndexedTorrent{
|
||||
Title: extractReleaseName(magnetLink),
|
||||
OriginalTitle: title,
|
||||
Details: link,
|
||||
Year: year,
|
||||
Audio: audio,
|
||||
MagnetLink: magnetLink,
|
||||
})
|
||||
}
|
||||
|
||||
return indexedTorrents, nil
|
||||
}
|
||||
|
||||
func extractReleaseName(magnetLink string) string {
|
||||
re := regexp.MustCompile(`dn=(.*)&`)
|
||||
matches := re.FindStringSubmatch(magnetLink)
|
||||
if len(matches) > 0 {
|
||||
return matches[1]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
10
go.mod
10
go.mod
@@ -4,4 +4,12 @@ go 1.17
|
||||
|
||||
require github.com/sirupsen/logrus v1.9.0
|
||||
|
||||
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
|
||||
require (
|
||||
github.com/andybalholm/cascadia v1.3.1 // indirect
|
||||
golang.org/x/net v0.7.0 // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/PuerkitoBio/goquery v1.8.1
|
||||
golang.org/x/sys v0.5.0 // indirect
|
||||
)
|
||||
|
||||
37
go.sum
37
go.sum
@@ -1,3 +1,7 @@
|
||||
github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAcwmWM=
|
||||
github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ=
|
||||
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
|
||||
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
@@ -8,8 +12,39 @@ github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVs
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
|
||||
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=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
|
||||
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
2
main.go
2
main.go
@@ -4,12 +4,14 @@ import (
|
||||
"net/http"
|
||||
|
||||
handler "github.com/felipemarinho97/vercel-lambdas/api"
|
||||
"github.com/felipemarinho97/vercel-lambdas/api/indexers"
|
||||
"github.com/felipemarinho97/vercel-lambdas/api/statusinvest"
|
||||
)
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", handler.HandlerIndex)
|
||||
http.HandleFunc("/statusinvest/companies", statusinvest.HandlerListCompanies)
|
||||
http.HandleFunc("/indexers/comando_torrents", indexers.HandlerComandoIndexer)
|
||||
|
||||
err := http.ListenAndServe(":7006", nil)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user