5 "github.com/Kovensky/go-anidb/misc"
6 "github.com/Kovensky/go-anidb/udp"
15 gob.RegisterName("*github.com/Kovensky/go-anidb.File", &File{})
16 gob.RegisterName("*github.com/Kovensky/go-anidb.ed2kCache", &ed2kCache{})
17 gob.RegisterName("github.com/Kovensky/go-anidb.FID", FID(0))
20 func (f *File) Touch() {
24 func (f *File) IsStale() bool {
29 return time.Now().Sub(f.Cached) > FileIncompleteCacheDuration
31 return time.Now().Sub(f.Cached) > FileCacheDuration
38 func (e FID) Touch() {}
39 func (e FID) IsStale() bool { return false }
41 func (fid FID) File() *File {
43 if cache.Get(&f, "fid", fid) == nil {
49 type ed2kCache struct {
54 func (c *ed2kCache) Touch() {
58 func (c *ed2kCache) IsStale() bool {
59 return time.Now().Sub(c.Time) > FileCacheDuration
62 // Prefetches the Anime, Episode and Group that this
63 // file is linked to using the given AniDB instance.
65 // Returns a channel where this file will be sent to
66 // when the prefetching is done; if the file is nil,
67 // the channel will return nil.
68 func (f *File) Prefetch(adb *AniDB) <-chan *File {
69 ch := make(chan *File, 1)
72 a := adb.AnimeByID(f.AID)
73 g := adb.GroupByID(f.GID)
83 // Retrieves a File by its FID. Uses the UDP API.
84 func (adb *AniDB) FileByID(fid FID) <-chan *File {
85 keys := []cacheKey{"fid", fid}
87 ch := make(chan *File, 1)
89 ic := make(chan Cacheable, 1)
90 go func() { ch <- (<-ic).(*File); close(ch) }()
91 if intentMap.Intent(ic, keys...) {
95 if !cache.CheckValid(keys...) {
96 intentMap.Notify((*File)(nil), keys...)
102 intentMap.Notify(f, keys...)
107 reply := <-adb.udp.SendRecv("FILE",
114 if reply.Error() == nil {
115 f = parseFileResponse(reply)
117 cache.Set(&ed2kCache{FID: f.FID}, "fid", "by-ed2k", f.Ed2kHash, f.Filesize)
118 cache.Set(f, keys...)
119 } else if reply.Code() == 320 {
120 cache.MarkInvalid(keys...)
123 intentMap.Notify(f, keys...)
128 // Retrieves a File by its Ed2kHash + Filesize combination. Uses the UDP API.
129 func (adb *AniDB) FileByEd2kSize(ed2k string, size int64) <-chan *File {
130 keys := []cacheKey{"fid", "by-ed2k", ed2k, size}
132 ch := make(chan *File, 1)
134 ic := make(chan Cacheable, 1)
138 ch <- <-adb.FileByID(fid)
142 if intentMap.Intent(ic, keys...) {
146 if !cache.CheckValid(keys...) {
147 intentMap.Notify(FID(0), keys...)
154 if cache.Get(&ec, keys...) == nil && !ec.IsStale() {
155 intentMap.Notify(ec.FID, keys...)
161 reply := <-adb.udp.SendRecv("FILE",
170 if reply.Error() == nil {
171 f = parseFileResponse(reply)
175 cache.Set(&ed2kCache{FID: fid}, keys...)
176 cache.Set(f, "fid", fid)
177 } else if reply.Code() == 320 { // file not found
178 cache.MarkInvalid(keys...)
179 } else if reply.Code() == 322 { // multiple files found
180 panic("Don't know what to do with " + strings.Join(reply.Lines(), "\n"))
183 intentMap.Notify(fid, keys...)
188 var fileFmask = "77da7fe8"
189 var fileAmask = "00008000"
192 fileStateCRCOK = 1 << iota
202 func sanitizeCodec(codec string) string {
206 case "WMV9 (also WMV3)":
216 func parseFileResponse(reply udpapi.APIReply) *File {
217 if reply.Error() != nil {
220 if reply.Truncated() {
224 parts := strings.Split(reply.Lines()[1], "|")
225 ints := make([]int64, len(parts))
226 for i, p := range parts {
227 ints[i], _ = strconv.ParseInt(parts[i], 10, 64)
228 log.Printf("#%d: %s\n", i, p)
231 // how does epno look like?
232 log.Println("epno: " + parts[23])
234 version := FileVersion(1)
235 switch i := ints[6]; {
236 case i&fileStateV5 != 0:
238 case i&fileStateV4 != 0:
240 case i&fileStateV3 != 0:
242 case i&fileStateV2 != 0:
246 // codecs (parts[13]), bitrates (ints[14]), langs (parts[19])
247 codecs := strings.Split(parts[13], "'")
248 bitrates := strings.Split(parts[14], "'")
249 alangs := strings.Split(parts[19], "'")
250 streams := make([]AudioStream, len(codecs))
251 for i := range streams {
252 br, _ := strconv.ParseInt(bitrates[i], 10, 32)
253 streams[i] = AudioStream{
255 Codec: sanitizeCodec(codecs[i]),
256 Language: Language(alangs[i]),
260 sl := strings.Split(parts[20], "'")
261 slangs := make([]Language, len(sl))
263 slangs[i] = Language(sl[i])
266 depth := int(ints[11])
270 res := strings.Split(parts[17], "x")
271 width, _ := strconv.ParseInt(res[0], 10, 32)
272 height, _ := strconv.ParseInt(res[1], 10, 32)
274 Bitrate: int(ints[16]),
275 Codec: sanitizeCodec(parts[15]),
277 Resolution: image.Rect(0, 0, int(width), int(height)),
287 OtherEpisodes: misc.ParseEpisodeList(parts[4]).Simplify(),
288 Deprecated: ints[5] != 0,
290 CRCMatch: ints[6]&fileStateCRCOK != 0,
291 BadCRC: ints[6]&fileStateCRCERR != 0,
293 Uncensored: ints[6]&fileStateUncensored != 0,
294 Censored: ints[6]&fileStateCensored != 0,
296 Incomplete: video.Resolution.Empty(),
303 Source: FileSource(parts[12]),
305 AudioStreams: streams,
306 SubtitleLanguages: slangs,
308 FileExtension: parts[18],
310 Length: time.Duration(ints[21]) * time.Second,
311 AirDate: time.Unix(ints[22], 0),