mirror of
https://github.com/crunchy-labs/crunchy-cli.git
synced 2026-01-21 12:12:00 -06:00
Move functions into their own, separate files & add docs
This commit is contained in:
parent
d1859b4c25
commit
ec872d8c86
12 changed files with 681 additions and 638 deletions
88
watchlist.go
88
watchlist.go
|
|
@ -1,5 +1,93 @@
|
|||
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"`
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue