crunchy-cli/watchlist.go

101 lines
2.9 KiB
Go

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
)
// WatchlistContentType represents a filter type to filter Crunchyroll.Watchlist entries if they're series or movies.
type WatchlistContentType string
const (
WatchlistContentSeries WatchlistContentType = "series"
WatchlistContentMovies = "movie_listing"
)
// WatchlistOptions represents options for receiving the user watchlist.
type WatchlistOptions struct {
// OrderAsc specified whether the results should be order ascending or descending.
OrderAsc bool
// 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 WatchlistContentType
}
// 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.OrderAsc {
values.Set("order", "asc")
} else {
values.Set("order", "desc")
}
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.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"`
}