forked from Jessidhia/go-anidb
/
mylistanimecache.go
210 lines (168 loc) · 4.15 KB
/
mylistanimecache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package anidb
import (
"github.com/Kovensky/go-anidb/misc"
"github.com/Kovensky/go-anidb/udp"
"github.com/Kovensky/go-fscache"
"strings"
"time"
)
func (a *MyListAnime) setCachedTS(ts time.Time) {
a.Cached = ts
}
func (a *MyListAnime) IsStale() bool {
if a == nil {
return true
}
return time.Now().Sub(a.Cached) > MyListCacheDuration
}
var _ cacheable = &MyListAnime{}
func (uid UID) MyListAnime(aid AID) *MyListAnime {
var a MyListAnime
if CacheGet(&a, "mylist-anime", uid, aid) == nil {
return &a
}
return nil
}
func (u *User) MyListAnime(aid AID) *MyListAnime {
if u != nil {
return u.UID.MyListAnime(aid)
}
return nil
}
func (a *Anime) MyList(adb *AniDB) <-chan *MyListAnime {
ch := make(chan *MyListAnime, 1)
if a == nil {
ch <- nil
close(ch)
return ch
}
go func() {
user := <-adb.GetCurrentUser()
if user == nil || user.UID < 1 {
ch <- nil
close(ch)
return
}
ch <- <-adb.MyListAnime(a.AID)
close(ch)
}()
return ch
}
func (adb *AniDB) MyListAnime(aid AID) <-chan *MyListAnime {
ch := make(chan *MyListAnime, 1)
if aid < 1 {
ch <- nil
close(ch)
return ch
}
go func() {
user := <-adb.GetCurrentUser()
if user == nil || user.UID < 1 {
ch <- nil
close(ch)
return
}
key := []fscache.CacheKey{"mylist-anime", user.UID, aid}
ic := make(chan notification, 2)
go func() { ch <- (<-ic).(*MyListAnime); close(ch) }()
if intentMap.Intent(ic, key...) {
return
}
if !Cache.IsValid(InvalidKeyCacheDuration, key...) {
intentMap.NotifyClose((*MyListAnime)(nil), key...)
return
}
entry := user.UID.MyListAnime(aid)
if !entry.IsStale() {
intentMap.NotifyClose(entry, key...)
return
}
reply := <-adb.udp.SendRecv("MYLIST", paramMap{"aid": aid})
switch reply.Code() {
case 221:
r := adb.parseMylistReply(reply) // caches
// we have only a single file added for this anime -- construct a fake 312 struct
entry = &MyListAnime{AID: aid}
ep := <-adb.EpisodeByID(r.EID)
list := misc.EpisodeToList(&ep.Episode)
entry.EpisodesWithState = MyListStateMap{
r.MyListState: list,
}
if !r.DateWatched.IsZero() {
entry.WatchedEpisodes = list
}
entry.EpisodesPerGroup = GroupEpisodes{
r.GID: list,
}
case 312:
entry = adb.parseMylistAnime(reply)
entry.AID = aid
case 321:
Cache.SetInvalid(key...)
}
CacheSet(entry, key...)
intentMap.NotifyClose(entry, key...)
}()
return ch
}
func (adb *AniDB) UserMyListAnime(uid UID, aid AID) <-chan *MyListAnime {
key := []fscache.CacheKey{"mylist-anime", uid, aid}
ch := make(chan *MyListAnime, 1)
if uid < 1 || aid < 1 {
ch <- nil
close(ch)
return ch
}
ic := make(chan notification, 1)
go func() { ch <- (<-ic).(*MyListAnime); close(ch) }()
if intentMap.Intent(ic, key...) {
return ch
}
if !Cache.IsValid(InvalidKeyCacheDuration, key...) {
intentMap.NotifyClose((*MyListAnime)(nil), key...)
return ch
}
entry := uid.MyListAnime(aid)
if !entry.IsStale() {
intentMap.NotifyClose(entry, key...)
return ch
}
go func() {
user := <-adb.GetCurrentUser()
if user.UID != uid { // we can't query other users' lists from API
intentMap.NotifyClose(entry, key...)
return
}
intentMap.NotifyClose(<-adb.MyListAnime(aid), key...)
}()
return ch
}
func (adb *AniDB) parseMylistAnime(reply udpapi.APIReply) *MyListAnime {
if reply.Code() != 312 {
return nil
}
parts := strings.Split(reply.Lines()[1], "|")
// Everything from index 7 on is pairs of group name on odd positions and episode list on even
var groupParts []string
if len(parts) > 7 {
groupParts = parts[7:]
}
groupMap := make(GroupEpisodes, len(groupParts)/2)
for i := 0; i+1 < len(groupParts); i += 2 {
g := <-adb.GroupByName(groupParts[i])
if g == nil {
continue
}
groupMap[g.GID] = misc.ParseEpisodeList(groupParts[i+1])
}
return &MyListAnime{
EpisodesWithState: MyListStateMap{
MyListStateUnknown: misc.ParseEpisodeList(parts[2]),
MyListStateHDD: misc.ParseEpisodeList(parts[3]),
MyListStateCD: misc.ParseEpisodeList(parts[4]),
MyListStateDeleted: misc.ParseEpisodeList(parts[5]),
},
WatchedEpisodes: misc.ParseEpisodeList(parts[6]),
EpisodesPerGroup: groupMap,
}
}