6 "github.com/Kovensky/go-anidb/misc"
7 "github.com/Kovensky/go-anidb/udp"
17 gob.RegisterName("*github.com/Kovensky/go-anidb.File", &File{})
20 func (f *File) Touch() {
24 func (f *File) IsStale() bool {
28 return time.Now().Sub(f.Cached) > FileCacheDuration
33 func (fid FID) File() *File {
34 f, _ := caches.Get(fileCache).Get(int(fid)).(*File)
38 func ed2kKey(ed2k string, size int64) string {
39 return fmt.Sprintf("%s-%016x", ed2k, size)
42 func ed2kCache(f *File) {
45 defer ed2kFidLock.Unlock()
46 ed2kFidMap[ed2kKey(f.Ed2kHash, f.Filesize)] = f.FID
50 // Prefetches the Anime, Episode and Group that this
51 // file is linked to using the given AniDB instance.
53 // Returns a channel where this file will be sent to
54 // when the prefetching is done; if the file is nil,
55 // the channel will return nil.
56 func (f *File) Prefetch(adb *AniDB) <-chan *File {
57 ch := make(chan *File, 1)
60 a := adb.AnimeByID(f.AID)
61 g := adb.GroupByID(f.GID)
71 var ed2kFidMap = map[string]FID{}
72 var ed2kIntent = map[string][]chan *File{}
73 var ed2kFidLock = sync.RWMutex{}
75 func (adb *AniDB) FileByID(fid FID) <-chan *File {
76 ch := make(chan *File, 1)
77 if f := fid.File(); !f.IsStale() {
83 fc := caches.Get(fileCache)
84 ic := make(chan Cacheable, 1)
85 go func() { ch <- (<-ic).(*File); close(ch) }()
86 if fc.Intent(int(fid), ic) {
91 reply := <-adb.udp.SendRecv("FILE",
99 if reply.Error() == nil {
100 f = parseFileResponse(reply)
108 func (adb *AniDB) FileByEd2kSize(ed2k string, size int64) <-chan *File {
109 key := ed2kKey(ed2k, size)
110 ch := make(chan *File, 1)
113 if fid, ok := ed2kFidMap[key]; ok {
114 ed2kFidLock.RUnlock()
115 if f := fid.File(); f != nil {
120 return adb.FileByID(fid)
122 ed2kFidLock.RUnlock()
125 if list, ok := ed2kIntent[key]; ok {
126 ed2kIntent[key] = append(list, ch)
129 ed2kIntent[key] = append(list, ch)
133 reply := <-adb.udp.SendRecv("FILE",
142 if reply.Error() == nil {
143 f = parseFileResponse(reply)
146 caches.Get(fileCache).Set(int(f.FID), f)
147 } else if reply.Code() == 320 { // file not found
149 delete(ed2kFidMap, key)
151 } else if reply.Code() == 322 { // multiple files found
152 panic("Don't know what to do with " + strings.Join(reply.Lines(), "\n"))
156 defer ed2kFidLock.Unlock()
158 for _, ch := range ed2kIntent[key] {
162 delete(ed2kIntent, key)
167 var fileFmask = "77da7fe8"
168 var fileAmask = "00008000"
171 fileStateCRCOK = 1 << iota
181 func sanitizeCodec(codec string) string {
185 case "WMV9 (also WMV3)":
195 func parseFileResponse(reply udpapi.APIReply) *File {
196 if reply.Error() != nil {
199 if reply.Truncated() {
203 parts := strings.Split(reply.Lines()[1], "|")
204 ints := make([]int64, len(parts))
205 for i, p := range parts {
206 ints[i], _ = strconv.ParseInt(parts[i], 10, 64)
207 log.Printf("#%d: %s\n", i, p)
210 // how does epno look like?
211 log.Println("epno: " + parts[23])
213 version := FileVersion(1)
214 switch i := ints[6]; {
215 case i&fileStateV5 != 0:
217 case i&fileStateV4 != 0:
219 case i&fileStateV3 != 0:
221 case i&fileStateV2 != 0:
225 // codecs (parts[13]), bitrates (ints[14]), langs (parts[19])
226 codecs := strings.Split(parts[13], "'")
227 bitrates := strings.Split(parts[14], "'")
228 alangs := strings.Split(parts[19], "'")
229 streams := make([]AudioStream, len(codecs))
230 for i := range streams {
231 br, _ := strconv.ParseInt(bitrates[i], 10, 32)
232 streams[i] = AudioStream{
234 Codec: sanitizeCodec(codecs[i]),
235 Language: Language(alangs[i]),
239 sl := strings.Split(parts[20], "'")
240 slangs := make([]Language, len(sl))
242 slangs[i] = Language(sl[i])
245 depth := int(ints[11])
249 res := strings.Split(parts[17], "x")
250 width, _ := strconv.ParseInt(res[0], 10, 32)
251 height, _ := strconv.ParseInt(res[1], 10, 32)
253 Bitrate: int(ints[16]),
254 Codec: sanitizeCodec(parts[15]),
256 Resolution: image.Rect(0, 0, int(width), int(height)),
266 OtherEpisodes: misc.ParseEpisodeList(parts[4]).Simplify(),
267 Deprecated: ints[5] != 0,
269 CRCMatch: ints[6]&fileStateCRCOK != 0,
270 BadCRC: ints[6]&fileStateCRCERR != 0,
272 Uncensored: ints[6]&fileStateUncensored != 0,
273 Censored: ints[6]&fileStateCensored != 0,
275 Incomplete: video.Resolution.Empty(),
282 Source: FileSource(parts[12]),
284 AudioStreams: streams,
285 SubtitleLanguages: slangs,
287 FileExtension: parts[18],
289 Length: time.Duration(ints[21]) * time.Second,
290 AirDate: time.Unix(ints[22], 0),