]> git.lizzy.rs Git - go-anidb.git/blob - anime.go
anidb: Move EpisodeCount struct to misc
[go-anidb.git] / anime.go
1 package anidb
2
3 import (
4         "github.com/Kovensky/go-anidb/misc"
5         "strconv"
6         "time"
7 )
8
9 // See the constants list for the valid values.
10 type AnimeType string
11
12 const (
13         AnimeTypeTVSeries   = AnimeType("TV Series")  // Anime was a regular TV broadcast series
14         AnimeTypeTVSpecial  = AnimeType("TV Special") // Anime was broadcast on TV as a special
15         AnimeTypeMovie      = AnimeType("Movie")      // Anime was a feature film
16         AnimeTypeOVA        = AnimeType("OVA")        // Anime was released direct-to-video
17         AnimeTypeWeb        = AnimeType("Web")        // Anime was released through online streaming or downloads
18         AnimeTypeMusicVideo = AnimeType("Music Video")
19 )
20
21 type Rating struct {
22         Rating    float32
23         VoteCount int
24 }
25
26 type Resource []string
27
28 // Links to third party websites
29 type Resources struct {
30         AniDB,
31         ANN,
32         MyAnimeList,
33         AnimeNfo,
34         OfficialJapanese,
35         OfficialEnglish,
36         WikipediaEnglish,
37         WikipediaJapanese,
38         SyoboiSchedule,
39         AllCinema,
40         Anison,
41         VNDB,
42         MaruMegane Resource
43 }
44
45 type UniqueTitleMap map[Language]string
46 type TitleMap map[Language][]string
47
48 type Anime struct {
49         AID AID  // The Anime ID.
50         R18 bool // Whether this anime is considered porn.
51
52         Type          AnimeType         // Production/distribution type.
53         TotalEpisodes int               // Total number of regular episodes.
54         EpisodeCount  misc.EpisodeCount // Known numbers of the various types of episodes.
55
56         StartDate time.Time // Date of first episode release, if available.
57         EndDate   time.Time // Date of last episode release, if available.
58
59         PrimaryTitle   string         // The primary title in the database; almost always a romanization of the Japanese title.
60         OfficialTitles UniqueTitleMap // The official title for each language.
61         ShortTitles    TitleMap       // Shortcut titles used for searches
62         Synonyms       TitleMap       // Synonyms for each language, or unofficial titles
63
64         OfficialURL string // URL for original official website.
65         Picture     string // URL for the page picture on AniDB.
66
67         Description string
68
69         Votes          Rating // Votes from people who watched the whole thing.
70         TemporaryVotes Rating // Votes from people who are still watching this.
71         Reviews        Rating // Votes from reviewers.
72
73         Episodes []*Episode // List of episodes.
74
75         Awards    []string
76         Resources Resources
77
78         Incomplete bool      // Set if the UDP API part of the query failed.
79         Updated    time.Time // When the data was last modified in the server.
80         Cached     time.Time // When the data was retrieved from the server.
81 }
82
83 // Convenience method that runs AnimeByID on the result of
84 // SearchAnime.
85 func (adb *AniDB) AnimeByName(name string) <-chan *Anime {
86         return adb.AnimeByID(SearchAnime(name))
87 }
88
89 // Convenience method that runs AnimeByID on the result of
90 // SearchAnimeFold.
91 func (adb *AniDB) AnimeByNameFold(name string) <-chan *Anime {
92         return adb.AnimeByID(SearchAnimeFold(name))
93 }
94
95 // Returns a list of all Episodes in this Anime's Episodes list
96 // that are contained by the given EpisodeContainer.
97 func (a *Anime) EpisodeList(c misc.EpisodeContainer) (eps []*Episode) {
98         if a == nil || c == nil {
99                 return nil
100         }
101
102         for i, e := range a.Episodes {
103                 if c.ContainsEpisodes(&e.Episode) {
104                         eps = append(eps, a.Episodes[i])
105                 }
106         }
107         return
108 }
109
110 // Searches for the given Episode in this Anime's Episodes list
111 // and returns the match.
112 //
113 // Returns nil if there is no match.
114 func (a *Anime) Episode(ep *misc.Episode) *Episode {
115         switch list := a.EpisodeList(ep); len(list) {
116         case 0:
117                 return nil
118         case 1:
119                 return list[0]
120         default:
121                 panic("Single episode search returned more than one result")
122         }
123 }
124
125 // Convenience method that parses the string into an Episode
126 // before doing the Episode search.
127 func (a *Anime) EpisodeByString(name string) *Episode {
128         return a.Episode(misc.ParseEpisode(name))
129 }
130
131 // Convenience method that parses the int into an Episode
132 // before doing the Episode search.
133 //
134 // Only works with regular (i.e. not special, etc) episodes.
135 func (a *Anime) EpisodeByNumber(number int) *Episode {
136         return a.EpisodeByString(strconv.Itoa(number))
137 }
138
139 func (a *Anime) EpisodeByEID(eid EID) *Episode {
140         if a == nil {
141                 return nil
142         }
143         for _, ep := range a.Episodes {
144                 if ep.EID == eid {
145                         return ep
146                 }
147         }
148         return nil
149 }