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