6 "github.com/Kovensky/go-anidb/http"
7 "github.com/Kovensky/go-anidb/misc"
8 "github.com/Kovensky/go-anidb/udp"
16 gob.RegisterName("*github.com/Kovensky/go-anidb.Anime", &Anime{})
17 gob.RegisterName("github.com/Kovensky/go-anidb.AID", AID(0))
20 func (a *Anime) Touch() {
24 func (a *Anime) IsStale() bool {
29 return time.Now().Sub(a.Cached) > AnimeIncompleteCacheDuration
31 return time.Now().Sub(a.Cached) > AnimeCacheDuration
34 // Unique Anime IDentifier.
39 func (e AID) Touch() {}
40 func (e AID) IsStale() bool { return false }
42 // Returns a cached Anime. Returns nil if there is no cached Anime with this AID.
43 func (aid AID) Anime() *Anime {
45 if cache.Get(&a, "aid", aid) == nil {
51 type httpAnimeResponse struct {
56 // Retrieves an Anime by its AID. Uses both HTTP and UDP APIs,
57 // but can work without the UDP API.
58 func (adb *AniDB) AnimeByID(aid AID) <-chan *Anime {
59 keys := []cacheKey{"aid", aid}
60 ch := make(chan *Anime, 1)
62 ic := make(chan Cacheable, 1)
63 go func() { ch <- (<-ic).(*Anime); close(ch) }()
64 if intentMap.Intent(ic, keys...) {
68 if !cache.CheckValid(keys...) {
69 intentMap.Notify((*Anime)(nil), keys...)
75 intentMap.Notify(anime, keys...)
80 httpChan := make(chan httpAnimeResponse, 1)
82 a, err := httpapi.GetAnime(int(aid))
83 httpChan <- httpAnimeResponse{anime: a, err: err}
85 udpChan := adb.udp.SendRecv("ANIME",
91 timeout := time.After(adb.Timeout)
94 anime = &Anime{AID: aid}
96 anime.Incomplete = true
101 for i := 0; i < 2; i++ {
105 case resp := <-httpChan:
110 if !anime.populateFromHTTP(resp.anime) {
111 // HTTP ok but parsing not ok
112 if anime.PrimaryTitle == "" {
113 cache.MarkInvalid(keys...)
121 case reply := <-udpChan:
122 if reply.Code() == 330 {
123 cache.MarkInvalid(keys...)
125 cache.Delete(keys...)
130 anime.Incomplete = !anime.populateFromUDP(reply)
135 if anime.PrimaryTitle != "" {
137 cache.Set(anime, keys...)
139 intentMap.Notify(anime, keys...)
141 intentMap.Notify((*Anime)(nil), keys...)
147 func (a *Anime) populateFromHTTP(reply httpapi.Anime) bool {
148 if reply.Error != "" {
152 if a.AID != AID(reply.ID) {
153 panic(fmt.Sprintf("Requested AID %d different from received AID %d", a.AID, reply.ID))
157 a.Type = AnimeType(reply.Type)
158 // skip episode count since it's unreliable; UDP API handles that
160 // UDP API has more precise versions
162 if st, err := time.Parse(httpapi.DateFormat, reply.StartDate); err == nil {
165 if et, err := time.Parse(httpapi.DateFormat, reply.EndDate); err == nil {
170 for _, title := range reply.Titles {
173 a.PrimaryTitle = title.Title
175 if a.OfficialTitles == nil {
176 a.OfficialTitles = make(UniqueTitleMap)
178 a.OfficialTitles[Language(title.Lang)] = title.Title
180 if a.ShortTitles == nil {
181 a.ShortTitles = make(TitleMap)
183 a.ShortTitles[Language(title.Lang)] = append(a.ShortTitles[Language(title.Lang)], title.Title)
185 if a.Synonyms == nil {
186 a.Synonyms = make(TitleMap)
188 a.Synonyms[Language(title.Lang)] = append(a.Synonyms[Language(title.Lang)], title.Title)
192 a.OfficialURL = reply.URL
193 if reply.Picture != "" {
194 a.Picture = httpapi.AniDBImageBaseURL + reply.Picture
197 a.Description = reply.Description
200 Rating: reply.Ratings.Permanent.Rating,
201 VoteCount: reply.Ratings.Permanent.Count,
203 a.TemporaryVotes = Rating{
204 Rating: reply.Ratings.Temporary.Rating,
205 VoteCount: reply.Ratings.Temporary.Count,
208 Rating: reply.Ratings.Review.Rating,
209 VoteCount: reply.Ratings.Review.Count,
212 a.populateResources(reply.Resources)
214 counts := map[misc.EpisodeType]int{}
216 sort.Sort(reply.Episodes)
217 for _, ep := range reply.Episodes {
218 ad, _ := time.Parse(httpapi.DateFormat, ep.AirDate)
220 titles := make(UniqueTitleMap)
221 for _, title := range ep.Titles {
222 titles[Language(title.Lang)] = title.Title
229 Episode: *misc.ParseEpisode(ep.EpNo.EpNo),
231 Length: time.Duration(ep.Length) * time.Minute,
235 Rating: ep.Rating.Rating,
236 VoteCount: ep.Rating.Votes,
243 a.Episodes = append(a.Episodes, e)
246 a.EpisodeCount = EpisodeCount{
247 RegularCount: counts[misc.EpisodeTypeRegular],
248 SpecialCount: counts[misc.EpisodeTypeSpecial],
249 CreditsCount: counts[misc.EpisodeTypeCredits],
250 OtherCount: counts[misc.EpisodeTypeOther],
251 TrailerCount: counts[misc.EpisodeTypeTrailer],
252 ParodyCount: counts[misc.EpisodeTypeParody],
256 if !a.EndDate.IsZero() {
257 a.TotalEpisodes = a.EpisodeCount.RegularCount
264 func (a *Anime) populateResources(list []httpapi.Resource) {
265 a.Resources.AniDB = Resource{fmt.Sprintf("http://anidb.net/a%v", a.AID)}
267 for _, res := range list {
268 args := make([][]interface{}, len(res.ExternalEntity))
269 for i, e := range res.ExternalEntity {
270 args[i] = make([]interface{}, len(e.Identifiers))
271 for j := range args[i] {
272 args[i][j] = e.Identifiers[j]
278 for i := range res.ExternalEntity {
280 append(a.Resources.ANN, fmt.Sprintf(httpapi.ANNFormat, args[i]...))
282 case 2: // MyAnimeList
283 for i := range res.ExternalEntity {
284 a.Resources.MyAnimeList =
285 append(a.Resources.MyAnimeList, fmt.Sprintf(httpapi.MyAnimeListFormat, args[i]...))
288 for i := range res.ExternalEntity {
289 a.Resources.AnimeNfo =
290 append(a.Resources.AnimeNfo, fmt.Sprintf(httpapi.AnimeNfoFormat, args[i]...))
292 case 4: // OfficialJapanese
293 for _, e := range res.ExternalEntity {
294 for _, url := range e.URL {
295 a.Resources.OfficialJapanese = append(a.Resources.OfficialJapanese, url)
298 case 5: // OfficialEnglish
299 for _, e := range res.ExternalEntity {
300 for _, url := range e.URL {
301 a.Resources.OfficialEnglish = append(a.Resources.OfficialEnglish, url)
304 case 6: // WikipediaEnglish
305 for i := range res.ExternalEntity {
306 a.Resources.WikipediaEnglish =
307 append(a.Resources.WikipediaEnglish, fmt.Sprintf(httpapi.WikiEnglishFormat, args[i]...))
309 case 7: // WikipediaJapanese
310 for i := range res.ExternalEntity {
311 a.Resources.WikipediaJapanese =
312 append(a.Resources.WikipediaJapanese, fmt.Sprintf(httpapi.WikiJapaneseFormat, args[i]...))
314 case 8: // SyoboiSchedule
315 for i := range res.ExternalEntity {
316 a.Resources.SyoboiSchedule =
317 append(a.Resources.SyoboiSchedule, fmt.Sprintf(httpapi.SyoboiFormat, args[i]...))
320 for i := range res.ExternalEntity {
321 a.Resources.AllCinema =
322 append(a.Resources.AllCinema, fmt.Sprintf(httpapi.AllCinemaFormat, args[i]...))
325 for i := range res.ExternalEntity {
327 append(a.Resources.Anison, fmt.Sprintf(httpapi.AnisonFormat, args[i]...))
330 for i := range res.ExternalEntity {
332 append(a.Resources.VNDB, fmt.Sprintf(httpapi.VNDBFormat, args[i]...))
334 case 15: // MaruMegane
335 for i := range res.ExternalEntity {
336 a.Resources.MaruMegane =
337 append(a.Resources.MaruMegane, fmt.Sprintf(httpapi.MaruMeganeFormat, args[i]...))
343 // http://wiki.anidb.info/w/UDP_API_Definition#ANIME:_Retrieve_Anime_Data
344 // Everything that we can't easily get through the HTTP API, or that has more accuracy:
345 // episodes, air date, end date, award list, update date,
346 const animeAMask = "0000980201"
348 func (a *Anime) populateFromUDP(reply udpapi.APIReply) bool {
349 if reply != nil && reply.Error() == nil {
350 parts := strings.Split(reply.Lines()[1], "|")
352 ints := make([]int64, len(parts))
353 for i, p := range parts {
354 ints[i], _ = strconv.ParseInt(p, 10, 32)
357 a.TotalEpisodes = int(ints[0]) // episodes
358 st := time.Unix(ints[1], 0) // air date
359 et := time.Unix(ints[2], 0) // end date
360 aw := strings.Split(parts[3], "'") // award list
361 ut := time.Unix(ints[4], 0) // update date
363 if len(parts[3]) > 0 {
367 // 0 does not actually mean the Epoch here...