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
37 func (e FID) Touch() {}
38 func (e FID) IsStale() bool { return false }
40 func (fid FID) File() *File {
42 if cache.Get(&f, "fid", fid) == nil {
48 type ed2kCache struct {
53 func (c *ed2kCache) Touch() {
57 func (c *ed2kCache) IsStale() bool {
58 return time.Now().Sub(c.Time) > FileCacheDuration
61 // Prefetches the Anime, Episode and Group that this
62 // file is linked to using the given AniDB instance.
64 // Returns a channel where this file will be sent to
65 // when the prefetching is done; if the file is nil,
66 // the channel will return nil.
67 func (f *File) Prefetch(adb *AniDB) <-chan *File {
68 ch := make(chan *File, 1)
71 a := adb.AnimeByID(f.AID)
72 g := adb.GroupByID(f.GID)
82 func (adb *AniDB) FileByID(fid FID) <-chan *File {
83 keys := []cacheKey{"fid", fid}
85 ch := make(chan *File, 1)
87 ic := make(chan Cacheable, 1)
88 go func() { ch <- (<-ic).(*File); close(ch) }()
89 if intentMap.Intent(ic, keys...) {
93 if !cache.CheckValid(keys...) {
94 intentMap.Notify((*File)(nil), keys...)
98 if f := fid.File(); !f.IsStale() {
99 intentMap.Notify(f, keys...)
104 reply := <-adb.udp.SendRecv("FILE",
112 if reply.Error() == nil {
113 f = parseFileResponse(reply)
114 } else if reply.Code() == 320 {
115 cache.MarkInvalid(keys...)
118 cache.Set(&ed2kCache{FID: f.FID}, "fid", "by-ed2k", f.Ed2kHash, f.Filesize)
119 cache.Set(f, keys...)
121 intentMap.Notify(f, keys...)
126 func (adb *AniDB) FileByEd2kSize(ed2k string, size int64) <-chan *File {
127 keys := []cacheKey{"fid", "by-ed2k", ed2k, size}
129 ch := make(chan *File, 1)
131 ic := make(chan Cacheable, 1)
135 ch <- <-adb.FileByID(fid)
139 if intentMap.Intent(ic, keys...) {
143 if !cache.CheckValid(keys...) {
144 intentMap.Notify(FID(0), keys...)
149 if cache.Get(&ec, keys...) == nil {
150 intentMap.Notify(ec.FID, keys...)
155 reply := <-adb.udp.SendRecv("FILE",
165 if reply.Error() == nil {
166 f = parseFileResponse(reply)
170 cache.Set(&ed2kCache{FID: fid}, keys...)
171 cache.Set(f, "fid", fid)
172 } else if reply.Code() == 320 { // file not found
173 cache.MarkInvalid(keys...)
174 } else if reply.Code() == 322 { // multiple files found
175 panic("Don't know what to do with " + strings.Join(reply.Lines(), "\n"))
178 intentMap.Notify(fid, keys...)
183 var fileFmask = "77da7fe8"
184 var fileAmask = "00008000"
187 fileStateCRCOK = 1 << iota
197 func sanitizeCodec(codec string) string {
201 case "WMV9 (also WMV3)":
211 func parseFileResponse(reply udpapi.APIReply) *File {
212 if reply.Error() != nil {
215 if reply.Truncated() {
219 parts := strings.Split(reply.Lines()[1], "|")
220 ints := make([]int64, len(parts))
221 for i, p := range parts {
222 ints[i], _ = strconv.ParseInt(parts[i], 10, 64)
223 log.Printf("#%d: %s\n", i, p)
226 // how does epno look like?
227 log.Println("epno: " + parts[23])
229 version := FileVersion(1)
230 switch i := ints[6]; {
231 case i&fileStateV5 != 0:
233 case i&fileStateV4 != 0:
235 case i&fileStateV3 != 0:
237 case i&fileStateV2 != 0:
241 // codecs (parts[13]), bitrates (ints[14]), langs (parts[19])
242 codecs := strings.Split(parts[13], "'")
243 bitrates := strings.Split(parts[14], "'")
244 alangs := strings.Split(parts[19], "'")
245 streams := make([]AudioStream, len(codecs))
246 for i := range streams {
247 br, _ := strconv.ParseInt(bitrates[i], 10, 32)
248 streams[i] = AudioStream{
250 Codec: sanitizeCodec(codecs[i]),
251 Language: Language(alangs[i]),
255 sl := strings.Split(parts[20], "'")
256 slangs := make([]Language, len(sl))
258 slangs[i] = Language(sl[i])
261 depth := int(ints[11])
265 res := strings.Split(parts[17], "x")
266 width, _ := strconv.ParseInt(res[0], 10, 32)
267 height, _ := strconv.ParseInt(res[1], 10, 32)
269 Bitrate: int(ints[16]),
270 Codec: sanitizeCodec(parts[15]),
272 Resolution: image.Rect(0, 0, int(width), int(height)),
282 OtherEpisodes: misc.ParseEpisodeList(parts[4]).Simplify(),
283 Deprecated: ints[5] != 0,
285 CRCMatch: ints[6]&fileStateCRCOK != 0,
286 BadCRC: ints[6]&fileStateCRCERR != 0,
288 Uncensored: ints[6]&fileStateUncensored != 0,
289 Censored: ints[6]&fileStateCensored != 0,
291 Incomplete: video.Resolution.Empty(),
298 Source: FileSource(parts[12]),
300 AudioStreams: streams,
301 SubtitleLanguages: slangs,
303 FileExtension: parts[18],
305 Length: time.Duration(ints[21]) * time.Second,
306 AirDate: time.Unix(ints[22], 0),