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 func (adb *AniDB) FileByID(fid FID) <-chan *File {
84 keys := []cacheKey{"fid", fid}
86 ch := make(chan *File, 1)
88 ic := make(chan Cacheable, 1)
89 go func() { ch <- (<-ic).(*File); close(ch) }()
90 if intentMap.Intent(ic, keys...) {
94 if !cache.CheckValid(keys...) {
95 intentMap.Notify((*File)(nil), keys...)
99 if f := fid.File(); !f.IsStale() {
100 intentMap.Notify(f, keys...)
105 reply := <-adb.udp.SendRecv("FILE",
113 if reply.Error() == nil {
114 f = parseFileResponse(reply)
115 } else if reply.Code() == 320 {
116 cache.MarkInvalid(keys...)
119 cache.Set(&ed2kCache{FID: f.FID}, "fid", "by-ed2k", f.Ed2kHash, f.Filesize)
120 cache.Set(f, keys...)
122 intentMap.Notify(f, keys...)
127 func (adb *AniDB) FileByEd2kSize(ed2k string, size int64) <-chan *File {
128 keys := []cacheKey{"fid", "by-ed2k", ed2k, size}
130 ch := make(chan *File, 1)
132 ic := make(chan Cacheable, 1)
136 ch <- <-adb.FileByID(fid)
140 if intentMap.Intent(ic, keys...) {
144 if !cache.CheckValid(keys...) {
145 intentMap.Notify(FID(0), keys...)
150 if cache.Get(&ec, keys...) == nil {
151 intentMap.Notify(ec.FID, keys...)
156 reply := <-adb.udp.SendRecv("FILE",
166 if reply.Error() == nil {
167 f = parseFileResponse(reply)
171 cache.Set(&ed2kCache{FID: fid}, keys...)
172 cache.Set(f, "fid", fid)
173 } else if reply.Code() == 320 { // file not found
174 cache.MarkInvalid(keys...)
175 } else if reply.Code() == 322 { // multiple files found
176 panic("Don't know what to do with " + strings.Join(reply.Lines(), "\n"))
179 intentMap.Notify(fid, keys...)
184 var fileFmask = "77da7fe8"
185 var fileAmask = "00008000"
188 fileStateCRCOK = 1 << iota
198 func sanitizeCodec(codec string) string {
202 case "WMV9 (also WMV3)":
212 func parseFileResponse(reply udpapi.APIReply) *File {
213 if reply.Error() != nil {
216 if reply.Truncated() {
220 parts := strings.Split(reply.Lines()[1], "|")
221 ints := make([]int64, len(parts))
222 for i, p := range parts {
223 ints[i], _ = strconv.ParseInt(parts[i], 10, 64)
224 log.Printf("#%d: %s\n", i, p)
227 // how does epno look like?
228 log.Println("epno: " + parts[23])
230 version := FileVersion(1)
231 switch i := ints[6]; {
232 case i&fileStateV5 != 0:
234 case i&fileStateV4 != 0:
236 case i&fileStateV3 != 0:
238 case i&fileStateV2 != 0:
242 // codecs (parts[13]), bitrates (ints[14]), langs (parts[19])
243 codecs := strings.Split(parts[13], "'")
244 bitrates := strings.Split(parts[14], "'")
245 alangs := strings.Split(parts[19], "'")
246 streams := make([]AudioStream, len(codecs))
247 for i := range streams {
248 br, _ := strconv.ParseInt(bitrates[i], 10, 32)
249 streams[i] = AudioStream{
251 Codec: sanitizeCodec(codecs[i]),
252 Language: Language(alangs[i]),
256 sl := strings.Split(parts[20], "'")
257 slangs := make([]Language, len(sl))
259 slangs[i] = Language(sl[i])
262 depth := int(ints[11])
266 res := strings.Split(parts[17], "x")
267 width, _ := strconv.ParseInt(res[0], 10, 32)
268 height, _ := strconv.ParseInt(res[1], 10, 32)
270 Bitrate: int(ints[16]),
271 Codec: sanitizeCodec(parts[15]),
273 Resolution: image.Rect(0, 0, int(width), int(height)),
283 OtherEpisodes: misc.ParseEpisodeList(parts[4]).Simplify(),
284 Deprecated: ints[5] != 0,
286 CRCMatch: ints[6]&fileStateCRCOK != 0,
287 BadCRC: ints[6]&fileStateCRCERR != 0,
289 Uncensored: ints[6]&fileStateUncensored != 0,
290 Censored: ints[6]&fileStateCensored != 0,
292 Incomplete: video.Resolution.Empty(),
299 Source: FileSource(parts[12]),
301 AudioStreams: streams,
302 SubtitleLanguages: slangs,
304 FileExtension: parts[18],
306 Length: time.Duration(ints[21]) * time.Second,
307 AirDate: time.Unix(ints[22], 0),