Add available function to check if episode streams are available

This commit is contained in:
bytedream 2022-05-16 22:24:21 +02:00
parent 6c476df24e
commit 0ffae4ddda
3 changed files with 15 additions and 0 deletions

View file

@ -112,6 +112,8 @@ func EpisodeFromID(crunchy *Crunchyroll, id string) (*Episode, error) {
// Every episode in a season (should) have the same audio locale, // 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 // so if you want to get the audio locale of a season, just call
// this method on the first episode of the season. // this method on the first episode of the season.
// Will fail if no streams are available, thus use Available to
// prevent any misleading errors.
func (e *Episode) AudioLocale() (LOCALE, error) { func (e *Episode) AudioLocale() (LOCALE, error) {
streams, err := e.Streams() streams, err := e.Streams()
if err != nil { if err != nil {
@ -120,6 +122,11 @@ func (e *Episode) AudioLocale() (LOCALE, error) {
return streams[0].AudioLocale, nil return streams[0].AudioLocale, nil
} }
// Available returns if downloadable streams for this episodes are available.
func (e *Episode) Available() bool {
return e.crunchy.Config.Premium || !e.IsPremiumOnly
}
// GetFormat returns the format which matches the given resolution and subtitle locale. // GetFormat returns the format which matches the given resolution and subtitle locale.
func (e *Episode) GetFormat(resolution string, subtitle LOCALE, hardsub bool) (*Format, error) { func (e *Episode) GetFormat(resolution string, subtitle LOCALE, hardsub bool) (*Format, error) {
streams, err := e.Streams() streams, err := e.Streams()

5
url.go
View file

@ -49,6 +49,11 @@ func (c *Crunchyroll) ExtractEpisodesFromUrl(url string, audio ...LOCALE) ([]*Ep
} }
for _, episode := range episodes { for _, episode := range episodes {
// if no episode streams are available, calling episode.AudioLocale
// will result in an unwanted error
if !episode.Available() {
continue
}
locale, err := episode.AudioLocale() locale, err := episode.AudioLocale()
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -52,6 +52,9 @@ func SortEpisodesByAudio(episodes []*crunchyroll.Episode) (map[crunchyroll.LOCAL
var wg sync.WaitGroup var wg sync.WaitGroup
var lock sync.Mutex var lock sync.Mutex
for _, episode := range episodes { for _, episode := range episodes {
if !episode.Available() {
continue
}
episode := episode episode := episode
wg.Add(1) wg.Add(1)
go func() { go func() {