Add available function to check if season streams are available

This commit is contained in:
bytedream 2022-05-18 21:47:17 +02:00
parent 43be2eee14
commit f635bf1a2e
3 changed files with 14 additions and 3 deletions

View file

@ -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 {

View file

@ -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 {

1
url.go
View file

@ -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