Mercurial > minori
annotate src/services/anilist.cc @ 284:e66ffc338d82
anime: refactor title structure to a map
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Wed, 08 May 2024 16:21:05 -0400 |
parents | 657fda1b9cac |
children | 53e3c015a973 |
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 |
185
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
184
diff
changeset
|
29 constexpr int CLIENT_ID = 13706; |
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; } |
37 void SetAuthToken(std::string const& 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); | |
133 | |
11 | 134 ParseTitle(json.at("/title"_json_pointer), anime); |
9 | 135 |
175 | 136 anime.SetEpisodes(JSON::GetNumber(json, "/episodes"_json_pointer, 0)); |
137 anime.SetFormat(Translate::AniList::ToSeriesFormat(JSON::GetString<std::string>(json, "/format"_json_pointer, ""))); | |
9 | 138 |
258 | 139 anime.SetAiringStatus( |
140 Translate::AniList::ToSeriesStatus(JSON::GetString<std::string>(json, "/status"_json_pointer, ""))); | |
9 | 141 |
175 | 142 anime.SetAirDate(Date(json["/startDate"_json_pointer])); |
9 | 143 |
175 | 144 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
|
145 |
175 | 146 anime.SetAudienceScore(JSON::GetNumber(json, "/averageScore"_json_pointer, 0)); |
279 | 147 // anime.SetSeason(Translate::AniList::ToSeriesSeason(JSON::GetString<std::string>(json, "/season"_json_pointer, ""))); |
175 | 148 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
|
149 |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
150 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
|
151 Strings::TextifySynopsis(synopsis); |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
152 anime.SetSynopsis(synopsis); |
9 | 153 |
175 | 154 anime.SetGenres(JSON::GetArray<std::vector<std::string>>(json, "/genres"_json_pointer, {})); |
155 anime.SetTitleSynonyms(JSON::GetArray<std::vector<std::string>>(json, "/synonyms"_json_pointer, {})); | |
156 | |
10 | 157 return id; |
9 | 158 } |
159 | |
10 | 160 int ParseListItem(const nlohmann::json& json) { |
161 int id = ParseMediaJson(json["media"]); | |
162 | |
163 Anime::Anime& anime = Anime::db.items[id]; | |
164 | |
165 anime.AddToUserList(); | |
9 | 166 |
175 | 167 anime.SetUserScore(JSON::GetNumber(json, "/score"_json_pointer, 0)); |
168 anime.SetUserProgress(JSON::GetNumber(json, "/progress"_json_pointer, 0)); | |
169 ParseListStatus(JSON::GetString<std::string>(json, "/status"_json_pointer, ""), anime); | |
170 anime.SetUserNotes(JSON::GetString<std::string>(json, "/notes"_json_pointer, "")); | |
9 | 171 |
175 | 172 anime.SetUserDateStarted(Date(json["/startedAt"_json_pointer])); |
173 anime.SetUserDateCompleted(Date(json["/completedAt"_json_pointer])); | |
9 | 174 |
175 | 175 anime.SetUserTimeUpdated(JSON::GetNumber(json, "/updatedAt"_json_pointer, 0)); |
10 | 176 |
177 return id; | |
9 | 178 } |
179 | |
180 int ParseList(const nlohmann::json& json) { | |
181 for (const auto& entry : json["entries"].items()) { | |
182 ParseListItem(entry.value()); | |
183 } | |
10 | 184 return 1; |
9 | 185 } |
186 | |
10 | 187 int GetAnimeList() { |
175 | 188 if (!account.IsValid()) { |
189 std::cerr << "AniList: Account isn't valid!" << std::endl; | |
190 return 0; | |
191 } | |
192 | |
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
193 /* 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
|
194 constexpr std::string_view query = "query ($id: Int) {\n" |
258 | 195 " MediaListCollection (userId: $id, type: ANIME) {\n" |
196 " lists {\n" | |
197 " name\n" | |
198 " entries {\n" | |
199 " score\n" | |
200 " notes\n" | |
201 " status\n" | |
202 " progress\n" | |
203 " startedAt {\n" | |
204 " year\n" | |
205 " month\n" | |
206 " day\n" | |
207 " }\n" | |
208 " completedAt {\n" | |
209 " year\n" | |
210 " month\n" | |
211 " day\n" | |
212 " }\n" | |
213 " updatedAt\n" | |
214 " media {\n" | |
215 " coverImage {\n" | |
216 " large\n" | |
217 " }\n" | |
218 " id\n" | |
219 " title {\n" | |
220 " romaji\n" | |
221 " english\n" | |
222 " native\n" | |
223 " }\n" | |
224 " format\n" | |
225 " status\n" | |
226 " averageScore\n" | |
227 " season\n" | |
228 " startDate {\n" | |
229 " year\n" | |
230 " month\n" | |
231 " day\n" | |
232 " }\n" | |
233 " genres\n" | |
234 " episodes\n" | |
235 " duration\n" | |
236 " synonyms\n" | |
237 " description(asHtml: false)\n" | |
238 " }\n" | |
239 " }\n" | |
240 " }\n" | |
241 " }\n" | |
242 "}\n"; | |
9 | 243 // clang-format off |
244 nlohmann::json json = { | |
245 {"query", query}, | |
246 {"variables", { | |
10 | 247 {"id", account.UserId()} |
9 | 248 }} |
249 }; | |
250 // clang-format on | |
175 | 251 |
252 auto res = SendJSONRequest(json); | |
253 | |
9 | 254 for (const auto& list : res["data"]["MediaListCollection"]["lists"].items()) { |
10 | 255 ParseList(list.value()); |
9 | 256 } |
257 return 1; | |
258 } | |
259 | |
250 | 260 /* return is a vector of anime ids */ |
261 std::vector<int> Search(const std::string& search) { | |
258 | 262 constexpr std::string_view query = "query ($search: String) {\n" |
263 " Page (page: 1, perPage: 50) {\n" | |
264 " media (search: $search, type: ANIME) {\n" | |
265 " coverImage {\n" | |
266 " large\n" | |
267 " }\n" | |
268 " id\n" | |
269 " title {\n" | |
270 " romaji\n" | |
271 " english\n" | |
272 " native\n" | |
273 " }\n" | |
274 " format\n" | |
275 " status\n" | |
276 " averageScore\n" | |
277 " season\n" | |
278 " startDate {\n" | |
279 " year\n" | |
280 " month\n" | |
281 " day\n" | |
282 " }\n" | |
283 " genres\n" | |
284 " episodes\n" | |
285 " duration\n" | |
286 " synonyms\n" | |
287 " description(asHtml: false)\n" | |
288 " }\n" | |
289 " }\n" | |
290 "}\n"; | |
250 | 291 |
292 // clang-format off | |
293 nlohmann::json json = { | |
294 {"query", query}, | |
295 {"variables", { | |
296 {"search", search} | |
297 }} | |
298 }; | |
299 // clang-format on | |
300 | |
301 auto res = SendJSONRequest(json); | |
302 | |
303 std::vector<int> ret; | |
304 ret.reserve(res["data"]["Page"]["media"].size()); | |
305 | |
306 for (const auto& media : res["data"]["Page"]["media"].items()) | |
307 ret.push_back(ParseMediaJson(media.value())); | |
308 | |
309 return ret; | |
310 } | |
311 | |
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
312 int UpdateAnimeEntry(int id) { |
9 | 313 /** |
314 * possible values: | |
15 | 315 * |
9 | 316 * int mediaId, |
317 * MediaListStatus status, | |
318 * float score, | |
319 * int scoreRaw, | |
320 * int progress, | |
184
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
321 * int progressVolumes, // manga-specific. |
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
322 * int repeat, // rewatch |
258 | 323 * int priority, |
9 | 324 * bool private, |
325 * string notes, | |
326 * bool hiddenFromStatusLists, | |
327 * string[] customLists, | |
328 * float[] advancedScores, | |
329 * Date startedAt, | |
330 * Date completedAt | |
258 | 331 **/ |
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
332 Anime::Anime& anime = Anime::db.items[id]; |
184
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
333 if (!anime.IsInUserList()) |
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
334 return 0; |
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
335 |
250 | 336 constexpr std::string_view query = |
258 | 337 "mutation ($media_id: Int, $progress: Int, $status: MediaListStatus, $score: Int, $notes: String, $start: " |
338 "FuzzyDateInput, $comp: FuzzyDateInput, $repeat: Int) {\n" | |
339 " SaveMediaListEntry (mediaId: $media_id, progress: $progress, status: $status, scoreRaw: $score, notes: " | |
340 "$notes, startedAt: $start, completedAt: $comp, repeat: $repeat) {\n" | |
341 " id\n" | |
342 " }\n" | |
343 "}\n"; | |
9 | 344 // clang-format off |
345 nlohmann::json json = { | |
346 {"query", query}, | |
347 {"variables", { | |
10 | 348 {"media_id", anime.GetId()}, |
349 {"progress", anime.GetUserProgress()}, | |
15 | 350 {"status", ListStatusToString(anime)}, |
10 | 351 {"score", anime.GetUserScore()}, |
77 | 352 {"notes", anime.GetUserNotes()}, |
353 {"start", anime.GetUserDateStarted().GetAsAniListJson()}, | |
184
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
354 {"comp", anime.GetUserDateCompleted().GetAsAniListJson()}, |
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
355 {"repeat", anime.GetUserRewatchedTimes()} |
9 | 356 }} |
357 }; | |
358 // clang-format on | |
175 | 359 |
360 auto ret = SendJSONRequest(json); | |
361 | |
362 return JSON::GetNumber(ret, "/data/SaveMediaListEntry/id"_json_pointer, 0); | |
9 | 363 } |
364 | |
365 int ParseUser(const nlohmann::json& json) { | |
175 | 366 account.SetUserId(JSON::GetNumber(json, "/id"_json_pointer, 0)); |
10 | 367 return account.UserId(); |
9 | 368 } |
369 | |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
370 bool AuthorizeUser() { |
9 | 371 /* Prompt for PIN */ |
258 | 372 QDesktopServices::openUrl(QUrl(Strings::ToQString("https://anilist.co/api/v2/oauth/authorize?client_id=" + |
373 Strings::ToUtf8String(CLIENT_ID) + "&response_type=token"))); | |
175 | 374 |
9 | 375 bool ok; |
376 QString token = QInputDialog::getText( | |
258 | 377 0, "Credentials needed!", "Please enter the code given to you after logging in to AniList:", QLineEdit::Normal, |
378 "", &ok); | |
175 | 379 |
380 if (!ok || token.isEmpty()) | |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
381 return false; |
175 | 382 |
383 account.SetAuthToken(Strings::ToUtf8String(token)); | |
384 | |
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
385 constexpr std::string_view query = "query {\n" |
258 | 386 " Viewer {\n" |
387 " id\n" | |
388 " name\n" | |
389 " mediaListOptions {\n" | |
390 " scoreFormat\n" // this will be used... eventually | |
391 " }\n" | |
392 " }\n" | |
393 "}\n"; | |
9 | 394 nlohmann::json json = { |
258 | 395 {"query", query} |
396 }; | |
175 | 397 |
398 auto ret = SendJSONRequest(json); | |
399 | |
74 | 400 ParseUser(ret["data"]["Viewer"]); |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
401 return true; |
9 | 402 } |
403 | |
63 | 404 } // namespace AniList |
405 } // namespace Services |