-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
122 lines (99 loc) · 2.59 KB
/
client.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
package kickass
import (
"errors"
"fmt"
"net/http"
"gopkg.in/xmlpath.v2"
)
// Kickass default endpoint
const DefaultEndpoint = "https://kat.cr"
// MaxElementsPerPage represents the max number of elements per page
const MaxElementsPerPage = 25
// Custom errors
var (
ErrUnexpectedContent = errors.New("kickass: unexpected content")
ErrMissingUserParam = errors.New("kickass: missing user param")
ErrMissingSearchParam = errors.New("kickass: missing search param")
)
// Torrent represents a torrent from kickass
type Torrent struct {
Name string
TorrentURL string
MagnetURL string
Seed int
Leech int
Age string
Size string
FileCount int
Verified bool
User string
}
// Client represents the kickass client
type Client struct {
Endpoint string
HTTPClient *http.Client
}
// New creates a new client
func New() *Client {
return &Client{
Endpoint: DefaultEndpoint,
HTTPClient: http.DefaultClient,
}
}
func (c *Client) searchBaseURL(q *Query) string {
return fmt.Sprintf("%s/usearch/%s", c.Endpoint, q.searchField())
}
// Search searches from a query
func (c *Client) Search(q *Query) ([]*Torrent, error) {
// The only required param is the search
if q.Search == "" {
return nil, ErrMissingSearchParam
}
return c.getPages(q, c.searchBaseURL(q))
}
func (c *Client) listByUserBaseURL(q *Query) string {
return fmt.Sprintf("%s/user/%s/uploads", c.Endpoint, q.User)
}
// ListByUser returns the torrents for a specific user
func (c *Client) ListByUser(q *Query) ([]*Torrent, error) {
// The only required param is the user
if q.User == "" {
return nil, ErrMissingUserParam
}
return c.getPages(q, c.listByUserBaseURL(q))
}
// getPages downloads each page and merges the results
func (c *Client) getPages(q *Query, baseURL string) ([]*Torrent, error) {
torrents := []*Torrent{}
// Set default number of pages to 1
if q.Pages == 0 {
q.Pages = 1
}
for i := 1; i <= q.Pages; i++ {
URL := fmt.Sprintf("%s/%s", baseURL, q.urlParams(i))
t, err := c.getPage(URL)
if err != nil {
return nil, err
}
torrents = append(torrents, t...)
// If the number of results is lower than the max number of elements
// per page that means there is no need to continue
if len(t) < MaxElementsPerPage {
break
}
}
return torrents, nil
}
// getPage downloads a page and parses its content
func (c *Client) getPage(URL string) ([]*Torrent, error) {
resp, err := c.HTTPClient.Get(URL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
root, err := xmlpath.ParseHTML(resp.Body)
if err != nil {
return nil, err
}
return parseFunc(root)
}