-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
114 lines (99 loc) · 2.81 KB
/
parser.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
package kickass
import (
"strconv"
"gopkg.in/xmlpath.v2"
)
// XPATH
var (
xpathNoResult = xmlpath.MustCompile("//text()[contains(.,'did not match any documents')]")
xpathTorrentResults = xmlpath.MustCompile("//tr[contains(@id, 'torrent_')]")
xpathTorrentName = xmlpath.MustCompile(".//a[@class=\"cellMainLink\"]")
xpathTorrentURL = xmlpath.MustCompile(".//a[contains(@title,'Download torrent file')]/@href")
xpathMagnetURL = xmlpath.MustCompile(".//a[contains(@title,'Torrent magnet link')]/@href")
xpathSeed = xmlpath.MustCompile(".//td[5]")
xpathLeech = xmlpath.MustCompile(".//td[6]")
xpathAge = xmlpath.MustCompile(".//td[4]")
xpathSize = xmlpath.MustCompile(".//td[2]")
xpathFileCount = xmlpath.MustCompile(".//td[3]")
xpathVerify = xmlpath.MustCompile(".//a[contains(@class,'iverify')]")
xpathUser = xmlpath.MustCompile(".//a[contains(@href, '/user/')]")
)
// Default parse function, to be overwritten during the tests
var parseFunc = parseResult
func parseResult(root *xmlpath.Node) ([]*Torrent, error) {
torrents := []*Torrent{}
// Don't go further if there is no results
if xpathNoResult.Exists(root) {
return torrents, nil
}
iter := xpathTorrentResults.Iter(root)
for iter.Next() {
name, ok := xpathTorrentName.String(iter.Node())
if !ok {
return nil, ErrUnexpectedContent
}
torrentURL, ok := xpathTorrentURL.String(iter.Node())
if !ok {
return nil, ErrUnexpectedContent
}
magnet, ok := xpathMagnetURL.String(iter.Node())
if !ok {
return nil, ErrUnexpectedContent
}
verify := xpathVerify.Exists(iter.Node())
if !ok {
return nil, ErrUnexpectedContent
}
seedStr, ok := xpathSeed.String(iter.Node())
if !ok {
return nil, ErrUnexpectedContent
}
seed, err := strconv.Atoi(seedStr)
if err != nil {
return nil, err
}
leechStr, ok := xpathLeech.String(iter.Node())
if !ok {
return nil, ErrUnexpectedContent
}
leech, err := strconv.Atoi(leechStr)
if err != nil {
return nil, err
}
age, ok := xpathAge.String(iter.Node())
if !ok {
return nil, ErrUnexpectedContent
}
fileCountStr, ok := xpathFileCount.String(iter.Node())
if !ok {
return nil, ErrUnexpectedContent
}
fileCount, err := strconv.Atoi(fileCountStr)
if err != nil {
return nil, err
}
size, ok := xpathSize.String(iter.Node())
if !ok {
return nil, ErrUnexpectedContent
}
user, ok := xpathUser.String(iter.Node())
if !ok {
// The user name is not always present
user = ""
}
t := &Torrent{
Name: name,
TorrentURL: torrentURL,
MagnetURL: magnet,
Seed: seed,
Leech: leech,
Age: age,
FileCount: fileCount,
Size: size,
Verified: verify,
User: user,
}
torrents = append(torrents, t)
}
return torrents, nil
}