Added function to sort episode by audio

This commit is contained in:
bytedream 2022-03-06 01:04:27 +01:00
parent 6fb6b3c03c
commit 5f5ec38585

View file

@ -5,6 +5,7 @@ import (
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
"sync"
) )
// SortEpisodesBySeason sorts the given episodes by their seasons. // SortEpisodesBySeason sorts the given episodes by their seasons.
@ -42,6 +43,44 @@ func SortEpisodesBySeason(episodes []*crunchyroll.Episode) [][]*crunchyroll.Epis
return eps return eps
} }
// SortEpisodesByAudio sort the given episodes by their audio locale
func SortEpisodesByAudio(episodes []*crunchyroll.Episode) (map[crunchyroll.LOCALE][]*crunchyroll.Episode, error) {
eps := map[crunchyroll.LOCALE][]*crunchyroll.Episode{}
errChan := make(chan error)
var wg sync.WaitGroup
var lock sync.Mutex
for _, episode := range episodes {
episode := episode
wg.Add(1)
go func() {
defer wg.Done()
audioLocale, err := episode.AudioLocale()
if err != nil {
errChan <- err
return
}
lock.Lock()
defer lock.Unlock()
if _, ok := eps[audioLocale]; !ok {
eps[audioLocale] = make([]*crunchyroll.Episode, 0)
}
eps[audioLocale] = append(eps[audioLocale], episode)
}()
}
go func() {
wg.Wait()
errChan <- nil
}()
if err := <-errChan; err != nil {
return nil, err
}
return eps, nil
}
// MovieListingsByDuration sorts movie listings by their duration // MovieListingsByDuration sorts movie listings by their duration
type MovieListingsByDuration []*crunchyroll.MovieListing type MovieListingsByDuration []*crunchyroll.MovieListing