forked from tnychn/torrodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetx.go
181 lines (166 loc) · 4.97 KB
/
leetx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package leetx
import (
"context"
"fmt"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/avast/retry-go"
"github.com/dustin/go-humanize"
"github.com/sirupsen/logrus"
"golang.org/x/net/html"
"golang.org/x/time/rate"
"github.com/stl3/torgo/models"
"github.com/stl3/torgo/request"
)
const (
Name = "1337x"
Site = "https://1337x.to"
// Site = "https://1377x.to"
// Site = "https://1337xto.to/"
// Site = "https://www.1337xx.to"
)
type provider struct {
models.Provider
*rate.Limiter
}
func New() models.ProviderInterface {
provider := &provider{}
provider.Name = Name
provider.Site = Site
provider.Categories = models.Categories{
All: "/search/%v/%d/",
Movie: "/category-search/%v/Movies/%d/",
TV: "/category-search/%v/TV/%d/",
Anime: "/category-search/%v/Anime/%d/",
Porn: "/category-search/%v/XXX/%d/",
Documentaries: "/category-search/%v/Documentaries/%d/",
}
provider.Limiter = rate.NewLimiter(rate.Every(time.Second), 1)
return provider
}
func (provider *provider) Search(query string, count int, categoryURL models.CategoryURL) ([]models.Source, error) {
perPage := 40
if categoryURL == provider.Categories.All {
perPage = 20
}
results, err := provider.Query(query, categoryURL, count, perPage, 0, provider.extractor)
return results, err
}
func (provider *provider) extractor(surl string, page int, results *[]models.Source, wg *sync.WaitGroup) {
logrus.Infof("1337x: [%d] Extracting results...\n", page)
var html string
err := retry.Do(func() (err error) {
_ = provider.Wait(context.Background())
_, html, err = request.Get(nil, surl, nil)
return
},
retry.RetryIf(func(err error) bool {
// return err.Error() == http.StatusText(503)
return err.Error() == http.StatusText(http.StatusServiceUnavailable)
}),
retry.Attempts(3),
)
if err != nil {
logrus.Errorln(fmt.Sprintf("1337x: [%d]", page), err)
wg.Done()
return
}
var sources []models.Source // Temporary array for storing models.Source(s) but without magnet and torrent links
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(html))
table := doc.Find("table.table-list.table.table-responsive.table-striped")
table.Find("tr").Each(func(i int, tr *goquery.Selection) {
// title
title := tr.Find("td.coll-1.name").Text()
if containsHTMLEncodedEntities(title) {
decodedTitle, err := decodeHTMLText(title)
if err != nil {
logrus.Errorln("Error decoding HTML text:", err)
// return
decodedTitle = title
}
logrus.Infof("Decoded Title: %s", decodedTitle)
} else {
logrus.Infof("Title: %s", title)
}
// seeders
s := tr.Find("td.coll-2.seeds").Text()
seeders, _ := strconv.Atoi(strings.TrimSpace(s))
// leechers
l := tr.Find("td.coll-3.leeches").Text()
leechers, _ := strconv.Atoi(strings.TrimSpace(l))
// filesize
tr.Find("td.coll-4.size").Find("span.seeds").Remove()
filesize, _ := humanize.ParseBytes(strings.TrimSpace(tr.Find("td.coll-4.size").Text())) // convert human words to bytes number
// url
URL, _ := tr.Find(`a[href^="/torrent"]`).Attr("href")
if title == "" || URL == "" || seeders == 0 {
return
}
// ---
source := models.Source{
From: "1337x",
Title: strings.TrimSpace(title),
URL: Site + URL,
Seeders: seeders,
Leechers: leechers,
FileSize: int64(filesize),
}
sources = append(sources, source)
})
logrus.Debugf("1337x: [%d] Amount of results: %d", page, len(sources))
logrus.Debugf("1337x: [%d] Getting sources in parallel...", page)
group := sync.WaitGroup{}
for _, source := range sources {
group.Add(1)
go func(source models.Source) {
var magnet string
_, html, err := request.Get(nil, source.URL, nil)
if err != nil {
logrus.Errorln(err)
group.Done()
return
}
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(html))
dropdown := doc.Find("ul.dropdown-menu")
li := dropdown.Find("li")
if li != nil {
if val, ok := li.Last().Find("a").Attr("href"); ok {
magnet = val
}
}
// Assignment
source.Magnet = magnet
*results = append(*results, source)
group.Done()
}(source)
}
group.Wait()
wg.Done()
}
// Checks if the text contains HTML-encoded entities
func containsHTMLEncodedEntities(text string) bool {
return strings.ContainsAny(text, "&<>'\"")
}
// Decodes HTML-encoded text
func decodeHTMLText(text string) (string, error) {
var decodedText string
tokenizer := html.NewTokenizer(strings.NewReader(text))
for {
tokenType := tokenizer.Next()
switch tokenType {
case html.ErrorToken:
err := tokenizer.Err()
if err != nil {
return text, err // Return the original text and the decoding error
}
return decodedText, nil // Return the decoded text
case html.TextToken:
token := tokenizer.Token()
decodedText += token.Data
}
}
}