From: Diogo Franco (Kovensky) Date: Tue, 16 Jul 2013 17:06:13 +0000 (-0300) Subject: misc: CountEpisodes and convenience conversion functions X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=3a452d896ced08450d4914f0d3f098d51158fddb;p=go-anidb.git misc: CountEpisodes and convenience conversion functions --- diff --git a/misc/episodelist.go b/misc/episodelist.go index a22bef4..7f7d0ab 100644 --- a/misc/episodelist.go +++ b/misc/episodelist.go @@ -17,6 +17,14 @@ type EpisodeCount struct { type EpisodeList []*EpisodeRange +func EpisodeToList(ep *Episode) EpisodeList { + return RangesToList(EpisodeToRange(ep)) +} + +func RangesToList(ranges ...*EpisodeRange) EpisodeList { + return EpisodeList(ranges) +} + // Converts the EpisodeList into the AniDB API list format. func (el EpisodeList) String() string { scales := map[EpisodeType]int{} @@ -134,6 +142,37 @@ func (el EpisodeList) Simplify() EpisodeList { return nl } +func (el EpisodeList) CountEpisodes() (ec EpisodeCount) { + for _, er := range el { + var c *int + switch er.Type { + case EpisodeTypeRegular: + c = &ec.RegularCount + case EpisodeTypeSpecial: + c = &ec.SpecialCount + case EpisodeTypeCredits: + c = &ec.CreditsCount + case EpisodeTypeOther: + c = &ec.OtherCount + case EpisodeTypeTrailer: + c = &ec.TrailerCount + case EpisodeTypeParody: + c = &ec.ParodyCount + default: + continue + } + if *c < 0 { + continue + } + if er.End == nil { + *c = -1 + continue + } + *c += er.End.Number - er.Start.Number + } + return +} + func (el EpisodeList) Len() int { return len(el) } diff --git a/misc/episoderange.go b/misc/episoderange.go index 0f11b0c..fb91caa 100644 --- a/misc/episoderange.go +++ b/misc/episoderange.go @@ -12,6 +12,14 @@ type EpisodeRange struct { End *Episode // The end of the range; may be nil, which represents an endless range } +func EpisodeToRange(ep *Episode) *EpisodeRange { + return &EpisodeRange{ + Type: ep.Type, + Start: ep, + End: ep, + } +} + // Converts the EpisodeRange into AniDB API range format. func (er *EpisodeRange) String() string { return er.Format(er.scale())