diff --git a/episode.go b/episode.go index 6392054..07b8616 100644 --- a/episode.go +++ b/episode.go @@ -152,11 +152,16 @@ func (e *Episode) GetFormat(resolution string, subtitle LOCALE, hardsub bool) (* var res *Format for _, format := range formats { if resolution == "worst" || resolution == "best" { - curSplitRes := strings.SplitN(format.Video.Resolution, "x", 1) + if res == nil { + res = format + continue + } + + curSplitRes := strings.SplitN(format.Video.Resolution, "x", 2) curResX, _ := strconv.Atoi(curSplitRes[0]) curResY, _ := strconv.Atoi(curSplitRes[1]) - resSplitRes := strings.SplitN(res.Video.Resolution, "x", 1) + resSplitRes := strings.SplitN(res.Video.Resolution, "x", 2) resResX, _ := strconv.Atoi(resSplitRes[0]) resResY, _ := strconv.Atoi(resSplitRes[1]) diff --git a/utils/sort.go b/utils/sort.go index 628af64..bf9fb77 100644 --- a/utils/sort.go +++ b/utils/sort.go @@ -2,10 +2,45 @@ package utils import ( "github.com/ByteDream/crunchyroll-go" + "sort" "strconv" "strings" ) +// SortEpisodesBySeason sorts the given episodes by their seasons +func SortEpisodesBySeason(episodes []*crunchyroll.Episode) [][]*crunchyroll.Episode { + sortMap := map[string]map[int][]*crunchyroll.Episode{} + + for _, episode := range episodes { + if _, ok := sortMap[episode.SeriesID]; !ok { + sortMap[episode.SeriesID] = map[int][]*crunchyroll.Episode{} + } + if _, ok := sortMap[episode.SeriesID][episode.SeasonNumber]; !ok { + sortMap[episode.SeriesID][episode.SeasonNumber] = make([]*crunchyroll.Episode, 0) + } + sortMap[episode.SeriesID][episode.SeasonNumber] = append(sortMap[episode.SeriesID][episode.SeasonNumber], episode) + } + + var eps [][]*crunchyroll.Episode + for _, series := range sortMap { + keys := make([]int, len(series)) + for seriesNumber := range series { + keys = append(keys, seriesNumber) + } + sort.Ints(keys) + + for _, key := range keys { + es := series[key] + if len(es) > 0 { + sort.Sort(EpisodesByNumber(es)) + eps = append(eps, es) + } + } + } + + return eps +} + // MovieListingsByDuration sorts movie listings by their duration type MovieListingsByDuration []*crunchyroll.MovieListing @@ -32,6 +67,18 @@ func (ebd EpisodesByDuration) Less(i, j int) bool { return ebd[i].DurationMS < ebd[j].DurationMS } +type EpisodesByNumber []*crunchyroll.Episode + +func (ebn EpisodesByNumber) Len() int { + return len(ebn) +} +func (ebn EpisodesByNumber) Swap(i, j int) { + ebn[i], ebn[j] = ebn[j], ebn[i] +} +func (ebn EpisodesByNumber) Less(i, j int) bool { + return ebn[i].EpisodeNumber < ebn[j].EpisodeNumber +} + // FormatsByResolution sorts formats after their resolution type FormatsByResolution []*crunchyroll.Format @@ -42,11 +89,11 @@ func (fbr FormatsByResolution) Swap(i, j int) { fbr[i], fbr[j] = fbr[j], fbr[i] } func (fbr FormatsByResolution) Less(i, j int) bool { - iSplitRes := strings.Split(fbr[i].Video.Resolution, "x") + iSplitRes := strings.SplitN(fbr[i].Video.Resolution, "x", 2) iResX, _ := strconv.Atoi(iSplitRes[0]) iResY, _ := strconv.Atoi(iSplitRes[1]) - jSplitRes := strings.Split(fbr[j].Video.Resolution, "x") + jSplitRes := strings.SplitN(fbr[j].Video.Resolution, "x", 2) jResX, _ := strconv.Atoi(jSplitRes[0]) jResY, _ := strconv.Atoi(jSplitRes[1])