/
trakt.go
495 lines (441 loc) · 14.3 KB
/
trakt.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package apiexternal
import (
"context"
"fmt"
"log"
"net/http"
"strconv"
"time"
"github.com/RussellLuo/slidingwindow"
"golang.org/x/oauth2"
"golang.org/x/time/rate"
)
type TraktMovies struct {
}
type TraktMovieAnticipated struct {
ListCount int `json:"list_count"`
Movie TraktMovie `json:"movie"`
}
type TraktMovieTrending struct {
Watchers int `json:"watchers"`
Movie TraktMovie `json:"movie"`
}
type TraktUserList struct {
Rank int `json:"rank"`
Id int `json:"id"`
Notes string `json:"notes"`
Type string `json:"type"`
Movie TraktMovie `json:"movie"`
Serie TraktSerie `json:"show"`
}
type TraktMovie struct {
Title string `json:"title"`
Year int `json:"year"`
Ids struct {
Trakt int `json:"trakt"`
Slug string `json:"slug"`
Imdb string `json:"imdb"`
Tmdb int `json:"tmdb"`
} `json:"ids"`
}
type TraktSerieTrending struct {
Watchers int `json:"watchers"`
Serie TraktSerie `json:"show"`
}
type TraktSerieAnticipated struct {
ListCount int `json:"list_count"`
Serie TraktSerie `json:"show"`
}
type TraktSerieSeason struct {
Number int `json:"number"`
Ids struct {
Trakt int `json:"trakt"`
Tvdb int `json:"tvdb"`
Tvrage int `json:"tvrage"`
Imdb string `json:"imdb"`
Tmdb int `json:"tmdb"`
} `json:"ids"`
}
type TraktSerieSeasonEpisodes struct {
Season int `json:"season"`
Episode int `json:"number"`
Title string `json:"title"`
Ids struct {
Trakt int `json:"trakt"`
Tvdb int `json:"tvdb"`
Tvrage int `json:"tvrage"`
Imdb string `json:"imdb"`
Tmdb int `json:"tmdb"`
} `json:"ids"`
EpisodeAbs int `json:"number_abs"`
Overview string `json:"overview"`
Rating float32 `json:"rating"`
Votes int `json:"votes"`
Comments int `json:"comment_count"`
AvailableTranslations []string `json:"available_translations"`
Runtime int `json:"runtime"`
FirstAired time.Time `json:"first_aired"`
}
type TraktSerie struct {
Title string `json:"title"`
Year int `json:"year"`
Ids struct {
Trakt int `json:"trakt"`
Slug string `json:"slug"`
Tvdb int `json:"tvdb"`
Tvrage int `json:"tvrage"`
Imdb string `json:"imdb"`
Tmdb int `json:"tmdb"`
} `json:"ids"`
}
type TraktSerieData struct {
Title string `json:"title"`
Year int `json:"year"`
Ids struct {
Trakt int `json:"trakt"`
Slug string `json:"slug"`
Tvdb int `json:"tvdb"`
Tvrage int `json:"tvrage"`
Imdb string `json:"imdb"`
Tmdb int `json:"tmdb"`
} `json:"ids"`
Overview string `json:"overview"`
FirstAired time.Time `json:"first_aired"`
Runtime int `json:"runtime"`
Certification string `json:"certification"`
Network string `json:"network"`
Country string `json:"country"`
Trailer string `json:"trailer"`
Homepage string `json:"homepage"`
Status string `json:"status"`
Rating float32 `json:"rating"`
Votes int `json:"votes"`
Comments int `json:"comment_count"`
Language string `json:"language"`
AvailableTranslations []string `json:"available_translations"`
Genres []string `json:"genres"`
AiredEpisodes int `json:"aired_episodes"`
}
type TraktShowAliases struct {
Title string `json:"title"`
Country string `json:"country"`
}
type TraktMovieAliases struct {
Title string `json:"title"`
Country string `json:"country"`
}
type TraktMovieExtend struct {
Title string `json:"title"`
Year int `json:"year"`
Ids struct {
Trakt int `json:"trakt"`
Slug string `json:"slug"`
Imdb string `json:"imdb"`
Tmdb int `json:"tmdb"`
} `json:"ids"`
Tagline string `json:"tagline"`
Overview string `json:"overview"`
Released string `json:"released"`
Runtime int `json:"runtime"`
Country string `json:"country"`
Trailer string `json:"trailer"`
Homepage string `json:"homepage"`
Status string `json:"status"`
Rating float32 `json:"rating"`
Votes int `json:"votes"`
Comments int `json:"comment_count"`
Language string `json:"language"`
AvailableTranslations []string `json:"available_translations"`
Genres []string `json:"genres"`
Certification string `json:"certification"`
}
type TraktClient struct {
ApiKey string
ClientID string
ClientSecret string
Client *RLHTTPClient
Auth *oauth2.Config
Token *oauth2.Token
}
var TraktApi TraktClient
func NewTraktClient(clientid string, clientsecret string, token oauth2.Token, seconds int, calls int) {
if seconds == 0 {
seconds = 1
}
if calls == 0 {
calls = 1
}
rl := rate.NewLimiter(rate.Every(time.Duration(seconds)*time.Second), calls) // 50 request every 10 seconds
limiter, _ := slidingwindow.NewLimiter(time.Duration(seconds)*time.Second, int64(calls), func() (slidingwindow.Window, slidingwindow.StopFunc) { return slidingwindow.NewLocalWindow() })
conf := &oauth2.Config{
ClientID: clientid,
ClientSecret: clientsecret,
RedirectURL: "http://localhost:9090",
Endpoint: oauth2.Endpoint{
AuthURL: "https://api.trakt.tv/oauth/authorize",
TokenURL: "https://api.trakt.tv/oauth/token",
},
}
TraktApi = TraktClient{
ApiKey: clientid,
ClientID: clientid,
ClientSecret: clientsecret,
Client: NewClient(rl, limiter),
Auth: conf,
Token: &token}
}
func (t TraktClient) GetMoviePopular(limit int) ([]TraktMovie, error) {
url := "https://api.trakt.tv/movies/popular"
if limit >= 1 {
url = url + "?limit=" + strconv.Itoa(limit)
} else {
limit = 10
}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("trakt-api-version", "2")
req.Header.Add("trakt-api-key", t.ApiKey)
var result []TraktMovie
err := t.Client.DoJson(req, &result)
if err != nil {
return []TraktMovie{}, err
}
//json.Unmarshal(responseData, &result)
return result, nil
}
func (t TraktClient) GetMovieTrending(limit int) ([]TraktMovieTrending, error) {
url := "https://api.trakt.tv/movies/trending"
if limit >= 1 {
url = url + "?limit=" + strconv.Itoa(limit)
} else {
limit = 10
}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("trakt-api-version", "2")
req.Header.Add("trakt-api-key", t.ApiKey)
var result []TraktMovieTrending
err := t.Client.DoJson(req, &result)
if err != nil {
return []TraktMovieTrending{}, err
}
//json.Unmarshal(responseData, &result)
return result, nil
}
func (t TraktClient) GetMovieAnticipated(limit int) ([]TraktMovieAnticipated, error) {
url := "https://api.trakt.tv/movies/anticipated"
if limit >= 1 {
url = url + "?limit=" + strconv.Itoa(limit)
} else {
limit = 10
}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("trakt-api-version", "2")
req.Header.Add("trakt-api-key", t.ApiKey)
var result []TraktMovieAnticipated
err := t.Client.DoJson(req, &result)
if err != nil {
return []TraktMovieAnticipated{}, err
}
//json.Unmarshal(responseData, &result)
return result, nil
}
func (t TraktClient) GetMovieAliases(movieid string) ([]TraktMovieAliases, error) {
url := "https://api.trakt.tv/movies/" + movieid + "/aliases"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("trakt-api-version", "2")
req.Header.Add("trakt-api-key", t.ApiKey)
var result []TraktMovieAliases
err := t.Client.DoJson(req, &result)
if err != nil {
return []TraktMovieAliases{}, err
}
//json.Unmarshal(responseData, &result)
return result, nil
}
func (t TraktClient) GetMovie(movieid string) (TraktMovieExtend, error) {
url := "https://api.trakt.tv/movies/" + movieid + "?extended=full"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("trakt-api-version", "2")
req.Header.Add("trakt-api-key", t.ApiKey)
var result TraktMovieExtend
err := t.Client.DoJson(req, &result)
if err != nil {
return TraktMovieExtend{}, err
}
//json.Unmarshal(responseData, &result)
return result, nil
}
func (t TraktClient) GetSerie(movieid string) (TraktSerieData, error) {
url := "https://api.trakt.tv/shows/" + movieid + "?extended=full"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("trakt-api-version", "2")
req.Header.Add("trakt-api-key", t.ApiKey)
var result TraktSerieData
err := t.Client.DoJson(req, &result)
if err != nil {
return TraktSerieData{}, err
}
//json.Unmarshal(responseData, &result)
return result, nil
}
func (t TraktClient) GetSerieAliases(movieid string) ([]TraktShowAliases, error) {
url := "https://api.trakt.tv/shows/" + movieid + "/aliases"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("trakt-api-version", "2")
req.Header.Add("trakt-api-key", t.ApiKey)
var result []TraktShowAliases
err := t.Client.DoJson(req, &result)
if err != nil {
return []TraktShowAliases{}, err
}
//json.Unmarshal(responseData, &result)
return result, nil
}
func (t TraktClient) GetSerieSeasons(movieid string) ([]TraktSerieSeason, error) {
url := "https://api.trakt.tv/shows/" + movieid + "/seasons"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("trakt-api-version", "2")
req.Header.Add("trakt-api-key", t.ApiKey)
var result []TraktSerieSeason
err := t.Client.DoJson(req, &result)
if err != nil {
return []TraktSerieSeason{}, err
}
//json.Unmarshal(responseData, &result)
return result, nil
}
func (t TraktClient) GetSerieSeasonEpisodes(movieid string, season int) ([]TraktSerieSeasonEpisodes, error) {
url := "https://api.trakt.tv/shows/" + movieid + "/seasons/" + strconv.Itoa(season) + "?extended=full"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("trakt-api-version", "2")
req.Header.Add("trakt-api-key", t.ApiKey)
var result []TraktSerieSeasonEpisodes
err := t.Client.DoJson(req, &result)
if err != nil {
return []TraktSerieSeasonEpisodes{}, err
}
//json.Unmarshal(responseData, &result)
return result, nil
}
func (t TraktClient) GetUserList(username string, listname string, listtype string, limit int) ([]TraktUserList, error) {
url := "https://api.trakt.tv/users/" + username + "/lists/" + listname + "/items/" + listtype
if limit >= 1 {
url = url + "?limit=" + strconv.Itoa(limit)
}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Content-Type", "application/json")
if t.Token.AccessToken != "" {
req.Header.Add("Authorization", "Bearer "+t.Token.AccessToken)
}
req.Header.Add("trakt-api-version", "2")
req.Header.Add("trakt-api-key", t.ApiKey)
var result []TraktUserList
err := t.Client.DoJson(req, &result)
if err != nil {
return []TraktUserList{}, err
}
//json.Unmarshal(responseData, &result)
return result, nil
}
func (t TraktClient) GetSeriePopular(limit int) ([]TraktSerie, error) {
url := "https://api.trakt.tv/shows/popular"
if limit >= 1 {
url = url + "?limit=" + strconv.Itoa(limit)
} else {
limit = 10
}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("trakt-api-version", "2")
req.Header.Add("trakt-api-key", t.ApiKey)
var result []TraktSerie
err := t.Client.DoJson(req, &result)
if err != nil {
return []TraktSerie{}, err
}
//json.Unmarshal(responseData, &result)
return result, nil
}
func (t TraktClient) GetSerieTrending(limit int) ([]TraktSerieTrending, error) {
url := "https://api.trakt.tv/shows/trending"
if limit >= 1 {
url = url + "?limit=" + strconv.Itoa(limit)
} else {
limit = 10
}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("trakt-api-version", "2")
req.Header.Add("trakt-api-key", t.ApiKey)
var result []TraktSerieTrending
err := t.Client.DoJson(req, &result)
if err != nil {
return []TraktSerieTrending{}, err
}
//json.Unmarshal(responseData, &result)
return result, nil
}
func (t TraktClient) GetSerieAnticipated(limit int) ([]TraktSerieAnticipated, error) {
url := "https://api.trakt.tv/shows/anticipated"
if limit >= 1 {
url = url + "?limit=" + strconv.Itoa(limit)
} else {
limit = 10
}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("trakt-api-version", "2")
req.Header.Add("trakt-api-key", t.ApiKey)
var result []TraktSerieAnticipated
err := t.Client.DoJson(req, &result)
if err != nil {
return []TraktSerieAnticipated{}, err
}
//json.Unmarshal(responseData, &result)
return result, nil
}
func (t TraktClient) GetAuthUrl() string {
// Redirect user to consent page to ask for permission
// for the scopes specified above.
url := t.Auth.AuthCodeURL("state", oauth2.AccessTypeOffline)
fmt.Printf("Visit the URL for the auth dialog: %v", url)
return url
}
func (t TraktClient) GetAuthToken(clientcode string) *oauth2.Token {
// Redirect user to consent page to ask for permission
// for the scopes specified above.
ctx := context.Background()
tok, err := t.Auth.Exchange(ctx, clientcode)
if err != nil {
log.Fatal(err)
}
return tok
}
func (t TraktClient) GetUserListAuth(username string, listname string, listtype string, limit int) ([]TraktUserList, error) {
url := "https://api.trakt.tv/users/" + username + "/lists/" + listname + "/items/" + listtype
if limit >= 1 {
url = url + "?limit=" + strconv.Itoa(limit)
}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Content-Type", "application/json")
if t.Token.AccessToken != "" {
req.Header.Add("Authorization", "Bearer "+t.Token.AccessToken)
}
req.Header.Add("trakt-api-version", "2")
req.Header.Add("trakt-api-key", t.ApiKey)
var result []TraktUserList
err := t.Client.DoJson(req, &result)
if err != nil {
return []TraktUserList{}, err
}
//json.Unmarshal(responseData, &result)
return result, nil
}