4 "github.com/Kovensky/go-fscache"
10 // Gets the Files that the given Group has released for the given
11 // Episode. Convenience method that calls FilesByGID.
12 func (adb *AniDB) FilesByGroup(ep *Episode, g *Group) <-chan *File {
13 ch := make(chan *File, 1)
14 if ep == nil || g == nil {
19 return adb.FilesByGID(ep, g.GID)
22 // Gets the Files that the Group (given by its ID) has released
23 // for the given Episode. The returned channel may return multiple
24 // (or no) Files. Uses the UDP API.
26 // On API error (offline, etc), the first *File returned is nil,
27 // followed by cached files (which may also be nil).
28 func (adb *AniDB) FilesByGID(ep *Episode, gid GID) <-chan *File {
29 ch := make(chan *File, 10)
31 fidChan := adb.FIDsByGID(ep, gid)
34 chs := []<-chan *File{}
35 for fid := range fidChan {
36 chs = append(chs, adb.FileByID(fid))
38 for _, c := range chs {
48 // Gets the FIDs that the Group (given by its ID) has released
49 // for the given Episode. The returned channel may return multiple
50 // (or no) FIDs. Uses the UDP API.
52 // On API error (offline, etc), the first *File returned is nil,
53 // followed by cached files (which may also be nil).
54 func (adb *AniDB) FIDsByGID(ep *Episode, gid GID) <-chan FID {
55 key := []fscache.CacheKey{"fid", "by-eid-gid", ep.EID, gid}
57 ch := make(chan FID, 10)
59 if ep == nil || gid < 1 {
65 ic := make(chan notification, 1)
72 if intentMap.Intent(ic, key...) {
76 if !Cache.IsValid(InvalidKeyCacheDuration, key...) {
77 intentMap.Close(key...)
82 switch ts, err := Cache.Get(&fids, key...); {
83 case err == nil && time.Now().Sub(ts) < FileCacheDuration:
84 is := intentMap.LockIntent(key...)
86 defer intentMap.Free(is, key...)
89 for _, fid := range fids {
97 reply := <-adb.udp.SendRecv("FILE",
101 "epno": ep.Episode.String(),
106 is := intentMap.LockIntent(key...)
107 defer intentMap.Free(is, key...)
109 switch reply.Code() {
112 if adb.parseFileResponse(&f, reply, true) {
114 CacheSet(&fids, key...)
118 is.NotifyClose(f.FID)
120 is.NotifyClose(FID(0))
124 parts := strings.Split(reply.Lines()[1], "|")
125 fids = make([]FID, len(parts))
126 for i := range parts {
127 id, _ := strconv.ParseInt(parts[i], 10, 32)
131 CacheSet(&fids, key...)
133 Cache.SetInvalid(key...)
141 for _, fid := range fids {