Mercurial > minori
annotate src/services/anilist.cpp @ 52:0c4138de2ea7
anime list: we are finally read-write
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Mon, 25 Sep 2023 22:49:42 -0400 |
parents | e613772f41d5 |
children | 3d2decf093bb |
rev | line source |
---|---|
9 | 1 #include "services/anilist.h" |
2 #include "core/anime.h" | |
10 | 3 #include "core/anime_db.h" |
9 | 4 #include "core/config.h" |
5 #include "core/json.h" | |
6 #include "core/session.h" | |
7 #include "core/strings.h" | |
15 | 8 #include "gui/translate/anilist.h" |
9 | 9 #include <QDesktopServices> |
10 #include <QInputDialog> | |
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
11 #include <QDebug> |
9 | 12 #include <QLineEdit> |
13 #include <QMessageBox> | |
10 | 14 #include <QUrl> |
9 | 15 #include <chrono> |
16 #include <curl/curl.h> | |
17 #include <exception> | |
18 #define CLIENT_ID "13706" | |
19 | |
15 | 20 using nlohmann::literals::operator"" _json_pointer; |
11 | 21 |
9 | 22 namespace Services::AniList { |
23 | |
24 class Account { | |
25 public: | |
10 | 26 std::string Username() const { return session.config.anilist.username; } |
36 | 27 void SetUsername(std::string const& username) { session.config.anilist.username = username; } |
9 | 28 |
10 | 29 int UserId() const { return session.config.anilist.user_id; } |
30 void SetUserId(const int id) { session.config.anilist.user_id = id; } | |
9 | 31 |
10 | 32 std::string AuthToken() const { return session.config.anilist.auth_token; } |
36 | 33 void SetAuthToken(std::string const& auth_token) { session.config.anilist.auth_token = auth_token; } |
9 | 34 |
35 bool Authenticated() const { return !AuthToken().empty(); } | |
10 | 36 }; |
9 | 37 |
38 static Account account; | |
39 | |
40 static size_t CurlWriteCallback(void* contents, size_t size, size_t nmemb, void* userdata) { | |
41 ((std::string*)userdata)->append((char*)contents, size * nmemb); | |
42 return size * nmemb; | |
43 } | |
44 | |
45 /* A wrapper around cURL to send requests to AniList */ | |
46 std::string SendRequest(std::string data) { | |
47 struct curl_slist* list = NULL; | |
48 std::string userdata; | |
49 CURL* curl = curl_easy_init(); | |
50 if (curl) { | |
15 | 51 std::string bearer = "Authorization: Bearer " + account.AuthToken(); |
9 | 52 list = curl_slist_append(list, "Accept: application/json"); |
53 list = curl_slist_append(list, "Content-Type: application/json"); | |
54 list = curl_slist_append(list, bearer.c_str()); | |
55 curl_easy_setopt(curl, CURLOPT_URL, "https://graphql.anilist.co"); | |
56 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str()); | |
57 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); | |
58 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &userdata); | |
59 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &CurlWriteCallback); | |
60 /* Use system certs... useful on Windows. */ | |
61 curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA); | |
62 CURLcode res = curl_easy_perform(curl); | |
48
e613772f41d5
statistics.cpp: show requests made
Paper <mrpapersonic@gmail.com>
parents:
47
diff
changeset
|
63 session.IncrementRequests(); |
9 | 64 curl_slist_free_all(list); |
65 curl_easy_cleanup(curl); | |
66 if (res != CURLE_OK) { | |
67 QMessageBox box(QMessageBox::Icon::Critical, "", | |
36 | 68 QString("curl_easy_perform(curl) failed!: ") + QString(curl_easy_strerror(res))); |
9 | 69 box.exec(); |
70 return ""; | |
71 } | |
72 return userdata; | |
73 } | |
74 return ""; | |
75 } | |
76 | |
15 | 77 void ParseListStatus(std::string status, Anime::Anime& anime) { |
78 std::unordered_map<std::string, Anime::ListStatus> map = { | |
79 {"CURRENT", Anime::ListStatus::CURRENT }, | |
80 {"PLANNING", Anime::ListStatus::PLANNING }, | |
81 {"COMPLETED", Anime::ListStatus::COMPLETED}, | |
82 {"DROPPED", Anime::ListStatus::DROPPED }, | |
83 {"PAUSED", Anime::ListStatus::PAUSED } | |
84 }; | |
9 | 85 |
15 | 86 if (status == "REPEATING") { |
87 anime.SetUserIsRewatching(true); | |
88 anime.SetUserStatus(Anime::ListStatus::CURRENT); | |
89 return; | |
90 } | |
9 | 91 |
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
92 if (map.find(status) == map.end()) { |
15 | 93 anime.SetUserStatus(Anime::ListStatus::NOT_IN_LIST); |
94 return; | |
95 } | |
9 | 96 |
15 | 97 anime.SetUserStatus(map[status]); |
98 } | |
9 | 99 |
15 | 100 std::string ListStatusToString(const Anime::Anime& anime) { |
101 std::unordered_map<Anime::ListStatus, std::string> map = { | |
102 {Anime::ListStatus::CURRENT, "CURRENT" }, | |
103 {Anime::ListStatus::PLANNING, "PLANNING" }, | |
104 {Anime::ListStatus::COMPLETED, "COMPLETED"}, | |
105 {Anime::ListStatus::DROPPED, "DROPPED" }, | |
106 {Anime::ListStatus::PAUSED, "PAUSED" } | |
107 }; | |
9 | 108 |
15 | 109 if (anime.GetUserIsRewatching()) |
110 return "REWATCHING"; | |
111 | |
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
112 if (map.find(anime.GetUserStatus()) == map.end()) |
15 | 113 return "CURRENT"; |
114 return map[anime.GetUserStatus()]; | |
115 } | |
9 | 116 |
10 | 117 Date ParseDate(const nlohmann::json& json) { |
118 Date date; | |
11 | 119 if (json.contains("/year"_json_pointer) && json.at("/year"_json_pointer).is_number()) |
9 | 120 date.SetYear(JSON::GetInt(json, "/year"_json_pointer)); |
121 else | |
122 date.VoidYear(); | |
123 | |
11 | 124 if (json.contains("/month"_json_pointer) && json.at("/month"_json_pointer).is_number()) |
9 | 125 date.SetMonth(JSON::GetInt(json, "/month"_json_pointer)); |
126 else | |
127 date.VoidMonth(); | |
128 | |
11 | 129 if (json.contains("/day"_json_pointer) && json.at("/day"_json_pointer).is_number()) |
9 | 130 date.SetDay(JSON::GetInt(json, "/day"_json_pointer)); |
131 else | |
132 date.VoidDay(); | |
10 | 133 return date; |
9 | 134 } |
135 | |
136 void ParseTitle(const nlohmann::json& json, Anime::Anime& anime) { | |
137 anime.SetNativeTitle(JSON::GetString(json, "/native"_json_pointer)); | |
138 anime.SetEnglishTitle(JSON::GetString(json, "/english"_json_pointer)); | |
139 anime.SetRomajiTitle(JSON::GetString(json, "/romaji"_json_pointer)); | |
140 } | |
141 | |
142 int ParseMediaJson(const nlohmann::json& json) { | |
143 int id = JSON::GetInt(json, "/id"_json_pointer); | |
144 if (!id) | |
145 return 0; | |
146 Anime::Anime& anime = Anime::db.items[id]; | |
147 anime.SetId(id); | |
148 | |
11 | 149 ParseTitle(json.at("/title"_json_pointer), anime); |
9 | 150 |
151 anime.SetEpisodes(JSON::GetInt(json, "/episodes"_json_pointer)); | |
15 | 152 anime.SetFormat(Translate::AniList::ToSeriesFormat(JSON::GetString(json, "/format"_json_pointer))); |
9 | 153 |
15 | 154 anime.SetAiringStatus(Translate::AniList::ToSeriesStatus(JSON::GetString(json, "/status"_json_pointer))); |
9 | 155 |
10 | 156 anime.SetAirDate(ParseDate(json["/startDate"_json_pointer])); |
9 | 157 |
158 anime.SetAudienceScore(JSON::GetInt(json, "/averageScore"_json_pointer)); | |
15 | 159 anime.SetSeason(Translate::AniList::ToSeriesSeason(JSON::GetString(json, "/season"_json_pointer))); |
9 | 160 anime.SetDuration(JSON::GetInt(json, "/duration"_json_pointer)); |
10 | 161 anime.SetSynopsis(Strings::TextifySynopsis(JSON::GetString(json, "/description"_json_pointer))); |
9 | 162 |
163 if (json.contains("/genres"_json_pointer) && json["/genres"_json_pointer].is_array()) | |
164 anime.SetGenres(json["/genres"_json_pointer].get<std::vector<std::string>>()); | |
165 if (json.contains("/synonyms"_json_pointer) && json["/synonyms"_json_pointer].is_array()) | |
10 | 166 anime.SetTitleSynonyms(json["/synonyms"_json_pointer].get<std::vector<std::string>>()); |
167 return id; | |
9 | 168 } |
169 | |
10 | 170 int ParseListItem(const nlohmann::json& json) { |
171 int id = ParseMediaJson(json["media"]); | |
172 | |
173 Anime::Anime& anime = Anime::db.items[id]; | |
174 | |
175 anime.AddToUserList(); | |
9 | 176 |
10 | 177 anime.SetUserScore(JSON::GetInt(json, "/score"_json_pointer)); |
178 anime.SetUserProgress(JSON::GetInt(json, "/progress"_json_pointer)); | |
15 | 179 ParseListStatus(JSON::GetString(json, "/status"_json_pointer), anime); |
10 | 180 anime.SetUserNotes(JSON::GetString(json, "/notes"_json_pointer)); |
9 | 181 |
10 | 182 anime.SetUserDateStarted(ParseDate(json["/startedAt"_json_pointer])); |
183 anime.SetUserDateCompleted(ParseDate(json["/completedAt"_json_pointer])); | |
9 | 184 |
10 | 185 anime.SetUserTimeUpdated(JSON::GetInt(json, "/updatedAt"_json_pointer)); |
186 | |
187 return id; | |
9 | 188 } |
189 | |
190 int ParseList(const nlohmann::json& json) { | |
191 for (const auto& entry : json["entries"].items()) { | |
192 ParseListItem(entry.value()); | |
193 } | |
10 | 194 return 1; |
9 | 195 } |
196 | |
10 | 197 int GetAnimeList() { |
9 | 198 /* NOTE: these should be in the qrc file */ |
199 const std::string query = "query ($id: Int) {\n" | |
15 | 200 " MediaListCollection (userId: $id, type: ANIME) {\n" |
201 " lists {\n" | |
202 " name\n" | |
203 " entries {\n" | |
204 " score\n" | |
205 " notes\n" | |
206 " status\n" | |
207 " progress\n" | |
208 " startedAt {\n" | |
209 " year\n" | |
210 " month\n" | |
211 " day\n" | |
212 " }\n" | |
213 " completedAt {\n" | |
214 " year\n" | |
215 " month\n" | |
216 " day\n" | |
217 " }\n" | |
218 " updatedAt\n" | |
219 " media {\n" | |
220 " id\n" | |
221 " title {\n" | |
222 " romaji\n" | |
223 " english\n" | |
224 " native\n" | |
225 " }\n" | |
226 " format\n" | |
227 " status\n" | |
228 " averageScore\n" | |
229 " season\n" | |
230 " startDate {\n" | |
231 " year\n" | |
232 " month\n" | |
233 " day\n" | |
234 " }\n" | |
235 " genres\n" | |
236 " episodes\n" | |
237 " duration\n" | |
238 " synonyms\n" | |
239 " description(asHtml: false)\n" | |
240 " }\n" | |
241 " }\n" | |
242 " }\n" | |
243 " }\n" | |
244 "}\n"; | |
9 | 245 // clang-format off |
246 nlohmann::json json = { | |
247 {"query", query}, | |
248 {"variables", { | |
10 | 249 {"id", account.UserId()} |
9 | 250 }} |
251 }; | |
252 // clang-format on | |
253 /* TODO: do a try catch here, catch any json errors and then call | |
254 Authorize() if needed */ | |
255 auto res = nlohmann::json::parse(SendRequest(json.dump())); | |
256 /* TODO: make sure that we actually need the wstring converter and see | |
257 if we can just get wide strings back from nlohmann::json */ | |
258 for (const auto& list : res["data"]["MediaListCollection"]["lists"].items()) { | |
10 | 259 ParseList(list.value()); |
9 | 260 } |
261 return 1; | |
262 } | |
263 | |
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
264 int UpdateAnimeEntry(int id) { |
9 | 265 /** |
266 * possible values: | |
15 | 267 * |
9 | 268 * int mediaId, |
269 * MediaListStatus status, | |
270 * float score, | |
271 * int scoreRaw, | |
272 * int progress, | |
273 * int progressVolumes, | |
274 * int repeat, | |
275 * int priority, | |
276 * bool private, | |
277 * string notes, | |
278 * bool hiddenFromStatusLists, | |
279 * string[] customLists, | |
280 * float[] advancedScores, | |
281 * Date startedAt, | |
282 * Date completedAt | |
15 | 283 **/ |
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
284 Anime::Anime& anime = Anime::db.items[id]; |
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
285 const std::string query = "mutation ($media_id: Int, $progress: Int, $status: MediaListStatus, $score: Int, $notes: String) {\n" |
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
286 " SaveMediaListEntry (mediaId: $media_id, progress: $progress, status: $status, scoreRaw: $score, notes: $notes) {\n" |
15 | 287 " id\n" |
288 " }\n" | |
289 "}\n"; | |
9 | 290 // clang-format off |
291 nlohmann::json json = { | |
292 {"query", query}, | |
293 {"variables", { | |
10 | 294 {"media_id", anime.GetId()}, |
295 {"progress", anime.GetUserProgress()}, | |
15 | 296 {"status", ListStatusToString(anime)}, |
10 | 297 {"score", anime.GetUserScore()}, |
298 {"notes", anime.GetUserNotes()} | |
9 | 299 }} |
300 }; | |
301 // clang-format on | |
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
302 auto ret = nlohmann::json::parse(SendRequest(json.dump())); |
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
303 return JSON::GetInt(ret, "/data/SaveMediaListEntry/id"_json_pointer); |
9 | 304 } |
305 | |
306 int ParseUser(const nlohmann::json& json) { | |
307 account.SetUsername(JSON::GetString(json, "/name"_json_pointer)); | |
308 account.SetUserId(JSON::GetInt(json, "/id"_json_pointer)); | |
10 | 309 return account.UserId(); |
9 | 310 } |
311 | |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
312 bool AuthorizeUser() { |
9 | 313 /* Prompt for PIN */ |
36 | 314 QDesktopServices::openUrl( |
315 QUrl("https://anilist.co/api/v2/oauth/authorize?client_id=" CLIENT_ID "&response_type=token")); | |
9 | 316 bool ok; |
317 QString token = QInputDialog::getText( | |
36 | 318 0, "Credentials needed!", "Please enter the code given to you after logging in to AniList:", QLineEdit::Normal, |
319 "", &ok); | |
9 | 320 if (ok && !token.isEmpty()) |
321 account.SetAuthToken(token.toStdString()); | |
15 | 322 else // fail |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
323 return false; |
9 | 324 const std::string query = "query {\n" |
15 | 325 " Viewer {\n" |
326 " id\n" | |
327 " name\n" | |
328 " mediaListOptions {\n" | |
329 " scoreFormat\n" | |
330 " }\n" | |
331 " }\n" | |
332 "}\n"; | |
9 | 333 nlohmann::json json = { |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
334 {"query", query} |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
335 }; |
9 | 336 auto ret = nlohmann::json::parse(SendRequest(json.dump())); |
10 | 337 ParseUser(json["Viewer"]); |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
338 return true; |
9 | 339 } |
340 | |
341 } // namespace Services::AniList |