This repository has been archived by the owner on Sep 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwatchlist.go
100 lines (83 loc) · 2.86 KB
/
watchlist.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
package crunchyroll
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
)
// WatchlistLanguageType represents a filter type to filter Crunchyroll.Watchlist entries after sub or dub.
type WatchlistLanguageType int
const (
WatchlistLanguageSubbed WatchlistLanguageType = iota + 1
WatchlistLanguageDubbed
)
// WatchlistOrderType represents how Crunchyroll.Watchlist entries should be ordered.
type WatchlistOrderType string
const (
WatchlistOrderAsc = "asc"
WatchlistOrderDesc = "desc"
)
// WatchlistOptions represents options for receiving the user watchlist.
type WatchlistOptions struct {
// Order specified whether the results should be order ascending or descending.
Order WatchlistOrderType
// OnlyFavorites specifies whether only episodes which are marked as favorite should be returned.
OnlyFavorites bool
// LanguageType specifies whether returning episodes should be only subbed or dubbed.
LanguageType WatchlistLanguageType
// ContentType specified whether returning videos should only be series episodes or movies.
// But tbh all movies I've searched on crunchy were flagged as series too, so this
// parameter is kinda useless.
ContentType MediaType
}
// Watchlist returns the watchlist entries for the currently logged in user.
func (c *Crunchyroll) Watchlist(options WatchlistOptions, limit uint) ([]*WatchlistEntry, error) {
values := url.Values{}
if options.Order == "" {
options.Order = WatchlistOrderDesc
}
values.Set("order", string(options.Order))
if options.OnlyFavorites {
values.Set("only_favorites", "true")
}
switch options.LanguageType {
case WatchlistLanguageSubbed:
values.Set("is_subbed", "true")
case WatchlistLanguageDubbed:
values.Set("is_dubbed", "true")
}
values.Set("n", strconv.Itoa(int(limit)))
values.Set("locale", string(c.Locale))
endpoint := fmt.Sprintf("https://beta-api.crunchyroll.com/content/v1/%s/watchlist?%s", c.Config.AccountID, values.Encode())
resp, err := c.request(endpoint, http.MethodGet)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var jsonBody map[string]interface{}
json.NewDecoder(resp.Body).Decode(&jsonBody)
var watchlistEntries []*WatchlistEntry
if err := decodeMapToStruct(jsonBody["items"], &watchlistEntries); err != nil {
return nil, err
}
for _, entry := range watchlistEntries {
switch entry.Panel.Type {
case WatchlistEntryEpisode:
entry.Panel.EpisodeMetadata.crunchy = c
case WatchlistEntrySeries:
entry.Panel.SeriesMetadata.crunchy = c
}
}
return watchlistEntries, nil
}
// WatchlistEntry contains information about an entry on the watchlist.
type WatchlistEntry struct {
Panel Panel `json:"panel"`
New bool `json:"new"`
NewContent bool `json:"new_content"`
IsFavorite bool `json:"is_favorite"`
NeverWatched bool `json:"never_watched"`
CompleteStatus bool `json:"complete_status"`
Playahead uint `json:"playahead"`
}