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