package anidb
import (
- "encoding/gob"
+ "fmt"
"github.com/Kovensky/go-anidb/misc"
"github.com/Kovensky/go-anidb/udp"
+ "github.com/Kovensky/go-fscache"
"image"
"log"
+ "regexp"
+ "sort"
"strconv"
"strings"
"time"
)
-func init() {
- gob.RegisterName("*github.com/Kovensky/go-anidb.File", &File{})
- gob.RegisterName("*github.com/Kovensky/go-anidb.ed2kCache", &ed2kCache{})
- gob.RegisterName("github.com/Kovensky/go-anidb.FID", FID(0))
-}
+var _ cacheable = &File{}
-func (f *File) Touch() {
- f.Cached = time.Now()
+func (f *File) setCachedTS(ts time.Time) {
+ f.Cached = ts
}
func (f *File) IsStale() bool {
return time.Now().Sub(f.Cached) > FileCacheDuration
}
-type FID int
-
-// make FID Cacheable
+func cacheFile(f *File) {
+ CacheSet(f.FID, "fid", "by-ed2k", f.Ed2kHash, f.Filesize)
+ CacheSet(f, "fid", f.FID)
+}
-func (e FID) Touch() {}
-func (e FID) IsStale() bool { return false }
+type FID int
func (fid FID) File() *File {
var f File
- if cache.Get(&f, "fid", fid) == nil {
+ if CacheGet(&f, "fid", fid) == nil {
return &f
}
return nil
}
-type ed2kCache struct {
- FID
- Time time.Time
-}
-
-func (c *ed2kCache) Touch() {
- c.Time = time.Now()
-}
-
-func (c *ed2kCache) IsStale() bool {
- return time.Now().Sub(c.Time) > FileCacheDuration
-}
-
// Prefetches the Anime, Episode and Group that this
// file is linked to using the given AniDB instance.
//
return ch
}
-// Retrieves a File by its FID.
+// Retrieves a File by its FID. Uses the UDP API.
func (adb *AniDB) FileByID(fid FID) <-chan *File {
- keys := []cacheKey{"fid", fid}
+ key := []fscache.CacheKey{"fid", fid}
ch := make(chan *File, 1)
- ic := make(chan Cacheable, 1)
+ if fid < 1 {
+ ch <- nil
+ close(ch)
+ return ch
+ }
+
+ ic := make(chan notification, 1)
go func() { ch <- (<-ic).(*File); close(ch) }()
- if intentMap.Intent(ic, keys...) {
+ if intentMap.Intent(ic, key...) {
return ch
}
- if !cache.CheckValid(keys...) {
- intentMap.Notify((*File)(nil), keys...)
+ if !Cache.IsValid(InvalidKeyCacheDuration, key...) {
+ intentMap.NotifyClose((*File)(nil), key...)
return ch
}
f := fid.File()
if !f.IsStale() {
- intentMap.Notify(f, keys...)
+ intentMap.NotifyClose(f, key...)
return ch
}
})
if reply.Error() == nil {
- f = parseFileResponse(reply)
+ f = adb.parseFileResponse(reply, false)
- cache.Set(&ed2kCache{FID: f.FID}, "fid", "by-ed2k", f.Ed2kHash, f.Filesize)
- cache.Set(f, keys...)
+ cacheFile(f)
} else if reply.Code() == 320 {
- cache.MarkInvalid(keys...)
+ Cache.SetInvalid(key...)
}
- intentMap.Notify(f, keys...)
+ intentMap.NotifyClose(f, key...)
}()
return ch
}
-// Retrieves a File by its Ed2kHash + Filesize combination.
+var validEd2kHash = regexp.MustCompile(`\A[:xdigit:]{32}\z`)
+
+// Retrieves a File by its Ed2kHash + Filesize combination. Uses the UDP API.
func (adb *AniDB) FileByEd2kSize(ed2k string, size int64) <-chan *File {
- keys := []cacheKey{"fid", "by-ed2k", ed2k, size}
+ key := []fscache.CacheKey{"fid", "by-ed2k", ed2k, size}
ch := make(chan *File, 1)
- ic := make(chan Cacheable, 1)
+ if size < 1 || !validEd2kHash.MatchString(ed2k) {
+ ch <- nil
+ close(ch)
+ return ch
+ }
+ // AniDB always uses lower case hashes
+ ed2k = strings.ToLower(ed2k)
+
+ ic := make(chan notification, 1)
go func() {
fid := (<-ic).(FID)
if fid > 0 {
}
close(ch)
}()
- if intentMap.Intent(ic, keys...) {
+ if intentMap.Intent(ic, key...) {
return ch
}
- if !cache.CheckValid(keys...) {
- intentMap.Notify(FID(0), keys...)
+ if !Cache.IsValid(InvalidKeyCacheDuration, key...) {
+ intentMap.NotifyClose(FID(0), key...)
return ch
}
fid := FID(0)
- var ec ed2kCache
- if cache.Get(&ec, keys...) == nil; !ec.IsStale() {
- intentMap.Notify(ec.FID, keys...)
+ switch ts, err := Cache.Get(&fid, key...); {
+ case err != nil && time.Now().Sub(ts) < FileCacheDuration:
+ intentMap.NotifyClose(fid, key...)
return ch
}
- fid = ec.FID
go func() {
reply := <-adb.udp.SendRecv("FILE",
var f *File
if reply.Error() == nil {
- f = parseFileResponse(reply)
+ f = adb.parseFileResponse(reply, false)
fid = f.FID
- cache.Set(&ed2kCache{FID: fid}, keys...)
- cache.Set(f, "fid", fid)
+ cacheFile(f)
} else if reply.Code() == 320 { // file not found
- cache.MarkInvalid(keys...)
+ Cache.SetInvalid(key...)
} else if reply.Code() == 322 { // multiple files found
panic("Don't know what to do with " + strings.Join(reply.Lines(), "\n"))
}
- intentMap.Notify(fid, keys...)
+ intentMap.NotifyClose(fid, key...)
}()
return ch
}
return codec
}
-func parseFileResponse(reply udpapi.APIReply) *File {
+func (adb *AniDB) parseFileResponse(reply udpapi.APIReply, calledFromFIDsByGID bool) *File {
if reply.Error() != nil {
return nil
}
parts := strings.Split(reply.Lines()[1], "|")
ints := make([]int64, len(parts))
for i, p := range parts {
- ints[i], _ = strconv.ParseInt(parts[i], 10, 64)
- log.Printf("#%d: %s\n", i, p)
+ ints[i], _ = strconv.ParseInt(p, 10, 64)
}
- // how does epno look like?
- log.Println("epno: " + parts[23])
+ partial := false
+
+ rels := strings.Split(parts[4], "'")
+ relList := make([]EID, 0, len(parts[4]))
+ related := make(RelatedEpisodes, len(parts[4]))
+ for _, rel := range rels {
+ r := strings.Split(rel, ",")
+ if len(r) < 2 {
+ continue
+ }
+
+ eid, _ := strconv.ParseInt(r[0], 10, 32)
+ pct, _ := strconv.ParseInt(r[1], 10, 32)
+ relList = append(relList, EID(eid))
+ related[EID(eid)] = float32(pct) / 100
+
+ if pct != 100 {
+ partial = true
+ }
+ }
+
+ epno := misc.ParseEpisodeList(parts[23])
+ fid := FID(ints[0])
+ aid := AID(ints[1])
+ eid := EID(ints[2])
+ gid := GID(ints[3])
+
+ if !epno[0].Start.ContainsEpisodes(epno[0].End) || len(epno) > 1 || len(relList) > 0 {
+ // epno is broken -- we need to sanitize it
+ thisEp := <-adb.EpisodeByID(eid)
+ bad := false
+ if thisEp != nil {
+ parts := make([]string, 1, len(relList)+1)
+ parts[0] = thisEp.Episode.String()
+
+ // everything after this SHOULD be cache hits now, unless this is somehow
+ // linked with an EID from a different anime (*stares at Haruhi*).
+ // We don't want to use eps from different AIDs anyway, so that makes
+ // the job easier.
+
+ // We check if the related episodes are all in sequence from this one.
+ // If they are, we build a new epno with the sequence. Otherwise,
+ // our epno will only have the primary episode.
+
+ // gather the episode numbers
+ for _, eid := range relList {
+ if ep := eid.Episode(); ep != nil && ep.AID == thisEp.AID {
+ parts = append(parts, ep.Episode.String())
+ } else {
+ bad = true
+ break
+ }
+ }
+
+ test := misc.EpisodeList{}
+ // only if we didn't break the loop
+ if !bad {
+ test = misc.ParseEpisodeList(strings.Join(parts, ","))
+ }
+
+ if partial {
+ if calledFromFIDsByGID {
+ epno = test
+ log.Printf("UDP!!! FID %d is only part of episode %s with no complementary files", fid, epno)
+ } else if len(test) == 1 && test[0].Start.Number == test[0].End.Number {
+ fids := []int{}
+
+ for fid := range adb.FIDsByGID(thisEp, gid) {
+ fids = append(fids, int(fid))
+ }
+ if len(fids) >= 1 && fids[0] == 0 {
+ fids = fids[1:]
+ // Only entry was API error
+ if len(fids) == 0 {
+ return nil
+ }
+ }
+ sort.Sort(sort.IntSlice(fids))
+ idx := sort.SearchInts(fids, int(fid))
+ if idx == len(fids) {
+ panic(fmt.Sprintf("FID %d couldn't locate itself", fid))
+ }
+
+ epno = test
+
+ // equate pointers
+ epno[0].End = epno[0].Start
+
+ epno[0].Start.Parts = len(fids)
+ epno[0].Start.Part = idx
+ } else {
+ panic(fmt.Sprintf("Don't know what to do with partial episode %s (EID %d)", test, eid))
+ }
+ } else {
+ // if they're all in sequence, then we'll only have a single range in the list
+ if len(test) == 1 {
+ epno = test
+ } else {
+ // use only the primary epno then
+ epno = misc.ParseEpisodeList(thisEp.Episode.String())
+ }
+ }
+ }
+ }
version := FileVersion(1)
switch i := ints[6]; {
}
return &File{
- FID: FID(ints[0]),
+ FID: fid,
+
+ AID: aid,
+ EID: eid,
+ GID: gid,
- AID: AID(ints[1]),
- EID: EID(ints[2]),
- GID: GID(ints[3]),
+ EpisodeNumber: epno,
- OtherEpisodes: misc.ParseEpisodeList(parts[4]).Simplify(),
- Deprecated: ints[5] != 0,
+ RelatedEpisodes: related,
+ Deprecated: ints[5] != 0,
CRCMatch: ints[6]&fileStateCRCOK != 0,
BadCRC: ints[6]&fileStateCRCERR != 0,