-
Notifications
You must be signed in to change notification settings - Fork 1
/
tpb.go
97 lines (86 loc) · 2.43 KB
/
tpb.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
package tpb
import (
"context"
"errors"
"fmt"
"net/url"
"strconv"
"time"
)
var defaultTimeout = 10 * time.Second
// ErrMissingEndpoint is the error returned if there's no endpoint
var ErrMissingEndpoint = errors.New("tpb: missing endpoint")
// Client represent a Client used to make Search
type Client struct {
endpoints endpoints
// MaxTries represent the number total number of endpoint to try to find a
// result
MaxTries int
// EndpointTimeout defines the timeout to use by endpoint
EndpointTimeout time.Duration
}
// New return a new client with a the given endpoint(s)
func New(endpoints ...string) *Client {
return &Client{
endpoints: newEndpoints(endpoints...),
MaxTries: len(endpoints),
EndpointTimeout: defaultTimeout,
}
}
// Search will search torrents
func (c *Client) Search(ctx context.Context, search string, opts *SearchOptions) ([]*Torrent, error) {
if opts == nil {
opts = &SearchOptions{
Category: All,
}
}
v := opts.Category.URLValue()
v.Add("q", search)
path := "/q.php?" + v.Encode()
return c.fetchTorrents(ctx, path)
}
// User lists the torrents uploaded by a user
func (c *Client) User(ctx context.Context, user string, opts *UserOptions) ([]*Torrent, error) {
if opts == nil {
opts = &UserOptions{
Page: 0,
}
}
query := fmt.Sprintf("user:%s:%d", user, opts.Page)
v := url.Values{}
v.Add("q", query)
path := "/q.php?" + v.Encode()
return c.fetchTorrents(ctx, path)
}
// Category lists the latest torrents for a category
func (c *Client) Category(ctx context.Context, category TorrentCategory, opts *CategoryOptions) ([]*Torrent, error) {
if opts == nil {
opts = &CategoryOptions{
Page: 0,
}
}
query := fmt.Sprintf("category:%d:%d", category, opts.Page)
v := url.Values{}
v.Add("q", query)
path := "/q.php?" + v.Encode()
return c.fetchTorrents(ctx, path)
}
// Top100 lists the Top100 torrents
func (c *Client) Top100(ctx context.Context, cat TorrentCategory) ([]*Torrent, error) {
var catString = "all"
if cat != All {
catString = strconv.Itoa(int(cat))
}
path := "/precompiled/data_top100_" + catString + ".json"
return c.fetchTorrents(ctx, path)
}
// Infos gets infos about a given torrent ID
func (c *Client) Infos(ctx context.Context, id int) (*Torrent, error) {
t := &Torrent{}
v := url.Values{}
v.Add("id", strconv.Itoa(id))
path := "/t.php?" + v.Encode()
return t, c.fetch(ctx, path, &t)
}
// TODO: Implement FileList
// curl https://apibay.org/f.php?id=36120091