Mercurial > minori
annotate src/services/anilist.cc @ 258:862d0d8619f6
*: HUUUGE changes
animia has been renamed to animone, so instead of thinking of a
health condition, you think of a beautiful flower :)
I've also edited some of the code for animone, but I have no idea
if it even works or not because I don't have a mac or windows
machine lying around. whoops!
... anyway, all of the changes divergent from Anisthesia are now
licensed under BSD. it's possible that I could even rewrite most
of the code to where I don't even have to keep the MIT license,
but that's thinking too far into the future
I've been slacking off on implementing the anime seasons page,
mostly out of laziness. I think I'd have to create another db file
specifically for the seasons
anyway, this code is being pushed *primarily* because the hard drive
it's on is failing! yay :)
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Mon, 01 Apr 2024 02:43:44 -0400 |
parents | c130f47f6f48 |
children | dd211ff68b36 |
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 = { |
258 | 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 } | |
82 }; | |
9 | 83 |
15 | 84 if (status == "REPEATING") { |
85 anime.SetUserIsRewatching(true); | |
86 anime.SetUserStatus(Anime::ListStatus::CURRENT); | |
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()) { |
15 | 91 anime.SetUserStatus(Anime::ListStatus::NOT_IN_LIST); |
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) { |
99 if (anime.GetUserIsRewatching()) | |
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()) { |
76 | 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"; | |
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) { | |
175 | 113 anime.SetNativeTitle(JSON::GetString<std::string>(json, "/native"_json_pointer, "")); |
114 anime.SetEnglishTitle(JSON::GetString<std::string>(json, "/english"_json_pointer, "")); | |
115 anime.SetRomajiTitle(JSON::GetString<std::string>(json, "/romaji"_json_pointer, "")); | |
9 | 116 } |
117 | |
118 int ParseMediaJson(const nlohmann::json& json) { | |
175 | 119 int id = JSON::GetNumber(json, "/id"_json_pointer); |
9 | 120 if (!id) |
121 return 0; | |
175 | 122 |
9 | 123 Anime::Anime& anime = Anime::db.items[id]; |
124 anime.SetId(id); | |
125 | |
11 | 126 ParseTitle(json.at("/title"_json_pointer), anime); |
9 | 127 |
175 | 128 anime.SetEpisodes(JSON::GetNumber(json, "/episodes"_json_pointer, 0)); |
129 anime.SetFormat(Translate::AniList::ToSeriesFormat(JSON::GetString<std::string>(json, "/format"_json_pointer, ""))); | |
9 | 130 |
258 | 131 anime.SetAiringStatus( |
132 Translate::AniList::ToSeriesStatus(JSON::GetString<std::string>(json, "/status"_json_pointer, ""))); | |
9 | 133 |
175 | 134 anime.SetAirDate(Date(json["/startDate"_json_pointer])); |
9 | 135 |
175 | 136 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
|
137 |
175 | 138 anime.SetAudienceScore(JSON::GetNumber(json, "/averageScore"_json_pointer, 0)); |
139 anime.SetSeason(Translate::AniList::ToSeriesSeason(JSON::GetString<std::string>(json, "/season"_json_pointer, ""))); | |
140 anime.SetDuration(JSON::GetNumber(json, "/duration"_json_pointer, 0)); | |
141 anime.SetSynopsis(Strings::TextifySynopsis(JSON::GetString<std::string>(json, "/description"_json_pointer, ""))); | |
9 | 142 |
175 | 143 anime.SetGenres(JSON::GetArray<std::vector<std::string>>(json, "/genres"_json_pointer, {})); |
144 anime.SetTitleSynonyms(JSON::GetArray<std::vector<std::string>>(json, "/synonyms"_json_pointer, {})); | |
145 | |
10 | 146 return id; |
9 | 147 } |
148 | |
10 | 149 int ParseListItem(const nlohmann::json& json) { |
150 int id = ParseMediaJson(json["media"]); | |
151 | |
152 Anime::Anime& anime = Anime::db.items[id]; | |
153 | |
154 anime.AddToUserList(); | |
9 | 155 |
175 | 156 anime.SetUserScore(JSON::GetNumber(json, "/score"_json_pointer, 0)); |
157 anime.SetUserProgress(JSON::GetNumber(json, "/progress"_json_pointer, 0)); | |
158 ParseListStatus(JSON::GetString<std::string>(json, "/status"_json_pointer, ""), anime); | |
159 anime.SetUserNotes(JSON::GetString<std::string>(json, "/notes"_json_pointer, "")); | |
9 | 160 |
175 | 161 anime.SetUserDateStarted(Date(json["/startedAt"_json_pointer])); |
162 anime.SetUserDateCompleted(Date(json["/completedAt"_json_pointer])); | |
9 | 163 |
175 | 164 anime.SetUserTimeUpdated(JSON::GetNumber(json, "/updatedAt"_json_pointer, 0)); |
10 | 165 |
166 return id; | |
9 | 167 } |
168 | |
169 int ParseList(const nlohmann::json& json) { | |
170 for (const auto& entry : json["entries"].items()) { | |
171 ParseListItem(entry.value()); | |
172 } | |
10 | 173 return 1; |
9 | 174 } |
175 | |
10 | 176 int GetAnimeList() { |
175 | 177 if (!account.IsValid()) { |
178 std::cerr << "AniList: Account isn't valid!" << std::endl; | |
179 return 0; | |
180 } | |
181 | |
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
182 /* 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
|
183 constexpr std::string_view query = "query ($id: Int) {\n" |
258 | 184 " MediaListCollection (userId: $id, type: ANIME) {\n" |
185 " lists {\n" | |
186 " name\n" | |
187 " entries {\n" | |
188 " score\n" | |
189 " notes\n" | |
190 " status\n" | |
191 " progress\n" | |
192 " startedAt {\n" | |
193 " year\n" | |
194 " month\n" | |
195 " day\n" | |
196 " }\n" | |
197 " completedAt {\n" | |
198 " year\n" | |
199 " month\n" | |
200 " day\n" | |
201 " }\n" | |
202 " updatedAt\n" | |
203 " media {\n" | |
204 " coverImage {\n" | |
205 " large\n" | |
206 " }\n" | |
207 " id\n" | |
208 " title {\n" | |
209 " romaji\n" | |
210 " english\n" | |
211 " native\n" | |
212 " }\n" | |
213 " format\n" | |
214 " status\n" | |
215 " averageScore\n" | |
216 " season\n" | |
217 " startDate {\n" | |
218 " year\n" | |
219 " month\n" | |
220 " day\n" | |
221 " }\n" | |
222 " genres\n" | |
223 " episodes\n" | |
224 " duration\n" | |
225 " synonyms\n" | |
226 " description(asHtml: false)\n" | |
227 " }\n" | |
228 " }\n" | |
229 " }\n" | |
230 " }\n" | |
231 "}\n"; | |
9 | 232 // clang-format off |
233 nlohmann::json json = { | |
234 {"query", query}, | |
235 {"variables", { | |
10 | 236 {"id", account.UserId()} |
9 | 237 }} |
238 }; | |
239 // clang-format on | |
175 | 240 |
241 auto res = SendJSONRequest(json); | |
242 | |
9 | 243 for (const auto& list : res["data"]["MediaListCollection"]["lists"].items()) { |
10 | 244 ParseList(list.value()); |
9 | 245 } |
246 return 1; | |
247 } | |
248 | |
250 | 249 /* return is a vector of anime ids */ |
250 std::vector<int> Search(const std::string& search) { | |
258 | 251 constexpr std::string_view query = "query ($search: String) {\n" |
252 " Page (page: 1, perPage: 50) {\n" | |
253 " media (search: $search, type: ANIME) {\n" | |
254 " coverImage {\n" | |
255 " large\n" | |
256 " }\n" | |
257 " id\n" | |
258 " title {\n" | |
259 " romaji\n" | |
260 " english\n" | |
261 " native\n" | |
262 " }\n" | |
263 " format\n" | |
264 " status\n" | |
265 " averageScore\n" | |
266 " season\n" | |
267 " startDate {\n" | |
268 " year\n" | |
269 " month\n" | |
270 " day\n" | |
271 " }\n" | |
272 " genres\n" | |
273 " episodes\n" | |
274 " duration\n" | |
275 " synonyms\n" | |
276 " description(asHtml: false)\n" | |
277 " }\n" | |
278 " }\n" | |
279 "}\n"; | |
250 | 280 |
281 // clang-format off | |
282 nlohmann::json json = { | |
283 {"query", query}, | |
284 {"variables", { | |
285 {"search", search} | |
286 }} | |
287 }; | |
288 // clang-format on | |
289 | |
290 auto res = SendJSONRequest(json); | |
291 | |
292 std::vector<int> ret; | |
293 ret.reserve(res["data"]["Page"]["media"].size()); | |
294 | |
295 for (const auto& media : res["data"]["Page"]["media"].items()) | |
296 ret.push_back(ParseMediaJson(media.value())); | |
297 | |
298 return ret; | |
299 } | |
300 | |
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
301 int UpdateAnimeEntry(int id) { |
9 | 302 /** |
303 * possible values: | |
15 | 304 * |
9 | 305 * int mediaId, |
306 * MediaListStatus status, | |
307 * float score, | |
308 * int scoreRaw, | |
309 * int progress, | |
184
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
310 * int progressVolumes, // manga-specific. |
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
311 * int repeat, // rewatch |
258 | 312 * int priority, |
9 | 313 * bool private, |
314 * string notes, | |
315 * bool hiddenFromStatusLists, | |
316 * string[] customLists, | |
317 * float[] advancedScores, | |
318 * Date startedAt, | |
319 * Date completedAt | |
258 | 320 **/ |
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
321 Anime::Anime& anime = Anime::db.items[id]; |
184
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
322 if (!anime.IsInUserList()) |
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
323 return 0; |
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
324 |
250 | 325 constexpr std::string_view query = |
258 | 326 "mutation ($media_id: Int, $progress: Int, $status: MediaListStatus, $score: Int, $notes: String, $start: " |
327 "FuzzyDateInput, $comp: FuzzyDateInput, $repeat: Int) {\n" | |
328 " SaveMediaListEntry (mediaId: $media_id, progress: $progress, status: $status, scoreRaw: $score, notes: " | |
329 "$notes, startedAt: $start, completedAt: $comp, repeat: $repeat) {\n" | |
330 " id\n" | |
331 " }\n" | |
332 "}\n"; | |
9 | 333 // clang-format off |
334 nlohmann::json json = { | |
335 {"query", query}, | |
336 {"variables", { | |
10 | 337 {"media_id", anime.GetId()}, |
338 {"progress", anime.GetUserProgress()}, | |
15 | 339 {"status", ListStatusToString(anime)}, |
10 | 340 {"score", anime.GetUserScore()}, |
77 | 341 {"notes", anime.GetUserNotes()}, |
342 {"start", anime.GetUserDateStarted().GetAsAniListJson()}, | |
184
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
343 {"comp", anime.GetUserDateCompleted().GetAsAniListJson()}, |
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
344 {"repeat", anime.GetUserRewatchedTimes()} |
9 | 345 }} |
346 }; | |
347 // clang-format on | |
175 | 348 |
349 auto ret = SendJSONRequest(json); | |
350 | |
351 return JSON::GetNumber(ret, "/data/SaveMediaListEntry/id"_json_pointer, 0); | |
9 | 352 } |
353 | |
354 int ParseUser(const nlohmann::json& json) { | |
175 | 355 account.SetUserId(JSON::GetNumber(json, "/id"_json_pointer, 0)); |
10 | 356 return account.UserId(); |
9 | 357 } |
358 | |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
359 bool AuthorizeUser() { |
9 | 360 /* Prompt for PIN */ |
258 | 361 QDesktopServices::openUrl(QUrl(Strings::ToQString("https://anilist.co/api/v2/oauth/authorize?client_id=" + |
362 Strings::ToUtf8String(CLIENT_ID) + "&response_type=token"))); | |
175 | 363 |
9 | 364 bool ok; |
365 QString token = QInputDialog::getText( | |
258 | 366 0, "Credentials needed!", "Please enter the code given to you after logging in to AniList:", QLineEdit::Normal, |
367 "", &ok); | |
175 | 368 |
369 if (!ok || token.isEmpty()) | |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
370 return false; |
175 | 371 |
372 account.SetAuthToken(Strings::ToUtf8String(token)); | |
373 | |
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
374 constexpr std::string_view query = "query {\n" |
258 | 375 " Viewer {\n" |
376 " id\n" | |
377 " name\n" | |
378 " mediaListOptions {\n" | |
379 " scoreFormat\n" // this will be used... eventually | |
380 " }\n" | |
381 " }\n" | |
382 "}\n"; | |
9 | 383 nlohmann::json json = { |
258 | 384 {"query", query} |
385 }; | |
175 | 386 |
387 auto ret = SendJSONRequest(json); | |
388 | |
74 | 389 ParseUser(ret["data"]["Viewer"]); |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
390 return true; |
9 | 391 } |
392 | |
63 | 393 } // namespace AniList |
394 } // namespace Services |