-
Notifications
You must be signed in to change notification settings - Fork 0
/
myanimelist.go
146 lines (117 loc) · 3.01 KB
/
myanimelist.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
package main
import (
"context"
"errors"
"log"
"net/url"
"time"
"github.com/nstratos/go-myanimelist/mal"
"golang.org/x/oauth2"
)
var errEmptyMalID = errors.New("mal id is empty")
var animeFields = mal.Fields{
"alternative_titles",
"num_episodes",
"my_list_status",
"start_season",
}
type MyAnimeListClient struct {
c *mal.Client
username string
}
func NewMyAnimeListClient(ctx context.Context, oauth *OAuth, username string) (*MyAnimeListClient, error) {
httpClient := oauth2.NewClient(ctx, oauth.TokenSource())
httpClient.Timeout = 10 * time.Minute
client := mal.NewClient(httpClient)
return &MyAnimeListClient{c: client, username: username}, nil
}
func (c *MyAnimeListClient) GetUserAnimeList(ctx context.Context) ([]mal.UserAnime, error) {
var userAnimeList []mal.UserAnime
var offset int
for {
list, resp, err := c.c.User.AnimeList(ctx, c.username, animeFields, mal.Offset(offset), mal.Limit(100))
if err != nil {
return nil, err
}
userAnimeList = append(userAnimeList, list...)
if resp.NextOffset == 0 {
break
}
offset = resp.NextOffset
}
return userAnimeList, nil
}
func (c *MyAnimeListClient) GetAnimesByName(ctx context.Context, name string) ([]mal.Anime, error) {
animeList, _, err := c.c.Anime.List(ctx, name, animeFields, mal.Limit(3))
if err != nil {
return nil, err
}
return animeList, nil
}
func (c *MyAnimeListClient) GetAnimeByID(ctx context.Context, id int) (*mal.Anime, error) {
if id <= 0 {
return nil, errEmptyMalID
}
anime, _, err := c.c.Anime.Details(ctx, id, animeFields)
if err != nil {
return nil, err
}
return anime, nil
}
func (c *MyAnimeListClient) UpdateAnime(ctx context.Context, anime Anime) error {
if anime.IDMal <= 0 {
return errEmptyMalID
}
st, err := anime.Status.GetMalStatus()
if err != nil {
return err
}
opts := []mal.UpdateMyAnimeListStatusOption{
st,
mal.Score(anime.Score),
mal.NumEpisodesWatched(anime.Progress),
}
if anime.StartedAt != nil {
opts = append(opts, mal.StartDate(*anime.StartedAt))
} else {
opts = append(opts, mal.StartDate(time.Time{}))
}
if anime.Status == StatusCompleted && anime.FinishedAt != nil {
opts = append(opts, mal.FinishDate(*anime.FinishedAt))
} else {
opts = append(opts, mal.FinishDate(time.Time{}))
}
_, _, err = c.c.Anime.UpdateMyListStatus(
ctx,
anime.IDMal,
opts...,
)
if err != nil {
return err
}
return nil
}
func NewMyAnimeListOAuth(ctx context.Context, config Config) (*OAuth, error) {
code := url.QueryEscape(randHttpParamString(43))
oauthMAL, err := NewOAuth(
ctx,
config.MyAnimeList,
config.OAuth.RedirectURI,
"myanimelist",
[]oauth2.AuthCodeOption{
oauth2.SetAuthURLParam("code_challenge", code),
oauth2.SetAuthURLParam("code_verifier", code),
oauth2.SetAuthURLParam("code_challenge_method", "plain"),
},
config.TokenFilePath,
)
if err != nil {
return nil, err
}
if oauthMAL.NeedInit() {
getToken(ctx, oauthMAL, config.OAuth.Port)
} else {
log.Println("Token already set, no need to start server")
}
return oauthMAL, nil
}