forked from tnychn/torrodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbt4g.go
177 lines (149 loc) · 4.49 KB
/
bt4g.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
package bt4g
import (
"fmt"
"net/http"
"regexp"
"strconv"
"strings"
"sync"
"github.com/PuerkitoBio/goquery"
"github.com/dustin/go-humanize"
"github.com/sirupsen/logrus"
"golang.org/x/net/html"
"github.com/stl3/torgo/models"
"github.com/stl3/torgo/request"
)
const (
Name = "Bt4g"
Site = "https://bt4gprx.com"
)
type provider struct {
models.Provider
}
func New() models.ProviderInterface {
provider := &provider{}
provider.Name = Name
provider.Site = Site
provider.Categories = models.Categories{
All: "/search?q=%v&category=all&orderby=seeders&p=%d",
}
return provider
}
func (provider *provider) Search(query string, count int, categoryURL models.CategoryURL) ([]models.Source, error) {
results, err := provider.Query(query, categoryURL, count, 50, 1, extractor)
return results, err
}
func extractor(surl string, page int, results *[]models.Source, wg *sync.WaitGroup) {
logrus.Infof("Bt4g: [%d] Extracting results...\n", page)
_, html, err := request.Get(nil, surl, nil)
if err != nil {
logrus.Errorln(fmt.Sprintf("Bt4g: [%d]", page), err)
wg.Done()
return
}
var sources []models.Source
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(html))
resultsContainer := doc.Find("div.col.s12 > div")
resultsContainer.Each(func(_ int, result *goquery.Selection) {
title := result.Find("h5 a").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)
}
URL, _ := result.Find("h5 a").Attr("href")
logrus.Infof("URL: %s", URL)
newURL := "https://bt4gprx.com" + URL
logrus.Infof("newURL: %s", newURL)
hash, err := getHashFromURL(newURL)
if err != nil {
fmt.Println("Error:", err)
return
}
// Construct the magnet URI
magnet := fmt.Sprintf("magnet:?xt=urn:btih:%s", hash)
filesizeStr := result.Find("b.cpill").Text()
filesize, _ := humanize.ParseBytes(strings.TrimSpace(filesizeStr))
seedersStr := result.Find("b#seeders").Text()
seeders, _ := strconv.Atoi(seedersStr)
leechersStr := result.Find("b#leechers").Text()
leechers, _ := strconv.Atoi(leechersStr)
source := models.Source{
From: "Bt4g",
Title: title,
URL: Site + URL,
Seeders: seeders,
Leechers: leechers,
FileSize: int64(filesize),
Magnet: magnet,
}
sources = append(sources, source)
})
logrus.Debugf("Bt4g: [%d] Amount of results: %d", page, len(sources))
*results = append(*results, sources...)
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
}
}
}
func getHashFromURL(url string) (string, error) {
// Make a GET request to the URL
response, err := http.Get(url)
if err != nil {
return "", err
}
defer response.Body.Close()
// Parse the HTML response
document, err := goquery.NewDocumentFromReader(response.Body)
if err != nil {
return "", err
}
// Extract the href attribute value from the specified selector
href := document.Find(".s12 > table:nth-child(3) > tbody:nth-child(2) > tr:nth-child(1) > th:nth-child(1) > a:nth-child(1)").AttrOr("href", "")
if href == "" {
return "", fmt.Errorf("href attribute not found")
}
href1, err := ExtractMagnetHash(href)
if err != nil {
return "", err
}
return href1, nil
}
func ExtractMagnetHash(href string) (string, error) {
// Regular expression to match the magnet hash
re := regexp.MustCompile(`\/hash\/([a-fA-F0-9]+)`)
// Find submatches
matches := re.FindStringSubmatch(href)
if len(matches) < 2 {
return "", fmt.Errorf("unable to extract magnet hash from href")
}
// Extract and return the magnet hash
magnetHash := matches[1]
return magnetHash, nil
}