From f635bf1a2e1783e88fec015a25627d58c5698108 Mon Sep 17 00:00:00 2001 From: bytedream Date: Wed, 18 May 2022 21:47:17 +0200 Subject: [PATCH] Add available function to check if season streams are available --- episode.go | 4 ++-- season.go | 12 +++++++++++- url.go | 1 + 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/episode.go b/episode.go index 7d477ce..3a2e403 100644 --- a/episode.go +++ b/episode.go @@ -112,8 +112,8 @@ func EpisodeFromID(crunchy *Crunchyroll, id string) (*Episode, error) { // Every episode in a season (should) have the same audio locale, // so if you want to get the audio locale of a season, just call // this method on the first episode of the season. -// Will fail if no streams are available, thus use Available to -// prevent any misleading errors. +// Will fail if no streams are available, thus use Episode.Available +// to prevent any misleading errors. func (e *Episode) AudioLocale() (LOCALE, error) { streams, err := e.Streams() if err != nil { diff --git a/season.go b/season.go index f5ec08e..0bb8623 100644 --- a/season.go +++ b/season.go @@ -70,8 +70,9 @@ func SeasonFromID(crunchy *Crunchyroll, id string) (*Season, error) { } // AudioLocale returns the audio locale of the season. +// Will fail if no streams are available, thus use Season.Available +// to prevent any misleading errors. func (s *Season) AudioLocale() (LOCALE, error) { - // TODO: Add a function like Episode.Available to prevent this from returning an unwanted error when the account is non-premium episodes, err := s.Episodes() if err != nil { return "", err @@ -79,6 +80,15 @@ func (s *Season) AudioLocale() (LOCALE, error) { return episodes[0].AudioLocale() } +// Available returns if downloadable streams for this season are available. +func (s *Season) Available() (bool, error) { + episodes, err := s.Episodes() + if err != nil { + return false, err + } + return episodes[0].Available(), nil +} + // Episodes returns all episodes which are available for the season. func (s *Season) Episodes() (episodes []*Episode, err error) { if s.children != nil { diff --git a/url.go b/url.go index c84a3b9..0f5bb25 100644 --- a/url.go +++ b/url.go @@ -21,6 +21,7 @@ func (c *Crunchyroll) ExtractEpisodesFromUrl(url string, audio ...LOCALE) ([]*Ep } for _, season := range seasons { if audio != nil { + locale, err := season.AudioLocale() if err != nil { return nil, err