Mercurial > minori
annotate src/services/anilist.cc @ 286:53e3c015a973
anime: initial cross-service support
currently the Kitsu and MAL services don't work when chosen in the
GUI. This is because they haven't been implemented yet :)
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Wed, 08 May 2024 16:44:27 -0400 |
parents | e66ffc338d82 |
children | 9a88e1725fd2 |
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" |
76 | 5 #include "core/http.h" |
9 | 6 #include "core/json.h" |
7 #include "core/session.h" | |
8 #include "core/strings.h" | |
15 | 9 #include "gui/translate/anilist.h" |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
185
diff
changeset
|
10 |
258 | 11 #include <QByteArray> |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
185
diff
changeset
|
12 #include <QDate> |
9 | 13 #include <QDesktopServices> |
14 #include <QInputDialog> | |
15 #include <QLineEdit> | |
16 #include <QMessageBox> | |
10 | 17 #include <QUrl> |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
185
diff
changeset
|
18 |
9 | 19 #include <chrono> |
20 #include <exception> | |
175 | 21 |
22 #include <iostream> | |
23 | |
64 | 24 using namespace nlohmann::literals::json_literals; |
11 | 25 |
63 | 26 namespace Services { |
27 namespace AniList { | |
9 | 28 |
286
53e3c015a973
anime: initial cross-service support
Paper <paper@paper.us.eu.org>
parents:
284
diff
changeset
|
29 static constexpr int CLIENT_ID = 13706; |
185
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
184
diff
changeset
|
30 |
9 | 31 class Account { |
258 | 32 public: |
33 int UserId() const { return session.config.auth.anilist.user_id; } | |
34 void SetUserId(const int id) { session.config.auth.anilist.user_id = id; } | |
9 | 35 |
258 | 36 std::string AuthToken() const { return session.config.auth.anilist.auth_token; } |
286
53e3c015a973
anime: initial cross-service support
Paper <paper@paper.us.eu.org>
parents:
284
diff
changeset
|
37 void SetAuthToken(const std::string& auth_token) { session.config.auth.anilist.auth_token = auth_token; } |
9 | 38 |
258 | 39 bool Authenticated() const { return !AuthToken().empty(); } |
40 bool IsValid() const { return UserId() && Authenticated(); } | |
10 | 41 }; |
9 | 42 |
43 static Account account; | |
44 | |
45 std::string SendRequest(std::string data) { | |
76 | 46 std::vector<std::string> headers = {"Authorization: Bearer " + account.AuthToken(), "Accept: application/json", |
258 | 47 "Content-Type: application/json"}; |
77 | 48 return Strings::ToUtf8String(HTTP::Post("https://graphql.anilist.co", data, headers)); |
9 | 49 } |
50 | |
175 | 51 nlohmann::json SendJSONRequest(nlohmann::json data) { |
52 std::string request = SendRequest(data.dump()); | |
53 if (request.empty()) { | |
54 std::cerr << "[AniList] JSON Request returned an empty result!" << std::endl; | |
55 return {}; | |
56 } | |
57 | |
58 auto ret = nlohmann::json::parse(request, nullptr, false); | |
59 if (ret.is_discarded()) { | |
60 std::cerr << "[AniList] Failed to parse request JSON!" << std::endl; | |
61 return {}; | |
62 } | |
63 | |
64 if (ret.contains("/errors"_json_pointer) && ret.at("/errors"_json_pointer).is_array()) { | |
65 for (const auto& error : ret.at("/errors"_json_pointer)) | |
258 | 66 std::cerr << "[AniList] Received an error in response: " |
67 << JSON::GetString<std::string>(error, "/message"_json_pointer, "") << std::endl; | |
175 | 68 |
69 return {}; | |
70 } | |
71 | |
72 return ret; | |
73 } | |
74 | |
15 | 75 void ParseListStatus(std::string status, Anime::Anime& anime) { |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
185
diff
changeset
|
76 static const std::unordered_map<std::string, Anime::ListStatus> map = { |
279 | 77 {"CURRENT", Anime::ListStatus::Current }, |
78 {"PLANNING", Anime::ListStatus::Planning }, | |
79 {"COMPLETED", Anime::ListStatus::Completed}, | |
80 {"DROPPED", Anime::ListStatus::Dropped }, | |
81 {"PAUSED", Anime::ListStatus::Paused } | |
258 | 82 }; |
9 | 83 |
15 | 84 if (status == "REPEATING") { |
85 anime.SetUserIsRewatching(true); | |
279 | 86 anime.SetUserStatus(Anime::ListStatus::Current); |
15 | 87 return; |
88 } | |
9 | 89 |
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
90 if (map.find(status) == map.end()) { |
279 | 91 anime.SetUserStatus(Anime::ListStatus::NotInList); |
15 | 92 return; |
93 } | |
9 | 94 |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
185
diff
changeset
|
95 anime.SetUserStatus(map.at(status)); |
15 | 96 } |
9 | 97 |
15 | 98 std::string ListStatusToString(const Anime::Anime& anime) { |
279 | 99 if (anime.GetUserIsRewatching() && anime.GetUserStatus() == Anime::ListStatus::Current) |
15 | 100 return "REWATCHING"; |
101 | |
70
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
102 switch (anime.GetUserStatus()) { |
279 | 103 case Anime::ListStatus::Planning: return "PLANNING"; |
104 case Anime::ListStatus::Completed: return "COMPLETED"; | |
105 case Anime::ListStatus::Dropped: return "DROPPED"; | |
106 case Anime::ListStatus::Paused: return "PAUSED"; | |
76 | 107 default: break; |
70
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
108 } |
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
109 return "CURRENT"; |
15 | 110 } |
9 | 111 |
112 void ParseTitle(const nlohmann::json& json, Anime::Anime& anime) { | |
284
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
113 nlohmann::json::json_pointer g = "/native"_json_pointer; |
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
114 if (json.contains(g) && json[g].is_string()) |
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
115 anime.SetTitle(Anime::TitleLanguage::Native, json[g]); |
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
116 |
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
117 g = "/english"_json_pointer; |
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
118 if (json.contains(g) && json[g].is_string()) |
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
119 anime.SetTitle(Anime::TitleLanguage::English, json[g]); |
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
120 |
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
121 g = "/romaji"_json_pointer; |
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
122 if (json.contains(g) && json[g].is_string()) |
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
123 anime.SetTitle(Anime::TitleLanguage::Romaji, json[g]); |
9 | 124 } |
125 | |
126 int ParseMediaJson(const nlohmann::json& json) { | |
175 | 127 int id = JSON::GetNumber(json, "/id"_json_pointer); |
9 | 128 if (!id) |
129 return 0; | |
175 | 130 |
9 | 131 Anime::Anime& anime = Anime::db.items[id]; |
132 anime.SetId(id); | |
286
53e3c015a973
anime: initial cross-service support
Paper <paper@paper.us.eu.org>
parents:
284
diff
changeset
|
133 anime.SetServiceId(Anime::Service::AniList, Strings::ToUtf8String(id)); |
53e3c015a973
anime: initial cross-service support
Paper <paper@paper.us.eu.org>
parents:
284
diff
changeset
|
134 anime.SetServiceId(Anime::Service::MyAnimeList, Strings::ToUtf8String(JSON::GetNumber(json, "/id_mal"_json_pointer))); |
9 | 135 |
11 | 136 ParseTitle(json.at("/title"_json_pointer), anime); |
9 | 137 |
175 | 138 anime.SetEpisodes(JSON::GetNumber(json, "/episodes"_json_pointer, 0)); |
139 anime.SetFormat(Translate::AniList::ToSeriesFormat(JSON::GetString<std::string>(json, "/format"_json_pointer, ""))); | |
9 | 140 |
258 | 141 anime.SetAiringStatus( |
142 Translate::AniList::ToSeriesStatus(JSON::GetString<std::string>(json, "/status"_json_pointer, ""))); | |
9 | 143 |
175 | 144 anime.SetAirDate(Date(json["/startDate"_json_pointer])); |
9 | 145 |
175 | 146 anime.SetPosterUrl(JSON::GetString<std::string>(json, "/coverImage/large"_json_pointer, "")); |
66
6481c5aed3e1
posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
65
diff
changeset
|
147 |
175 | 148 anime.SetAudienceScore(JSON::GetNumber(json, "/averageScore"_json_pointer, 0)); |
279 | 149 // anime.SetSeason(Translate::AniList::ToSeriesSeason(JSON::GetString<std::string>(json, "/season"_json_pointer, ""))); |
175 | 150 anime.SetDuration(JSON::GetNumber(json, "/duration"_json_pointer, 0)); |
260
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
151 |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
152 std::string synopsis = JSON::GetString<std::string>(json, "/description"_json_pointer, ""); |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
153 Strings::TextifySynopsis(synopsis); |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
154 anime.SetSynopsis(synopsis); |
9 | 155 |
175 | 156 anime.SetGenres(JSON::GetArray<std::vector<std::string>>(json, "/genres"_json_pointer, {})); |
157 anime.SetTitleSynonyms(JSON::GetArray<std::vector<std::string>>(json, "/synonyms"_json_pointer, {})); | |
158 | |
10 | 159 return id; |
9 | 160 } |
161 | |
10 | 162 int ParseListItem(const nlohmann::json& json) { |
163 int id = ParseMediaJson(json["media"]); | |
164 | |
165 Anime::Anime& anime = Anime::db.items[id]; | |
166 | |
167 anime.AddToUserList(); | |
9 | 168 |
175 | 169 anime.SetUserScore(JSON::GetNumber(json, "/score"_json_pointer, 0)); |
170 anime.SetUserProgress(JSON::GetNumber(json, "/progress"_json_pointer, 0)); | |
171 ParseListStatus(JSON::GetString<std::string>(json, "/status"_json_pointer, ""), anime); | |
172 anime.SetUserNotes(JSON::GetString<std::string>(json, "/notes"_json_pointer, "")); | |
9 | 173 |
175 | 174 anime.SetUserDateStarted(Date(json["/startedAt"_json_pointer])); |
175 anime.SetUserDateCompleted(Date(json["/completedAt"_json_pointer])); | |
9 | 176 |
175 | 177 anime.SetUserTimeUpdated(JSON::GetNumber(json, "/updatedAt"_json_pointer, 0)); |
10 | 178 |
179 return id; | |
9 | 180 } |
181 | |
182 int ParseList(const nlohmann::json& json) { | |
183 for (const auto& entry : json["entries"].items()) { | |
184 ParseListItem(entry.value()); | |
185 } | |
10 | 186 return 1; |
9 | 187 } |
188 | |
10 | 189 int GetAnimeList() { |
175 | 190 if (!account.IsValid()) { |
191 std::cerr << "AniList: Account isn't valid!" << std::endl; | |
192 return 0; | |
193 } | |
194 | |
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
195 /* NOTE: these really ought to be in the qrc file */ |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
196 constexpr std::string_view query = "query ($id: Int) {\n" |
258 | 197 " MediaListCollection (userId: $id, type: ANIME) {\n" |
198 " lists {\n" | |
199 " name\n" | |
200 " entries {\n" | |
201 " score\n" | |
202 " notes\n" | |
203 " status\n" | |
204 " progress\n" | |
205 " startedAt {\n" | |
206 " year\n" | |
207 " month\n" | |
208 " day\n" | |
209 " }\n" | |
210 " completedAt {\n" | |
211 " year\n" | |
212 " month\n" | |
213 " day\n" | |
214 " }\n" | |
215 " updatedAt\n" | |
216 " media {\n" | |
217 " coverImage {\n" | |
218 " large\n" | |
219 " }\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 | |
175 | 253 |
254 auto res = SendJSONRequest(json); | |
255 | |
9 | 256 for (const auto& list : res["data"]["MediaListCollection"]["lists"].items()) { |
10 | 257 ParseList(list.value()); |
9 | 258 } |
259 return 1; | |
260 } | |
261 | |
250 | 262 /* return is a vector of anime ids */ |
263 std::vector<int> Search(const std::string& search) { | |
258 | 264 constexpr std::string_view query = "query ($search: String) {\n" |
265 " Page (page: 1, perPage: 50) {\n" | |
266 " media (search: $search, type: ANIME) {\n" | |
267 " coverImage {\n" | |
268 " large\n" | |
269 " }\n" | |
270 " id\n" | |
286
53e3c015a973
anime: initial cross-service support
Paper <paper@paper.us.eu.org>
parents:
284
diff
changeset
|
271 " id_mal\n" |
258 | 272 " title {\n" |
273 " romaji\n" | |
274 " english\n" | |
275 " native\n" | |
276 " }\n" | |
277 " format\n" | |
278 " status\n" | |
279 " averageScore\n" | |
280 " season\n" | |
281 " startDate {\n" | |
282 " year\n" | |
283 " month\n" | |
284 " day\n" | |
285 " }\n" | |
286 " genres\n" | |
287 " episodes\n" | |
288 " duration\n" | |
289 " synonyms\n" | |
290 " description(asHtml: false)\n" | |
291 " }\n" | |
292 " }\n" | |
293 "}\n"; | |
250 | 294 |
295 // clang-format off | |
296 nlohmann::json json = { | |
297 {"query", query}, | |
298 {"variables", { | |
299 {"search", search} | |
300 }} | |
301 }; | |
302 // clang-format on | |
303 | |
304 auto res = SendJSONRequest(json); | |
305 | |
306 std::vector<int> ret; | |
307 ret.reserve(res["data"]["Page"]["media"].size()); | |
308 | |
309 for (const auto& media : res["data"]["Page"]["media"].items()) | |
310 ret.push_back(ParseMediaJson(media.value())); | |
311 | |
312 return ret; | |
313 } | |
314 | |
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
315 int UpdateAnimeEntry(int id) { |
9 | 316 /** |
317 * possible values: | |
15 | 318 * |
9 | 319 * int mediaId, |
320 * MediaListStatus status, | |
321 * float score, | |
322 * int scoreRaw, | |
323 * int progress, | |
184
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
324 * int progressVolumes, // manga-specific. |
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
325 * int repeat, // rewatch |
258 | 326 * int priority, |
9 | 327 * bool private, |
328 * string notes, | |
329 * bool hiddenFromStatusLists, | |
330 * string[] customLists, | |
331 * float[] advancedScores, | |
332 * Date startedAt, | |
333 * Date completedAt | |
258 | 334 **/ |
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
335 Anime::Anime& anime = Anime::db.items[id]; |
184
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
336 if (!anime.IsInUserList()) |
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
337 return 0; |
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
338 |
250 | 339 constexpr std::string_view query = |
258 | 340 "mutation ($media_id: Int, $progress: Int, $status: MediaListStatus, $score: Int, $notes: String, $start: " |
341 "FuzzyDateInput, $comp: FuzzyDateInput, $repeat: Int) {\n" | |
342 " SaveMediaListEntry (mediaId: $media_id, progress: $progress, status: $status, scoreRaw: $score, notes: " | |
343 "$notes, startedAt: $start, completedAt: $comp, repeat: $repeat) {\n" | |
344 " id\n" | |
345 " }\n" | |
346 "}\n"; | |
9 | 347 // clang-format off |
348 nlohmann::json json = { | |
349 {"query", query}, | |
350 {"variables", { | |
10 | 351 {"media_id", anime.GetId()}, |
352 {"progress", anime.GetUserProgress()}, | |
15 | 353 {"status", ListStatusToString(anime)}, |
10 | 354 {"score", anime.GetUserScore()}, |
77 | 355 {"notes", anime.GetUserNotes()}, |
356 {"start", anime.GetUserDateStarted().GetAsAniListJson()}, | |
184
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
357 {"comp", anime.GetUserDateCompleted().GetAsAniListJson()}, |
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
358 {"repeat", anime.GetUserRewatchedTimes()} |
9 | 359 }} |
360 }; | |
361 // clang-format on | |
175 | 362 |
363 auto ret = SendJSONRequest(json); | |
364 | |
365 return JSON::GetNumber(ret, "/data/SaveMediaListEntry/id"_json_pointer, 0); | |
9 | 366 } |
367 | |
368 int ParseUser(const nlohmann::json& json) { | |
175 | 369 account.SetUserId(JSON::GetNumber(json, "/id"_json_pointer, 0)); |
10 | 370 return account.UserId(); |
9 | 371 } |
372 | |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
373 bool AuthorizeUser() { |
9 | 374 /* Prompt for PIN */ |
258 | 375 QDesktopServices::openUrl(QUrl(Strings::ToQString("https://anilist.co/api/v2/oauth/authorize?client_id=" + |
376 Strings::ToUtf8String(CLIENT_ID) + "&response_type=token"))); | |
175 | 377 |
9 | 378 bool ok; |
379 QString token = QInputDialog::getText( | |
258 | 380 0, "Credentials needed!", "Please enter the code given to you after logging in to AniList:", QLineEdit::Normal, |
381 "", &ok); | |
175 | 382 |
383 if (!ok || token.isEmpty()) | |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
384 return false; |
175 | 385 |
386 account.SetAuthToken(Strings::ToUtf8String(token)); | |
387 | |
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
388 constexpr std::string_view query = "query {\n" |
258 | 389 " Viewer {\n" |
390 " id\n" | |
391 " name\n" | |
392 " mediaListOptions {\n" | |
393 " scoreFormat\n" // this will be used... eventually | |
394 " }\n" | |
395 " }\n" | |
396 "}\n"; | |
9 | 397 nlohmann::json json = { |
258 | 398 {"query", query} |
399 }; | |
175 | 400 |
401 auto ret = SendJSONRequest(json); | |
402 | |
74 | 403 ParseUser(ret["data"]["Viewer"]); |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
404 return true; |
9 | 405 } |
406 | |
63 | 407 } // namespace AniList |
408 } // namespace Services |