X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=anime.go;h=393f94eefe03a4bf5666400a2e7e174276e54cbe;hb=59d77e55f0aaa0643c71a5066399f3d7ce740de6;hp=a8873b24759e15f20e41cfc750d9cd0affc01022;hpb=2c1ef9d904daf8108f4e9ac2de8264b6a558b3d9;p=go-anidb.git diff --git a/anime.go b/anime.go index a8873b2..393f94e 100644 --- a/anime.go +++ b/anime.go @@ -1,6 +1,7 @@ package anidb import ( + "fmt" "github.com/Kovensky/go-anidb/misc" "strconv" "time" @@ -49,9 +50,9 @@ type Anime struct { AID AID // The Anime ID. R18 bool // Whether this anime is considered porn. - Type AnimeType // Production/distribution type. - TotalEpisodes int // Total number of regular episodes. - EpisodeCount EpisodeCount // Known numbers of the various types of episodes. + Type AnimeType // Production/distribution type. + TotalEpisodes int // Total number of regular episodes. + EpisodeCount misc.EpisodeCount // Known numbers of the various types of episodes. StartDate time.Time // Date of first episode release, if available. EndDate time.Time // Date of last episode release, if available. @@ -70,7 +71,7 @@ type Anime struct { TemporaryVotes Rating // Votes from people who are still watching this. Reviews Rating // Votes from reviewers. - Episodes Episodes // List of episodes. + Episodes []*Episode // List of episodes. Awards []string Resources Resources @@ -80,15 +81,6 @@ type Anime struct { Cached time.Time // When the data was retrieved from the server. } -type EpisodeCount struct { - RegularCount int - SpecialCount int - CreditsCount int - OtherCount int - TrailerCount int - ParodyCount int -} - // Convenience method that runs AnimeByID on the result of // SearchAnime. func (adb *AniDB) AnimeByName(name string) <-chan *Anime { @@ -101,17 +93,34 @@ func (adb *AniDB) AnimeByNameFold(name string) <-chan *Anime { return adb.AnimeByID(SearchAnimeFold(name)) } +// Returns a list of all Episodes in this Anime's Episodes list +// that are contained by the given EpisodeContainer. +func (a *Anime) EpisodeList(c misc.EpisodeContainer) (eps []*Episode) { + if a == nil || c == nil { + return nil + } + + for i, e := range a.Episodes { + if c.ContainsEpisodes(&e.Episode) { + eps = append(eps, a.Episodes[i]) + } + } + return +} + // Searches for the given Episode in this Anime's Episodes list // and returns the match. // // Returns nil if there is no match. func (a *Anime) Episode(ep *misc.Episode) *Episode { - for i, e := range a.Episodes { - if ep.ContainsEpisodes(&e.Episode) { - return &a.Episodes[i] - } + switch list := a.EpisodeList(ep); len(list) { + case 0: + return nil + case 1: + return list[0] + default: + panic(fmt.Sprintf("Single episode search returned more than one result (wanted %v, got %v)", ep, list)) } - return nil } // Convenience method that parses the string into an Episode @@ -127,3 +136,15 @@ func (a *Anime) EpisodeByString(name string) *Episode { func (a *Anime) EpisodeByNumber(number int) *Episode { return a.EpisodeByString(strconv.Itoa(number)) } + +func (a *Anime) EpisodeByEID(eid EID) *Episode { + if a == nil { + return nil + } + for _, ep := range a.Episodes { + if ep.EID == eid { + return ep + } + } + return nil +}