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 the 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...)
116 if resp.anime.Error == "Anime not found" {
118 cache.Delete(keys...)
126 case reply := <-udpChan:
127 if reply.Code() == 330 {
128 cache.MarkInvalid(keys...)
130 cache.Delete(keys...)
135 anime.Incomplete = !anime.populateFromUDP(reply)
140 if anime.PrimaryTitle != "" {
142 cache.Set(anime, keys...)
144 intentMap.Notify(anime, keys...)
146 intentMap.Notify((*Anime)(nil), keys...)
152 func (a *Anime) populateFromHTTP(reply httpapi.Anime) bool {
153 if reply.Error != "" {
157 if a.AID != AID(reply.ID) {
158 panic(fmt.Sprintf("Requested AID %d different from received AID %d", a.AID, reply.ID))
162 a.Type = AnimeType(reply.Type)
163 // skip episode count since it's unreliable; UDP API handles that
165 // UDP API has more precise versions
167 if st, err := time.Parse(httpapi.DateFormat, reply.StartDate); err == nil {
170 if et, err := time.Parse(httpapi.DateFormat, reply.EndDate); err == nil {
175 for _, title := range reply.Titles {
178 a.PrimaryTitle = title.Title
180 if a.OfficialTitles == nil {
181 a.OfficialTitles = make(UniqueTitleMap)
183 a.OfficialTitles[Language(title.Lang)] = title.Title
185 if a.ShortTitles == nil {
186 a.ShortTitles = make(TitleMap)
188 a.ShortTitles[Language(title.Lang)] = append(a.ShortTitles[Language(title.Lang)], title.Title)
190 if a.Synonyms == nil {
191 a.Synonyms = make(TitleMap)
193 a.Synonyms[Language(title.Lang)] = append(a.Synonyms[Language(title.Lang)], title.Title)
197 a.OfficialURL = reply.URL
198 if reply.Picture != "" {
199 a.Picture = httpapi.AniDBImageBaseURL + reply.Picture
202 a.Description = reply.Description
205 Rating: reply.Ratings.Permanent.Rating,
206 VoteCount: reply.Ratings.Permanent.Count,
208 a.TemporaryVotes = Rating{
209 Rating: reply.Ratings.Temporary.Rating,
210 VoteCount: reply.Ratings.Temporary.Count,
213 Rating: reply.Ratings.Review.Rating,
214 VoteCount: reply.Ratings.Review.Count,
217 a.populateResources(reply.Resources)
219 counts := map[misc.EpisodeType]int{}
221 sort.Sort(reply.Episodes)
222 for _, ep := range reply.Episodes {
223 ad, _ := time.Parse(httpapi.DateFormat, ep.AirDate)
225 titles := make(UniqueTitleMap)
226 for _, title := range ep.Titles {
227 titles[Language(title.Lang)] = title.Title
234 Episode: *misc.ParseEpisode(ep.EpNo.EpNo),
236 Length: time.Duration(ep.Length) * time.Minute,
240 Rating: ep.Rating.Rating,
241 VoteCount: ep.Rating.Votes,
248 a.Episodes = append(a.Episodes, e)
251 a.EpisodeCount = EpisodeCount{
252 RegularCount: counts[misc.EpisodeTypeRegular],
253 SpecialCount: counts[misc.EpisodeTypeSpecial],
254 CreditsCount: counts[misc.EpisodeTypeCredits],
255 OtherCount: counts[misc.EpisodeTypeOther],
256 TrailerCount: counts[misc.EpisodeTypeTrailer],
257 ParodyCount: counts[misc.EpisodeTypeParody],
261 if !a.EndDate.IsZero() {
262 a.TotalEpisodes = a.EpisodeCount.RegularCount
269 func (a *Anime) populateResources(list []httpapi.Resource) {
270 a.Resources.AniDB = Resource{fmt.Sprintf("http://anidb.net/a%v", a.AID)}
272 for _, res := range list {
273 args := make([][]interface{}, len(res.ExternalEntity))
274 for i, e := range res.ExternalEntity {
275 args[i] = make([]interface{}, len(e.Identifiers))
276 for j := range args[i] {
277 args[i][j] = e.Identifiers[j]
283 for i := range res.ExternalEntity {
285 append(a.Resources.ANN, fmt.Sprintf(httpapi.ANNFormat, args[i]...))
287 case 2: // MyAnimeList
288 for i := range res.ExternalEntity {
289 a.Resources.MyAnimeList =
290 append(a.Resources.MyAnimeList, fmt.Sprintf(httpapi.MyAnimeListFormat, args[i]...))
293 for i := range res.ExternalEntity {
294 a.Resources.AnimeNfo =
295 append(a.Resources.AnimeNfo, fmt.Sprintf(httpapi.AnimeNfoFormat, args[i]...))
297 case 4: // OfficialJapanese
298 for _, e := range res.ExternalEntity {
299 for _, url := range e.URL {
300 a.Resources.OfficialJapanese = append(a.Resources.OfficialJapanese, url)
303 case 5: // OfficialEnglish
304 for _, e := range res.ExternalEntity {
305 for _, url := range e.URL {
306 a.Resources.OfficialEnglish = append(a.Resources.OfficialEnglish, url)
309 case 6: // WikipediaEnglish
310 for i := range res.ExternalEntity {
311 a.Resources.WikipediaEnglish =
312 append(a.Resources.WikipediaEnglish, fmt.Sprintf(httpapi.WikiEnglishFormat, args[i]...))
314 case 7: // WikipediaJapanese
315 for i := range res.ExternalEntity {
316 a.Resources.WikipediaJapanese =
317 append(a.Resources.WikipediaJapanese, fmt.Sprintf(httpapi.WikiJapaneseFormat, args[i]...))
319 case 8: // SyoboiSchedule
320 for i := range res.ExternalEntity {
321 a.Resources.SyoboiSchedule =
322 append(a.Resources.SyoboiSchedule, fmt.Sprintf(httpapi.SyoboiFormat, args[i]...))
325 for i := range res.ExternalEntity {
326 a.Resources.AllCinema =
327 append(a.Resources.AllCinema, fmt.Sprintf(httpapi.AllCinemaFormat, args[i]...))
330 for i := range res.ExternalEntity {
332 append(a.Resources.Anison, fmt.Sprintf(httpapi.AnisonFormat, args[i]...))
335 for i := range res.ExternalEntity {
337 append(a.Resources.VNDB, fmt.Sprintf(httpapi.VNDBFormat, args[i]...))
339 case 15: // MaruMegane
340 for i := range res.ExternalEntity {
341 a.Resources.MaruMegane =
342 append(a.Resources.MaruMegane, fmt.Sprintf(httpapi.MaruMeganeFormat, args[i]...))
348 // http://wiki.anidb.info/w/UDP_API_Definition#ANIME:_Retrieve_Anime_Data
349 // Everything that we can't easily get through the HTTP API, or that has more accuracy:
350 // episodes, air date, end date, award list, update date,
351 const animeAMask = "0000980201"
353 func (a *Anime) populateFromUDP(reply udpapi.APIReply) bool {
354 if reply != nil && reply.Error() == nil {
355 parts := strings.Split(reply.Lines()[1], "|")
357 ints := make([]int64, len(parts))
358 for i, p := range parts {
359 ints[i], _ = strconv.ParseInt(p, 10, 32)
362 a.TotalEpisodes = int(ints[0]) // episodes
363 st := time.Unix(ints[1], 0) // air date
364 et := time.Unix(ints[2], 0) // end date
365 aw := strings.Split(parts[3], "'") // award list
366 ut := time.Unix(ints[4], 0) // update date
368 if len(parts[3]) > 0 {
372 // 0 does not actually mean the Epoch here...