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