-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathwhatapi.go
443 lines (412 loc) · 14.5 KB
/
whatapi.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//Package whatapi is a wrapper for the What.CD JSON API (https://github.com/WhatCD/Gazelle/wiki/JSON-API-Documentation).
package whatapi
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"strconv"
)
//NewWhatAPI creates a new client for the What.CD API using the provided URL.
func NewWhatAPI(url string) (*WhatAPI, error) {
w := new(WhatAPI)
w.baseURL = url
cookieJar, err := cookiejar.New(nil)
if err != nil {
return w, err
}
w.client = &http.Client{Jar: cookieJar}
return w, err
}
//WhatAPI represents a client for the What.CD API.
type WhatAPI struct {
baseURL string
client *http.Client
authkey string
passkey string
loggedIn bool
}
//GetJSON sends a HTTP GET request to the API and decodes the JSON response into responseObj.
func (w *WhatAPI) GetJSON(requestURL string, responseObj interface{}) error {
if w.loggedIn {
resp, err := w.client.Get(requestURL)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return errRequestFailedReason("Status Code " + resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return json.Unmarshal(body, responseObj)
}
return errRequestFailedLogin
}
//CreateDownloadURL constructs a download URL using the provided torrent id.
func (w *WhatAPI) CreateDownloadURL(id int) (string, error) {
if w.loggedIn {
params := url.Values{}
params.Set("action", "download")
params.Set("id", strconv.Itoa(id))
params.Set("authkey", w.authkey)
params.Set("torrent_pass", w.passkey)
downloadURL, err := buildURL(w.baseURL, "torrents.php", "", params)
if err != nil {
return "", err
}
return downloadURL, nil
}
return "", errRequestFailedLogin
}
//Login logs in to the API using the provided credentials.
func (w *WhatAPI) Login(username, password string) error {
params := url.Values{}
params.Set("username", username)
params.Set("password", password)
resp, err := w.client.PostForm(w.baseURL+"login.php?", params)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.Request.URL.String()[len(w.baseURL):] != "index.php" {
return errLoginFailed
}
w.loggedIn = true
account, err := w.GetAccount()
if err != nil {
return err
}
w.authkey, w.passkey = account.AuthKey, account.PassKey
return nil
}
//Logout logs out of the API, ending the current session.
func (w *WhatAPI) Logout() error {
params := url.Values{"auth": {w.authkey}}
requestURL, err := buildURL(w.baseURL, "logout.php", "", params)
if err != nil {
return err
}
_, err = w.client.Get(requestURL)
if err != nil {
return err
}
w.loggedIn, w.authkey, w.passkey = false, "", ""
return nil
}
//GetAccount retrieves account information for the current user.
func (w *WhatAPI) GetAccount() (Account, error) {
account := AccountResponse{}
requestURL, err := buildURL(w.baseURL, "ajax.php", "index", url.Values{})
if err != nil {
return account.Response, err
}
err = w.GetJSON(requestURL, &account)
if err != nil {
return account.Response, err
}
return account.Response, checkResponseStatus(account.Status, account.Error)
}
//GetMailbox retrieves mailbox information for the current user using the provided parameters.
func (w *WhatAPI) GetMailbox(params url.Values) (Mailbox, error) {
mailbox := MailboxResponse{}
requestURL, err := buildURL(w.baseURL, "ajax.php", "inbox", params)
if err != nil {
return mailbox.Response, err
}
err = w.GetJSON(requestURL, &mailbox)
if err != nil {
return mailbox.Response, err
}
return mailbox.Response, checkResponseStatus(mailbox.Status, mailbox.Error)
}
//GetConversation retrieves conversation information for the current user using the provided conversation id and parameters.
func (w *WhatAPI) GetConversation(id int) (Conversation, error) {
conversation := ConversationResponse{}
params := url.Values{}
params.Set("type", "viewconv")
params.Set("id", strconv.Itoa(id))
requestURL, err := buildURL(w.baseURL, "ajax.php", "inbox", params)
if err != nil {
return conversation.Response, err
}
err = w.GetJSON(requestURL, &conversation)
if err != nil {
return conversation.Response, err
}
return conversation.Response, checkResponseStatus(conversation.Status, conversation.Error)
}
//GetNotifications retrieves notification information using the specifed parameters.
func (w *WhatAPI) GetNotifications(params url.Values) (Notifications, error) {
notifications := NotificationsResponse{}
requestURL, err := buildURL(w.baseURL, "ajax.php", "notifications", params)
if err != nil {
return notifications.Response, err
}
err = w.GetJSON(requestURL, ¬ifications)
if err != nil {
return notifications.Response, err
}
return notifications.Response, checkResponseStatus(notifications.Status, notifications.Error)
}
//GetAnnouncements retrieves announcement information.
func (w *WhatAPI) GetAnnouncements() (Announcements, error) {
params := url.Values{}
announcements := AnnouncementsResponse{}
requestURL, err := buildURL(w.baseURL, "ajax.php", "announcements", params)
if err != nil {
return announcements.Response, err
}
err = w.GetJSON(requestURL, &announcements)
if err != nil {
return announcements.Response, err
}
return announcements.Response, checkResponseStatus(announcements.Status, announcements.Error)
}
//GetSubscriptions retrieves forum subscription information for the current user using the provided parameters.
func (w *WhatAPI) GetSubscriptions(params url.Values) (Subscriptions, error) {
subscriptions := SubscriptionsResponse{}
requestURL, err := buildURL(w.baseURL, "ajax.php", "subscriptions", params)
if err != nil {
return subscriptions.Response, err
}
err = w.GetJSON(requestURL, &subscriptions)
if err != nil {
return subscriptions.Response, err
}
return subscriptions.Response, checkResponseStatus(subscriptions.Status, subscriptions.Error)
}
//GetCategories retrieves forum category information.
func (w *WhatAPI) GetCategories() (Categories, error) {
categories := CategoriesResponse{}
params := url.Values{}
params.Set("type", "main")
requestURL, err := buildURL(w.baseURL, "ajax.php", "forum", params)
if err != nil {
return categories.Response, err
}
err = w.GetJSON(requestURL, &categories)
if err != nil {
return categories.Response, err
}
return categories.Response, checkResponseStatus(categories.Status, categories.Error)
}
//GetForum retrieves forum information using the provided forum id and parameters.
func (w *WhatAPI) GetForum(id int, params url.Values) (Forum, error) {
forum := ForumResponse{}
params.Set("type", "viewforum")
params.Set("forumid", strconv.Itoa(id))
requestURL, err := buildURL(w.baseURL, "ajax.php", "forum", params)
if err != nil {
return forum.Response, err
}
err = w.GetJSON(requestURL, &forum)
if err != nil {
return forum.Response, err
}
return forum.Response, checkResponseStatus(forum.Status, forum.Error)
}
//GetThread retrieves forum thread information using the provided thread id and parameters.
func (w *WhatAPI) GetThread(id int, params url.Values) (Thread, error) {
thread := ThreadResponse{}
params.Set("type", "viewthread")
params.Set("threadid", strconv.Itoa(id))
requestURL, err := buildURL(w.baseURL, "ajax.php", "forum", params)
if err != nil {
return thread.Response, err
}
err = w.GetJSON(requestURL, &thread)
if err != nil {
return thread.Response, err
}
return thread.Response, checkResponseStatus(thread.Status, thread.Error)
}
//GetArtistBookmarks retrieves artist bookmark information for the current user.
func (w *WhatAPI) GetArtistBookmarks() (ArtistBookmarks, error) {
artistBookmarks := ArtistBookmarksResponse{}
params := url.Values{}
params.Set("type", "artists")
requestURL, err := buildURL(w.baseURL, "ajax.php", "bookmarks", params)
if err != nil {
return artistBookmarks.Response, err
}
err = w.GetJSON(requestURL, &artistBookmarks)
if err != nil {
return artistBookmarks.Response, err
}
return artistBookmarks.Response, checkResponseStatus(artistBookmarks.Status, artistBookmarks.Error)
}
//GetTorrentBookmarks retrieves torrent bookmark information for the current user.
func (w *WhatAPI) GetTorrentBookmarks() (TorrentBookmarks, error) {
torrentBookmarks := TorrentBookmarksResponse{}
params := url.Values{}
params.Set("type", "torrents")
requestURL, err := buildURL(w.baseURL, "ajax.php", "bookmarks", params)
if err != nil {
return torrentBookmarks.Response, err
}
err = w.GetJSON(requestURL, &torrentBookmarks)
if err != nil {
return torrentBookmarks.Response, err
}
return torrentBookmarks.Response, checkResponseStatus(torrentBookmarks.Status, torrentBookmarks.Error)
}
//GetArtist retrieves artist information using the provided artist id and parameters.
func (w *WhatAPI) GetArtist(id int, params url.Values) (Artist, error) {
artist := ArtistResponse{}
params.Set("id", strconv.Itoa(id))
requestURL, err := buildURL(w.baseURL, "ajax.php", "artist", params)
if err != nil {
return artist.Response, err
}
err = w.GetJSON(requestURL, &artist)
if err != nil {
return artist.Response, err
}
return artist.Response, checkResponseStatus(artist.Status, artist.Error)
}
//GetRequest retrieves request information using the provided request id and parameters.
func (w *WhatAPI) GetRequest(id int, params url.Values) (Request, error) {
request := RequestResponse{}
params.Set("id", strconv.Itoa(id))
requestURL, err := buildURL(w.baseURL, "ajax.php", "request", params)
if err != nil {
return request.Response, err
}
err = w.GetJSON(requestURL, &request)
if err != nil {
return request.Response, err
}
return request.Response, checkResponseStatus(request.Status, request.Error)
}
//GetTorrent retrieves torrent information using the provided torrent id and parameters.
func (w *WhatAPI) GetTorrent(id int, params url.Values) (Torrent, error) {
torrent := TorrentResponse{}
params.Set("id", strconv.Itoa(id))
requestURL, err := buildURL(w.baseURL, "ajax.php", "torrent", params)
if err != nil {
return torrent.Response, err
}
err = w.GetJSON(requestURL, &torrent)
if err != nil {
return torrent.Response, err
}
return torrent.Response, checkResponseStatus(torrent.Status, torrent.Error)
}
//GetTorrentGroup retrieves torrent group information using the provided torrent group id and parameters.
func (w *WhatAPI) GetTorrentGroup(id int, params url.Values) (TorrentGroup, error) {
torrentGroup := TorrentGroupResponse{}
params.Set("id", strconv.Itoa(id))
requestURL, err := buildURL(w.baseURL, "ajax.php", "torrentgroup", params)
if err != nil {
return torrentGroup.Response, err
}
err = w.GetJSON(requestURL, &torrentGroup)
if err != nil {
return torrentGroup.Response, err
}
return torrentGroup.Response, checkResponseStatus(torrentGroup.Status, torrentGroup.Error)
}
//SearchTorrents retrieves torrent search results using the provided search string and parameters.
func (w *WhatAPI) SearchTorrents(searchStr string, params url.Values) (TorrentSearch, error) {
torrentSearch := TorrentSearchResponse{}
params.Set("searchstr", searchStr)
requestURL, err := buildURL(w.baseURL, "ajax.php", "browse", params)
if err != nil {
return torrentSearch.Response, err
}
err = w.GetJSON(requestURL, &torrentSearch)
if err != nil {
return torrentSearch.Response, err
}
return torrentSearch.Response, checkResponseStatus(torrentSearch.Status, torrentSearch.Error)
}
//SearchRequests retrieves request search results using the provided search string and parameters.
func (w *WhatAPI) SearchRequests(searchStr string, params url.Values) (RequestsSearch, error) {
requestsSearch := RequestsSearchResponse{}
params.Set("search", searchStr)
requestURL, err := buildURL(w.baseURL, "ajax.php", "requests", params)
if err != nil {
return requestsSearch.Response, err
}
err = w.GetJSON(requestURL, &requestsSearch)
if err != nil {
return requestsSearch.Response, err
}
return requestsSearch.Response, checkResponseStatus(requestsSearch.Status, requestsSearch.Error)
}
//SearchUsers retrieves user search results using the provided search string and parameters.
func (w *WhatAPI) SearchUsers(searchStr string, params url.Values) (UserSearch, error) {
userSearch := UserSearchResponse{}
params.Set("search", searchStr)
requestURL, err := buildURL(w.baseURL, "ajax.php", "usersearch", params)
if err != nil {
return userSearch.Response, err
}
err = w.GetJSON(requestURL, &userSearch)
if err != nil {
return userSearch.Response, err
}
return userSearch.Response, checkResponseStatus(userSearch.Status, userSearch.Error)
}
//GetTopTenTorrents retrieves "top ten torrents" information using the provided parameters.
func (w *WhatAPI) GetTopTenTorrents(params url.Values) (TopTenTorrents, error) {
topTenTorrents := TopTenTorrentsResponse{}
params.Set("type", "torrents")
requestURL, err := buildURL(w.baseURL, "ajax.php", "top10", params)
if err != nil {
return topTenTorrents.Response, err
}
err = w.GetJSON(requestURL, &topTenTorrents)
if err != nil {
return topTenTorrents.Response, err
}
return topTenTorrents.Response, checkResponseStatus(topTenTorrents.Status, topTenTorrents.Error)
}
//GetTopTenTags retrieves "top ten tags" information using the provided parameters.
func (w *WhatAPI) GetTopTenTags(params url.Values) (TopTenTags, error) {
topTenTags := TopTenTagsResponse{}
params.Set("type", "tags")
requestURL, err := buildURL(w.baseURL, "ajax.php", "top10", params)
if err != nil {
return topTenTags.Response, err
}
err = w.GetJSON(requestURL, &topTenTags)
if err != nil {
return topTenTags.Response, err
}
return topTenTags.Response, checkResponseStatus(topTenTags.Status, topTenTags.Error)
}
//GetTopTenUsers retrieves "top tem users" information using the provided parameters.
func (w *WhatAPI) GetTopTenUsers(params url.Values) (TopTenUsers, error) {
topTenUsers := TopTenUsersResponse{}
params.Set("type", "users")
requestURL, err := buildURL(w.baseURL, "ajax.php", "top10", params)
if err != nil {
return topTenUsers.Response, err
}
err = w.GetJSON(requestURL, &topTenUsers)
if err != nil {
return topTenUsers.Response, err
}
return topTenUsers.Response, checkResponseStatus(topTenUsers.Status, topTenUsers.Error)
}
//GetSimilarArtists retrieves similar artist information using the provided artist id and limit.
func (w *WhatAPI) GetSimilarArtists(id, limit int) (SimilarArtists, error) {
similarArtists := SimilarArtists{}
params := url.Values{}
params.Set("id", strconv.Itoa(id))
params.Set("limit", strconv.Itoa(limit))
requestURL, err := buildURL(w.baseURL, "ajax.php", "similar_artists", params)
if err != nil {
return similarArtists, err
}
err = w.GetJSON(requestURL, &similarArtists)
if err != nil {
return similarArtists, err
}
return similarArtists, nil
}