-
Notifications
You must be signed in to change notification settings - Fork 1
/
anilist_utils.go
242 lines (222 loc) · 4.95 KB
/
anilist_utils.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package main
import (
"encoding/json"
"fmt"
"github.com/aqatl/mal/anilist"
"github.com/aqatl/mal/oauth2"
"github.com/urfave/cli"
"os"
"time"
)
type AniList struct {
Token oauth2.OAuthToken
User anilist.User
List
}
type List []anilist.MediaListEntry
func (l List) GetMediaListById(id int) *anilist.MediaListEntry {
for i := 0; i < len(l); i++ {
if l[i].Id == id {
return &l[i]
}
}
return nil
}
func (l List) GetMediaListByMalId(malId int) *anilist.MediaListEntry {
for i := 0; i < len(l); i++ {
if l[i].IdMal == malId {
return &l[i]
}
}
return nil
}
func (l List) DeleteById(listId int) List {
idx := 0
found := false
for i, entry := range l {
if entry.ListId == listId {
idx = i
found = true
break
}
}
if !found {
return l
}
l = append(l[:idx], l[idx+1:]...)
return l
}
func sliceOfEntryTitles(entry *anilist.MediaListEntry) []string {
alts := make([]string, 0, 3+len(entry.Synonyms))
if t := entry.Title.English; t != "" {
alts = append(alts, t)
}
if t := entry.Title.Native; t != "" {
alts = append(alts, t)
}
if t := entry.Title.Romaji; t != "" {
alts = append(alts, t)
}
alts = append(alts, entry.Synonyms...)
return alts
}
func alGetList(al *AniList, status anilist.MediaListStatus) List {
if status == anilist.All {
return al.List
} else {
list := make(List, 0)
for i := range al.List {
if al.List[i].Status == status {
list = append(list, al.List[i])
}
}
return list
}
}
func loadAniListFull(ctx *cli.Context) (al *AniList, entry *anilist.MediaListEntry, cfg *Config, err error) {
al, err = loadAniList(ctx)
if err != nil {
return
}
cfg = LoadConfig()
if cfg.ALSelectedID == 0 {
fmt.Println("No entry selected")
}
entry = al.GetMediaListById(cfg.ALSelectedID)
if entry == nil {
err = fmt.Errorf("no entry found")
}
return
}
func loadAniList(ctx *cli.Context) (*AniList, error) {
token, err := loadOAuthToken()
if err != nil {
return nil, err
}
al := &AniList{Token: token}
if err := loadAniListUser(al); err != nil {
return nil, err
}
if ctx.Bool("refresh") {
err = fetchAniListAnimeLists(al)
} else {
err = loadAniListAnimeLists(al)
}
return al, err
}
func loadOAuthToken() (oauth2.OAuthToken, error) {
token, err := loadCachedOAuthToken()
if err != nil {
if err == anilist.InvalidToken || os.IsNotExist(err) {
token, err = requestAniListToken()
}
}
return token, err
}
func loadCachedOAuthToken() (oauth2.OAuthToken, error) {
token := oauth2.OAuthToken{}
LoadJsonFile(AniListCredsFile, &token)
if token.Token == "" || token.ExpireDate.Before(time.Now()) {
return token, anilist.InvalidToken
}
return token, nil
}
func saveOAuthToken(token oauth2.OAuthToken) error {
f, err := os.Create(AniListCredsFile)
defer f.Close()
if err != nil {
return err
}
err = json.NewEncoder(f).Encode(&token)
return err
}
func requestAniListToken() (token oauth2.OAuthToken, err error) {
token, err = oauth2.OAuthImplicitGrantAuth(
"https://anilist.co/api/v2/oauth/authorize",
LoadConfig().BrowserPath,
743,
42505,
)
if err != nil {
return
}
err = saveOAuthToken(token)
return
}
func loadAniListUser(al *AniList) error {
if LoadJsonFile(AniListUserFile, &al.User) {
return nil
}
err := anilist.QueryAuthenticatedUser(&al.User, al.Token)
if err == anilist.InvalidToken {
if al.Token, err = requestAniListToken(); err != nil {
return err
}
err = anilist.QueryAuthenticatedUser(&al.User, al.Token)
}
if err == nil {
err = saveAniListUser(&al.User)
}
return err
}
func saveAniListUser(user *anilist.User) error {
f, err := os.Create(AniListUserFile)
defer f.Close()
if err != nil {
return err
}
err = json.NewEncoder(f).Encode(user)
return err
}
func loadAniListAnimeLists(al *AniList) error {
f, err := os.Open(AniListCacheFile)
defer f.Close()
if err == nil {
err = json.NewDecoder(f).Decode(&al.List)
return err
}
if !os.IsNotExist(err) {
return err
}
return fetchAniListAnimeLists(al)
}
func fetchAniListAnimeLists(al *AniList) error {
lists, err := anilist.QueryUserListsWaitAnimation(al.User.Id, al.User.MediaListOptions.ScoreFormat, al.Token)
entryIds := make(map[int]bool)
for i := range lists {
for _, entry := range lists[i].Entries {
if !entryIds[entry.Id] {
al.List = append(al.List, entry)
entryIds[entry.Id] = true
}
}
}
if err == anilist.InvalidToken {
if al.Token, err = requestAniListToken(); err != nil {
return err
}
lists, err = anilist.QueryUserListsWaitAnimation(al.User.Id, al.User.MediaListOptions.ScoreFormat, al.Token)
entryIds := make(map[int]bool)
for i := range lists {
for _, entry := range lists[i].Entries {
if !entryIds[entry.Id] {
al.List = append(al.List, entry)
entryIds[entry.Id] = true
}
}
}
}
if err == nil {
err = saveAniListAnimeLists(al)
}
return err
}
func saveAniListAnimeLists(al *AniList) error {
f, err := os.Create(AniListCacheFile)
defer f.Close()
if err != nil {
return err
}
err = json.NewEncoder(f).Encode(&al.List)
return err
}