-
Notifications
You must be signed in to change notification settings - Fork 6
/
torrent_page.go
195 lines (169 loc) · 4.88 KB
/
torrent_page.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
tpb-parser
Copyright (C) 2016 Denis V Chapligin <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"bytes"
"github.com/PuerkitoBio/goquery"
"regexp"
"strconv"
"strings"
"sync"
"time"
)
const (
streams = 128
)
type TorrentEntry struct {
Id int
Title string
Size int
Files int
Category string
Subcategory string
By string
Hash string
Uploaded time.Time
Magnet string
Info string
}
type Downloader struct {
topUrl string
initialId int
pageId chan int
wg sync.WaitGroup
output OutputModule
}
func newDownloader(o OutputModule, tU string, id int) *Downloader {
return &Downloader{
topUrl: tU,
initialId: id,
pageId: make(chan int, 1024),
wg: sync.WaitGroup{},
output: o,
}
}
func (d *Downloader) run() {
d.wg.Add(streams)
for w := 0; w <= streams; w++ {
go d.processPage()
}
for w := d.initialId; w >= 0; w-- {
d.pageId <- w
}
close(d.pageId)
log.Info("Processing complete, waiting for goroutines to finish")
d.wg.Wait()
d.output.Done()
}
func (d *Downloader) processPage() {
for id := range d.pageId {
var url bytes.Buffer
url.WriteString(d.topUrl)
url.WriteString("/torrent/")
url.WriteString(strconv.Itoa(id))
log.Info("Parsing torrent page at: %s", url.String())
doc, err := goquery.NewDocument(url.String())
if err != nil {
log.Warning("Can't download torrent page %s from TPB: %v", url, err)
continue
}
torrentData := doc.Find("#detailsframe")
if torrentData.Length() < 1 {
log.Warning("Erroneous torrent %d: \"%s\"", id, url.String())
continue
}
torrent := TorrentEntry{Id: id}
torrent.processTitle(torrentData)
torrent.processFirstColumn(torrentData)
torrent.processSecondColumn(torrentData)
torrent.processHash(torrentData)
torrent.processMagnet(torrentData)
torrent.processInfo(torrentData)
d.output.Put(&torrent)
log.Info("Processed torrent %d: \"%s\"", id, torrent.Title)
}
d.wg.Done()
}
func (t *TorrentEntry) processTitle(torrentData *goquery.Selection) {
t.Title = strings.TrimSpace(torrentData.Find("#title").Text())
}
func (t *TorrentEntry) processFirstColumn(torrentData *goquery.Selection) {
data := torrentData.Find("#details .col1 dt")
if data.Size() < 3 {
log.Warning("Not enough data to parse in first coumnt")
}
//Categories
categoryData, _ := data.First().Next().Find("a").Html()
categoryData = strings.Replace(categoryData, ">", ">", -1)
categories := strings.Split(categoryData, ">")
if len(categories) < 2 {
log.Warning("Can't retrieve category and sub category of torrent")
t.Category = "Unknown"
t.Subcategory = "Unknown"
} else {
t.Category = strings.TrimSpace(categories[0])
t.Subcategory = strings.TrimSpace(categories[1])
}
//Files
filesData, _ := data.Eq(1).Next().Find("a").Html()
files, err := strconv.Atoi(filesData)
if err != nil {
log.Warning("Can't retrieve number of files in torrent")
t.Files = -1
} else {
t.Files = files
}
//Size
sizeData, _ := data.Eq(2).Next().Html()
sizeData = strings.TrimSpace(sizeData)
rx, _ := regexp.Compile(`\((\d+)`)
if rx.MatchString(sizeData) {
size, err := strconv.Atoi(rx.FindStringSubmatch(sizeData)[1])
if err != nil {
log.Warning("Can't retrieve size of files in the torrent")
t.Size = -1
} else {
t.Size = size
}
} else {
log.Warning("Can't parse size of files in the torrent")
t.Size = -1
}
}
func (t *TorrentEntry) processSecondColumn(torrentData *goquery.Selection) {
data := torrentData.Find("#details .col2 dt")
if data.Size() < 2 {
log.Warning("Not enough data to parse in second coumnt")
}
//Uploaded
uploadedData := data.First().Next().Text()
t.Uploaded, _ = time.Parse("2006-01-02 15:04:05 MST", uploadedData)
//By
t.By = strings.TrimSpace(data.Eq(1).Next().Find("a").Text())
}
func (t *TorrentEntry) processHash(torrentData *goquery.Selection) {
t.Hash = strings.TrimSpace(torrentData.Find("#details .col2").Contents().Last().Text())
}
func (t *TorrentEntry) processMagnet(torrentData *goquery.Selection) {
u, pU := torrentData.Find(".download a").First().Attr("href")
if pU {
t.Magnet = strings.TrimSpace(u)
} else {
t.Magnet = ""
}
}
func (t *TorrentEntry) processInfo(torrentData *goquery.Selection) {
t.Info, _ = torrentData.Find(".nfo pre").Html()
}