-
Notifications
You must be signed in to change notification settings - Fork 0
/
jackett.go
74 lines (64 loc) · 1.58 KB
/
jackett.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
package jackett
import (
"encoding/json"
"net/http"
"net/url"
"strconv"
)
// Client represents a jackett client
type Client struct {
URL string
APIKey string
}
// New returns a new jackett client
func New(url, apiKey string) *Client {
return &Client{
URL: url,
APIKey: apiKey,
}
}
// Indexer represents an indexer
type Indexer struct {
ID string `json:"ID"`
Name string `json:"Name"`
Status int `json:"Status"`
Results int `json:"Results"`
Error string `json:"Error"`
}
// Result represents the jackett results
type Result struct {
Tracker string `json:"Tracker"`
TrackerID string `json:"TrackerId"`
CategoryDesc string `json:"CategoryDesc"`
Title string `json:"Title"`
GUID string `json:"Guid"`
Link string `json:"Link"`
Size int `json:"Size"`
Seeders int `json:"Seeders"`
Peers int `json:"Peers"`
}
// Response represents a response
type Response struct {
Results []*Result `json:"Results"`
Indexers []*Indexer `json:"Indexers"`
}
// Search searches for stuff
func (c *Client) Search(query string, trackers []Tracker, categories []Category) (*Response, error) {
v := url.Values{}
v.Add("apikey", c.APIKey)
v.Add("Query", query)
for _, c := range categories {
v.Add("Category", strconv.Itoa(int(c)))
}
for _, t := range trackers {
v.Add("Tracker", string(t))
}
url := c.URL + "/api/v2.0/indexers/all/results?" + v.Encode()
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
r := &Response{}
return r, json.NewDecoder(resp.Body).Decode(r)
}