-
Notifications
You must be signed in to change notification settings - Fork 1
/
eztv.go
182 lines (157 loc) · 4.81 KB
/
eztv.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
package eztv
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
var endpoint = "https://eztv.re"
const (
torrentEndpoint = "/api/get-torrents"
)
// Eztv errors
var (
ErrEpisodeNotFound = errors.New("episode not found")
ErrShowNotFound = errors.New("show not found")
ErrInvalidArgument = errors.New("invalid argument")
ErrMissingArgument = errors.New("missing argument")
)
// response reprensents the response structure of every call
type response struct {
TorrentsCount int `json:"torrents_count"`
Limit int `json:"limit"`
Page int `json:"page"`
ImdbID string `json:"imdb_id"`
Torrents []*EpisodeTorrent `json:"torrents"`
}
// EpisodeTorrent represents a torrent for an episode
type EpisodeTorrent struct {
ID int `json:"id"`
Hash string `json:"hash"`
Filename string `json:"filename"`
EpisodeURL string `json:"episode_url"`
TorrentURL string `json:"torrent_url"`
MagnetURL string `json:"magnet_url"`
Title string `json:"title"`
ImdbID string `json:"imdb_id"`
Season int `json:"season,string"`
Episode int `json:"episode,string"`
SmallScreenshot string `json:"small_screenshot"`
LargeScreenshot string `json:"large_screenshot"`
Seeds int `json:"seeds"`
Peers int `json:"peers"`
DateReleased time.Time `json:"date_released_unix"`
Size uint `json:"size_bytes,string"`
}
// UnmarshalJSON implements the Unmarshaller interface
func (t *EpisodeTorrent) UnmarshalJSON(data []byte) error {
type Alias EpisodeTorrent
// Unmarshal everything except the fields we need to edit
aux := &struct {
*Alias
ImdbID string `json:"imdb_id"`
DateReleased int64 `json:"date_released_unix"`
SmallScreenshot string `json:"small_screenshot"`
LargeScreenshot string `json:"large_screenshot"`
}{}
err := json.Unmarshal(data, aux)
if err != nil {
return err
}
// Copy the whole thing
*t = EpisodeTorrent(*(aux.Alias))
// Set the proper DateReleased time / imdbID / screenshots URLs
t.DateReleased = time.Unix(aux.DateReleased, 0)
t.ImdbID = "tt" + aux.ImdbID
t.SmallScreenshot = "https:" + aux.SmallScreenshot
t.LargeScreenshot = "https:" + aux.LargeScreenshot
return nil
}
func get(path string, v *url.Values) (*response, error) {
// Make the request
resp, err := http.Get(endpoint + path + "?" + v.Encode())
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("got status %d when making the request", resp.StatusCode)
}
r := &response{}
return r, json.NewDecoder(resp.Body).Decode(&r)
}
// getAll makes requests on all the pages until we have all the torrents
func getAll(path string, v *url.Values) ([]*EpisodeTorrent, error) {
var maxPages = 20
var torrents []*EpisodeTorrent
for i := 1; i < maxPages; i++ {
v.Add("page", strconv.Itoa(i))
v.Add("limit", "100")
resp, err := get(path, v)
if err != nil {
return nil, err
}
// No ImdbID or no torrents are considered as a failure
if resp.ImdbID == "" || resp.TorrentsCount == 0 {
return nil, ErrShowNotFound
}
torrents = append(torrents, resp.Torrents...)
// We asked for a 100 torrents, so if we got less, then the search is
// over
if len(resp.Torrents) < 100 {
break
}
}
return torrents, nil
}
// GetShowTorrents will get the torrents of a show from an ImdbID
func GetShowTorrents(imdbID string) ([]*EpisodeTorrent, error) {
if imdbID == "" {
return nil, ErrMissingArgument
}
// The API only accepts imdbIDs without their "tt"
imdbID = strings.Replace(imdbID, "t", "", -1)
v := &url.Values{}
v.Add("imdb_id", imdbID)
// Make the request
torrents, err := getAll(torrentEndpoint, v)
if err != nil {
return nil, err
}
return torrents, nil
}
// GetEpisodeTorrents will get the torrents of show's episode from an ImdbID,
// a season number and an episode number
func GetEpisodeTorrents(ImdbID string, season, episode int) ([]*EpisodeTorrent, error) {
episodeTorrents := []*EpisodeTorrent{}
torrents, err := GetShowTorrents(ImdbID)
if err != nil {
return nil, err
}
for _, e := range torrents {
if e.Episode == episode && e.Season == season {
episodeTorrents = append(episodeTorrents, e)
}
}
if len(episodeTorrents) == 0 {
return nil, ErrEpisodeNotFound
}
return episodeTorrents, nil
}
// GetTorrents will get torrents, given a limit and a page number
func GetTorrents(limit, page int) ([]*EpisodeTorrent, error) {
// Generate URL
v := &url.Values{}
v.Add("limit", strconv.Itoa(limit))
v.Add("page", strconv.Itoa(page))
// Make the request
r, err := get(torrentEndpoint, v)
if err != nil {
return nil, err
}
return r.Torrents, nil
}