Mercurial > minori
annotate src/services/anilist.cpp @ 69:27a19dd6cba1
*: fix up some stuff
settings: due to my own incompetence OnOK caused the entire program to crash,
now that's fixed :)
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Tue, 03 Oct 2023 03:38:25 -0400 |
| parents | 6481c5aed3e1 |
| children | 64e5f427c6a2 |
| 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" |
| 5 #include "core/json.h" | |
| 6 #include "core/session.h" | |
| 7 #include "core/strings.h" | |
| 15 | 8 #include "gui/translate/anilist.h" |
| 9 | 9 #include <QDesktopServices> |
| 10 #include <QInputDialog> | |
| 11 #include <QLineEdit> | |
| 12 #include <QMessageBox> | |
| 10 | 13 #include <QUrl> |
| 9 | 14 #include <chrono> |
| 15 #include <curl/curl.h> | |
| 16 #include <exception> | |
| 17 #define CLIENT_ID "13706" | |
| 18 | |
| 64 | 19 using namespace nlohmann::literals::json_literals; |
| 11 | 20 |
| 63 | 21 namespace Services { |
| 22 namespace AniList { | |
| 9 | 23 |
| 24 class Account { | |
| 25 public: | |
| 10 | 26 std::string Username() const { return session.config.anilist.username; } |
| 36 | 27 void SetUsername(std::string const& username) { session.config.anilist.username = username; } |
| 9 | 28 |
| 10 | 29 int UserId() const { return session.config.anilist.user_id; } |
| 30 void SetUserId(const int id) { session.config.anilist.user_id = id; } | |
| 9 | 31 |
| 10 | 32 std::string AuthToken() const { return session.config.anilist.auth_token; } |
| 36 | 33 void SetAuthToken(std::string const& auth_token) { session.config.anilist.auth_token = auth_token; } |
| 9 | 34 |
| 35 bool Authenticated() const { return !AuthToken().empty(); } | |
| 10 | 36 }; |
| 9 | 37 |
| 38 static Account account; | |
| 39 | |
| 40 static size_t CurlWriteCallback(void* contents, size_t size, size_t nmemb, void* userdata) { | |
| 64 | 41 reinterpret_cast<std::string*>(userdata)->append(reinterpret_cast<char*>(contents), size * nmemb); |
| 9 | 42 return size * nmemb; |
| 43 } | |
| 44 | |
| 45 /* A wrapper around cURL to send requests to AniList */ | |
| 46 std::string SendRequest(std::string data) { | |
| 47 struct curl_slist* list = NULL; | |
| 48 std::string userdata; | |
| 49 CURL* curl = curl_easy_init(); | |
| 50 if (curl) { | |
| 15 | 51 std::string bearer = "Authorization: Bearer " + account.AuthToken(); |
| 9 | 52 list = curl_slist_append(list, "Accept: application/json"); |
| 53 list = curl_slist_append(list, "Content-Type: application/json"); | |
| 54 list = curl_slist_append(list, bearer.c_str()); | |
| 55 curl_easy_setopt(curl, CURLOPT_URL, "https://graphql.anilist.co"); | |
| 56 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str()); | |
| 57 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); | |
| 58 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &userdata); | |
| 59 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &CurlWriteCallback); | |
| 60 /* Use system certs... useful on Windows. */ | |
| 61 curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA); | |
| 62 CURLcode res = curl_easy_perform(curl); | |
|
48
e613772f41d5
statistics.cpp: show requests made
Paper <mrpapersonic@gmail.com>
parents:
47
diff
changeset
|
63 session.IncrementRequests(); |
| 9 | 64 curl_slist_free_all(list); |
| 65 curl_easy_cleanup(curl); | |
| 66 if (res != CURLE_OK) { | |
| 67 QMessageBox box(QMessageBox::Icon::Critical, "", | |
| 36 | 68 QString("curl_easy_perform(curl) failed!: ") + QString(curl_easy_strerror(res))); |
| 9 | 69 box.exec(); |
| 70 return ""; | |
| 71 } | |
| 72 return userdata; | |
| 73 } | |
| 74 return ""; | |
| 75 } | |
| 76 | |
| 15 | 77 void ParseListStatus(std::string status, Anime::Anime& anime) { |
| 78 std::unordered_map<std::string, Anime::ListStatus> map = { | |
| 79 {"CURRENT", Anime::ListStatus::CURRENT }, | |
| 80 {"PLANNING", Anime::ListStatus::PLANNING }, | |
| 81 {"COMPLETED", Anime::ListStatus::COMPLETED}, | |
| 82 {"DROPPED", Anime::ListStatus::DROPPED }, | |
| 83 {"PAUSED", Anime::ListStatus::PAUSED } | |
| 84 }; | |
| 9 | 85 |
| 15 | 86 if (status == "REPEATING") { |
| 87 anime.SetUserIsRewatching(true); | |
| 88 anime.SetUserStatus(Anime::ListStatus::CURRENT); | |
| 89 return; | |
| 90 } | |
| 9 | 91 |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
92 if (map.find(status) == map.end()) { |
| 15 | 93 anime.SetUserStatus(Anime::ListStatus::NOT_IN_LIST); |
| 94 return; | |
| 95 } | |
| 9 | 96 |
| 15 | 97 anime.SetUserStatus(map[status]); |
| 98 } | |
| 9 | 99 |
| 15 | 100 std::string ListStatusToString(const Anime::Anime& anime) { |
| 101 std::unordered_map<Anime::ListStatus, std::string> map = { | |
| 102 {Anime::ListStatus::CURRENT, "CURRENT" }, | |
| 103 {Anime::ListStatus::PLANNING, "PLANNING" }, | |
| 104 {Anime::ListStatus::COMPLETED, "COMPLETED"}, | |
| 105 {Anime::ListStatus::DROPPED, "DROPPED" }, | |
| 106 {Anime::ListStatus::PAUSED, "PAUSED" } | |
| 107 }; | |
| 9 | 108 |
| 15 | 109 if (anime.GetUserIsRewatching()) |
| 110 return "REWATCHING"; | |
| 111 | |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
112 if (map.find(anime.GetUserStatus()) == map.end()) |
| 15 | 113 return "CURRENT"; |
| 114 return map[anime.GetUserStatus()]; | |
| 115 } | |
| 9 | 116 |
| 10 | 117 Date ParseDate(const nlohmann::json& json) { |
| 118 Date date; | |
| 64 | 119 /* JSON for Modern C++ warns here. I'm not too sure why, this code works when I set the |
| 120 standard to C++17 :/ */ | |
| 11 | 121 if (json.contains("/year"_json_pointer) && json.at("/year"_json_pointer).is_number()) |
| 9 | 122 date.SetYear(JSON::GetInt(json, "/year"_json_pointer)); |
| 123 else | |
| 124 date.VoidYear(); | |
| 125 | |
| 11 | 126 if (json.contains("/month"_json_pointer) && json.at("/month"_json_pointer).is_number()) |
| 9 | 127 date.SetMonth(JSON::GetInt(json, "/month"_json_pointer)); |
| 128 else | |
| 129 date.VoidMonth(); | |
| 130 | |
| 11 | 131 if (json.contains("/day"_json_pointer) && json.at("/day"_json_pointer).is_number()) |
| 9 | 132 date.SetDay(JSON::GetInt(json, "/day"_json_pointer)); |
| 133 else | |
| 134 date.VoidDay(); | |
| 10 | 135 return date; |
| 9 | 136 } |
| 137 | |
| 138 void ParseTitle(const nlohmann::json& json, Anime::Anime& anime) { | |
| 139 anime.SetNativeTitle(JSON::GetString(json, "/native"_json_pointer)); | |
| 140 anime.SetEnglishTitle(JSON::GetString(json, "/english"_json_pointer)); | |
| 141 anime.SetRomajiTitle(JSON::GetString(json, "/romaji"_json_pointer)); | |
| 142 } | |
| 143 | |
| 144 int ParseMediaJson(const nlohmann::json& json) { | |
| 145 int id = JSON::GetInt(json, "/id"_json_pointer); | |
| 146 if (!id) | |
| 147 return 0; | |
| 148 Anime::Anime& anime = Anime::db.items[id]; | |
| 149 anime.SetId(id); | |
| 150 | |
| 11 | 151 ParseTitle(json.at("/title"_json_pointer), anime); |
| 9 | 152 |
| 153 anime.SetEpisodes(JSON::GetInt(json, "/episodes"_json_pointer)); | |
| 15 | 154 anime.SetFormat(Translate::AniList::ToSeriesFormat(JSON::GetString(json, "/format"_json_pointer))); |
| 9 | 155 |
| 15 | 156 anime.SetAiringStatus(Translate::AniList::ToSeriesStatus(JSON::GetString(json, "/status"_json_pointer))); |
| 9 | 157 |
| 10 | 158 anime.SetAirDate(ParseDate(json["/startDate"_json_pointer])); |
| 9 | 159 |
|
66
6481c5aed3e1
posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
65
diff
changeset
|
160 anime.SetPosterUrl(JSON::GetString(json, "/coverImage/large"_json_pointer)); |
|
6481c5aed3e1
posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
65
diff
changeset
|
161 |
| 9 | 162 anime.SetAudienceScore(JSON::GetInt(json, "/averageScore"_json_pointer)); |
| 15 | 163 anime.SetSeason(Translate::AniList::ToSeriesSeason(JSON::GetString(json, "/season"_json_pointer))); |
| 9 | 164 anime.SetDuration(JSON::GetInt(json, "/duration"_json_pointer)); |
| 10 | 165 anime.SetSynopsis(Strings::TextifySynopsis(JSON::GetString(json, "/description"_json_pointer))); |
| 9 | 166 |
| 167 if (json.contains("/genres"_json_pointer) && json["/genres"_json_pointer].is_array()) | |
| 168 anime.SetGenres(json["/genres"_json_pointer].get<std::vector<std::string>>()); | |
| 169 if (json.contains("/synonyms"_json_pointer) && json["/synonyms"_json_pointer].is_array()) | |
| 10 | 170 anime.SetTitleSynonyms(json["/synonyms"_json_pointer].get<std::vector<std::string>>()); |
| 171 return id; | |
| 9 | 172 } |
| 173 | |
| 10 | 174 int ParseListItem(const nlohmann::json& json) { |
| 175 int id = ParseMediaJson(json["media"]); | |
| 176 | |
| 177 Anime::Anime& anime = Anime::db.items[id]; | |
| 178 | |
| 179 anime.AddToUserList(); | |
| 9 | 180 |
| 10 | 181 anime.SetUserScore(JSON::GetInt(json, "/score"_json_pointer)); |
| 182 anime.SetUserProgress(JSON::GetInt(json, "/progress"_json_pointer)); | |
| 15 | 183 ParseListStatus(JSON::GetString(json, "/status"_json_pointer), anime); |
| 10 | 184 anime.SetUserNotes(JSON::GetString(json, "/notes"_json_pointer)); |
| 9 | 185 |
| 10 | 186 anime.SetUserDateStarted(ParseDate(json["/startedAt"_json_pointer])); |
| 187 anime.SetUserDateCompleted(ParseDate(json["/completedAt"_json_pointer])); | |
| 9 | 188 |
| 10 | 189 anime.SetUserTimeUpdated(JSON::GetInt(json, "/updatedAt"_json_pointer)); |
| 190 | |
| 191 return id; | |
| 9 | 192 } |
| 193 | |
| 194 int ParseList(const nlohmann::json& json) { | |
| 195 for (const auto& entry : json["entries"].items()) { | |
| 196 ParseListItem(entry.value()); | |
| 197 } | |
| 10 | 198 return 1; |
| 9 | 199 } |
| 200 | |
| 10 | 201 int GetAnimeList() { |
| 9 | 202 /* NOTE: these should be in the qrc file */ |
| 203 const std::string query = "query ($id: Int) {\n" | |
| 15 | 204 " MediaListCollection (userId: $id, type: ANIME) {\n" |
| 205 " lists {\n" | |
| 206 " name\n" | |
| 207 " entries {\n" | |
| 208 " score\n" | |
| 209 " notes\n" | |
| 210 " status\n" | |
| 211 " progress\n" | |
| 212 " startedAt {\n" | |
| 213 " year\n" | |
| 214 " month\n" | |
| 215 " day\n" | |
| 216 " }\n" | |
| 217 " completedAt {\n" | |
| 218 " year\n" | |
| 219 " month\n" | |
| 220 " day\n" | |
| 221 " }\n" | |
| 222 " updatedAt\n" | |
| 223 " media {\n" | |
|
66
6481c5aed3e1
posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
65
diff
changeset
|
224 " coverImage {\n" |
|
6481c5aed3e1
posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
65
diff
changeset
|
225 " large\n" |
|
6481c5aed3e1
posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
65
diff
changeset
|
226 " }\n" |
| 15 | 227 " id\n" |
| 228 " title {\n" | |
| 229 " romaji\n" | |
| 230 " english\n" | |
| 231 " native\n" | |
| 232 " }\n" | |
| 233 " format\n" | |
| 234 " status\n" | |
| 235 " averageScore\n" | |
| 236 " season\n" | |
| 237 " startDate {\n" | |
| 238 " year\n" | |
| 239 " month\n" | |
| 240 " day\n" | |
| 241 " }\n" | |
| 242 " genres\n" | |
| 243 " episodes\n" | |
| 244 " duration\n" | |
| 245 " synonyms\n" | |
| 246 " description(asHtml: false)\n" | |
| 247 " }\n" | |
| 248 " }\n" | |
| 249 " }\n" | |
| 250 " }\n" | |
| 251 "}\n"; | |
| 9 | 252 // clang-format off |
| 253 nlohmann::json json = { | |
| 254 {"query", query}, | |
| 255 {"variables", { | |
| 10 | 256 {"id", account.UserId()} |
| 9 | 257 }} |
| 258 }; | |
| 259 // clang-format on | |
| 260 /* TODO: do a try catch here, catch any json errors and then call | |
| 261 Authorize() if needed */ | |
| 262 auto res = nlohmann::json::parse(SendRequest(json.dump())); | |
| 263 /* TODO: make sure that we actually need the wstring converter and see | |
| 264 if we can just get wide strings back from nlohmann::json */ | |
| 265 for (const auto& list : res["data"]["MediaListCollection"]["lists"].items()) { | |
| 10 | 266 ParseList(list.value()); |
| 9 | 267 } |
| 268 return 1; | |
| 269 } | |
| 270 | |
|
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
271 int UpdateAnimeEntry(int id) { |
| 9 | 272 /** |
| 273 * possible values: | |
| 15 | 274 * |
| 9 | 275 * int mediaId, |
| 276 * MediaListStatus status, | |
| 277 * float score, | |
| 278 * int scoreRaw, | |
| 279 * int progress, | |
| 280 * int progressVolumes, | |
| 281 * int repeat, | |
| 282 * int priority, | |
| 283 * bool private, | |
| 284 * string notes, | |
| 285 * bool hiddenFromStatusLists, | |
| 286 * string[] customLists, | |
| 287 * float[] advancedScores, | |
| 288 * Date startedAt, | |
| 289 * Date completedAt | |
| 15 | 290 **/ |
|
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
291 Anime::Anime& anime = Anime::db.items[id]; |
| 63 | 292 const std::string query = |
| 293 "mutation ($media_id: Int, $progress: Int, $status: MediaListStatus, $score: Int, $notes: String) {\n" | |
| 294 " SaveMediaListEntry (mediaId: $media_id, progress: $progress, status: $status, scoreRaw: $score, notes: " | |
| 295 "$notes) {\n" | |
| 296 " id\n" | |
| 297 " }\n" | |
| 298 "}\n"; | |
| 9 | 299 // clang-format off |
| 300 nlohmann::json json = { | |
| 301 {"query", query}, | |
| 302 {"variables", { | |
| 10 | 303 {"media_id", anime.GetId()}, |
| 304 {"progress", anime.GetUserProgress()}, | |
| 15 | 305 {"status", ListStatusToString(anime)}, |
| 10 | 306 {"score", anime.GetUserScore()}, |
| 307 {"notes", anime.GetUserNotes()} | |
| 9 | 308 }} |
| 309 }; | |
| 310 // clang-format on | |
|
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
311 auto ret = nlohmann::json::parse(SendRequest(json.dump())); |
|
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
312 return JSON::GetInt(ret, "/data/SaveMediaListEntry/id"_json_pointer); |
| 9 | 313 } |
| 314 | |
| 315 int ParseUser(const nlohmann::json& json) { | |
| 316 account.SetUsername(JSON::GetString(json, "/name"_json_pointer)); | |
| 317 account.SetUserId(JSON::GetInt(json, "/id"_json_pointer)); | |
| 10 | 318 return account.UserId(); |
| 9 | 319 } |
| 320 | |
|
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
321 bool AuthorizeUser() { |
| 9 | 322 /* Prompt for PIN */ |
| 36 | 323 QDesktopServices::openUrl( |
| 324 QUrl("https://anilist.co/api/v2/oauth/authorize?client_id=" CLIENT_ID "&response_type=token")); | |
| 9 | 325 bool ok; |
| 326 QString token = QInputDialog::getText( | |
| 36 | 327 0, "Credentials needed!", "Please enter the code given to you after logging in to AniList:", QLineEdit::Normal, |
| 328 "", &ok); | |
| 9 | 329 if (ok && !token.isEmpty()) |
|
65
26721c28bf22
*: avoid usage of (to|from)StdString
Paper <mrpapersonic@gmail.com>
parents:
64
diff
changeset
|
330 account.SetAuthToken(Strings::ToUtf8String(token)); |
| 15 | 331 else // fail |
|
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
332 return false; |
| 9 | 333 const std::string query = "query {\n" |
| 15 | 334 " Viewer {\n" |
| 335 " id\n" | |
| 336 " name\n" | |
| 337 " mediaListOptions {\n" | |
| 338 " scoreFormat\n" | |
| 339 " }\n" | |
| 340 " }\n" | |
| 341 "}\n"; | |
| 9 | 342 nlohmann::json json = { |
| 63 | 343 {"query", query} |
| 344 }; | |
| 9 | 345 auto ret = nlohmann::json::parse(SendRequest(json.dump())); |
| 10 | 346 ParseUser(json["Viewer"]); |
|
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
347 return true; |
| 9 | 348 } |
| 349 | |
| 63 | 350 } // namespace AniList |
| 351 } // namespace Services |
