Mercurial > minori
annotate src/services/anilist.cc @ 176:121c2d5b321f
anime/db: finalize anime db cache
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Fri, 01 Dec 2023 13:12:26 -0500 |
| parents | 9b10175be389 |
| children | 01d259b9c89f |
| 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" |
| 77 | 10 #include <QByteArray> |
| 9 | 11 #include <QDesktopServices> |
| 12 #include <QInputDialog> | |
| 13 #include <QLineEdit> | |
| 14 #include <QMessageBox> | |
| 10 | 15 #include <QUrl> |
| 9 | 16 #include <chrono> |
| 17 #include <exception> | |
| 175 | 18 |
| 19 #include <iostream> | |
| 20 | |
| 9 | 21 #define CLIENT_ID "13706" |
| 22 | |
| 64 | 23 using namespace nlohmann::literals::json_literals; |
| 11 | 24 |
| 63 | 25 namespace Services { |
| 26 namespace AniList { | |
| 9 | 27 |
| 28 class Account { | |
| 29 public: | |
| 120 | 30 std::string Username() const { return session.config.auth.anilist.username; } |
| 31 void SetUsername(std::string const& username) { session.config.auth.anilist.username = username; } | |
| 9 | 32 |
| 120 | 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 |
| 120 | 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 |
| 39 bool Authenticated() const { return !AuthToken().empty(); } | |
| 175 | 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", |
| 175 | 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)) | |
| 66 std::cerr << "[AniList] Received an error in response: " << JSON::GetString<std::string>(error, "/message"_json_pointer, "") << std::endl; | |
| 67 | |
| 68 return {}; | |
| 69 } | |
| 70 | |
| 71 return ret; | |
| 72 } | |
| 73 | |
| 15 | 74 void ParseListStatus(std::string status, Anime::Anime& anime) { |
| 75 std::unordered_map<std::string, Anime::ListStatus> map = { | |
| 175 | 76 {"CURRENT", Anime::ListStatus::CURRENT }, |
| 77 {"PLANNING", Anime::ListStatus::PLANNING }, | |
| 78 {"COMPLETED", Anime::ListStatus::COMPLETED}, | |
| 79 {"DROPPED", Anime::ListStatus::DROPPED }, | |
| 80 {"PAUSED", Anime::ListStatus::PAUSED } | |
| 81 }; | |
| 9 | 82 |
| 15 | 83 if (status == "REPEATING") { |
| 84 anime.SetUserIsRewatching(true); | |
| 85 anime.SetUserStatus(Anime::ListStatus::CURRENT); | |
| 86 return; | |
| 87 } | |
| 9 | 88 |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
89 if (map.find(status) == map.end()) { |
| 15 | 90 anime.SetUserStatus(Anime::ListStatus::NOT_IN_LIST); |
| 91 return; | |
| 92 } | |
| 9 | 93 |
| 15 | 94 anime.SetUserStatus(map[status]); |
| 95 } | |
| 9 | 96 |
| 15 | 97 std::string ListStatusToString(const Anime::Anime& anime) { |
| 98 if (anime.GetUserIsRewatching()) | |
| 99 return "REWATCHING"; | |
| 100 | |
|
70
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
101 switch (anime.GetUserStatus()) { |
| 76 | 102 case Anime::ListStatus::PLANNING: return "PLANNING"; |
| 103 case Anime::ListStatus::COMPLETED: return "COMPLETED"; | |
| 104 case Anime::ListStatus::DROPPED: return "DROPPED"; | |
| 105 case Anime::ListStatus::PAUSED: return "PAUSED"; | |
| 106 default: break; | |
|
70
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
107 } |
|
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
108 return "CURRENT"; |
| 15 | 109 } |
| 9 | 110 |
| 111 void ParseTitle(const nlohmann::json& json, Anime::Anime& anime) { | |
| 175 | 112 anime.SetNativeTitle(JSON::GetString<std::string>(json, "/native"_json_pointer, "")); |
| 113 anime.SetEnglishTitle(JSON::GetString<std::string>(json, "/english"_json_pointer, "")); | |
| 114 anime.SetRomajiTitle(JSON::GetString<std::string>(json, "/romaji"_json_pointer, "")); | |
| 9 | 115 } |
| 116 | |
| 117 int ParseMediaJson(const nlohmann::json& json) { | |
| 175 | 118 int id = JSON::GetNumber(json, "/id"_json_pointer); |
| 9 | 119 if (!id) |
| 120 return 0; | |
| 175 | 121 |
| 9 | 122 Anime::Anime& anime = Anime::db.items[id]; |
| 123 anime.SetId(id); | |
| 124 | |
| 11 | 125 ParseTitle(json.at("/title"_json_pointer), anime); |
| 9 | 126 |
| 175 | 127 anime.SetEpisodes(JSON::GetNumber(json, "/episodes"_json_pointer, 0)); |
| 128 anime.SetFormat(Translate::AniList::ToSeriesFormat(JSON::GetString<std::string>(json, "/format"_json_pointer, ""))); | |
| 9 | 129 |
| 175 | 130 anime.SetAiringStatus(Translate::AniList::ToSeriesStatus(JSON::GetString<std::string>(json, "/status"_json_pointer, ""))); |
| 9 | 131 |
| 175 | 132 anime.SetAirDate(Date(json["/startDate"_json_pointer])); |
| 9 | 133 |
| 175 | 134 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
|
135 |
| 175 | 136 anime.SetAudienceScore(JSON::GetNumber(json, "/averageScore"_json_pointer, 0)); |
| 137 anime.SetSeason(Translate::AniList::ToSeriesSeason(JSON::GetString<std::string>(json, "/season"_json_pointer, ""))); | |
| 138 anime.SetDuration(JSON::GetNumber(json, "/duration"_json_pointer, 0)); | |
| 139 anime.SetSynopsis(Strings::TextifySynopsis(JSON::GetString<std::string>(json, "/description"_json_pointer, ""))); | |
| 9 | 140 |
| 175 | 141 anime.SetGenres(JSON::GetArray<std::vector<std::string>>(json, "/genres"_json_pointer, {})); |
| 142 anime.SetTitleSynonyms(JSON::GetArray<std::vector<std::string>>(json, "/synonyms"_json_pointer, {})); | |
| 143 | |
| 10 | 144 return id; |
| 9 | 145 } |
| 146 | |
| 10 | 147 int ParseListItem(const nlohmann::json& json) { |
| 148 int id = ParseMediaJson(json["media"]); | |
| 149 | |
| 150 Anime::Anime& anime = Anime::db.items[id]; | |
| 151 | |
| 152 anime.AddToUserList(); | |
| 9 | 153 |
| 175 | 154 anime.SetUserScore(JSON::GetNumber(json, "/score"_json_pointer, 0)); |
| 155 anime.SetUserProgress(JSON::GetNumber(json, "/progress"_json_pointer, 0)); | |
| 156 ParseListStatus(JSON::GetString<std::string>(json, "/status"_json_pointer, ""), anime); | |
| 157 anime.SetUserNotes(JSON::GetString<std::string>(json, "/notes"_json_pointer, "")); | |
| 9 | 158 |
| 175 | 159 anime.SetUserDateStarted(Date(json["/startedAt"_json_pointer])); |
| 160 anime.SetUserDateCompleted(Date(json["/completedAt"_json_pointer])); | |
| 9 | 161 |
| 175 | 162 anime.SetUserTimeUpdated(JSON::GetNumber(json, "/updatedAt"_json_pointer, 0)); |
| 10 | 163 |
| 164 return id; | |
| 9 | 165 } |
| 166 | |
| 167 int ParseList(const nlohmann::json& json) { | |
| 168 for (const auto& entry : json["entries"].items()) { | |
| 169 ParseListItem(entry.value()); | |
| 170 } | |
| 10 | 171 return 1; |
| 9 | 172 } |
| 173 | |
| 10 | 174 int GetAnimeList() { |
| 175 | 175 if (!account.IsValid()) { |
| 176 std::cerr << "AniList: Account isn't valid!" << std::endl; | |
| 177 return 0; | |
| 178 } | |
| 179 | |
| 9 | 180 /* NOTE: these should be in the qrc file */ |
| 181 const std::string query = "query ($id: Int) {\n" | |
| 175 | 182 " MediaListCollection (userId: $id, type: ANIME) {\n" |
| 183 " lists {\n" | |
| 184 " name\n" | |
| 185 " entries {\n" | |
| 186 " score\n" | |
| 187 " notes\n" | |
| 188 " status\n" | |
| 189 " progress\n" | |
| 190 " startedAt {\n" | |
| 191 " year\n" | |
| 192 " month\n" | |
| 193 " day\n" | |
| 194 " }\n" | |
| 195 " completedAt {\n" | |
| 196 " year\n" | |
| 197 " month\n" | |
| 198 " day\n" | |
| 199 " }\n" | |
| 200 " updatedAt\n" | |
| 201 " media {\n" | |
| 202 " coverImage {\n" | |
| 203 " large\n" | |
| 204 " }\n" | |
| 205 " id\n" | |
| 206 " title {\n" | |
| 207 " romaji\n" | |
| 208 " english\n" | |
| 209 " native\n" | |
| 210 " }\n" | |
| 211 " format\n" | |
| 212 " status\n" | |
| 213 " averageScore\n" | |
| 214 " season\n" | |
| 215 " startDate {\n" | |
| 216 " year\n" | |
| 217 " month\n" | |
| 218 " day\n" | |
| 219 " }\n" | |
| 220 " genres\n" | |
| 221 " episodes\n" | |
| 222 " duration\n" | |
| 223 " synonyms\n" | |
| 224 " description(asHtml: false)\n" | |
| 225 " }\n" | |
| 226 " }\n" | |
| 227 " }\n" | |
| 228 " }\n" | |
| 229 "}\n"; | |
| 9 | 230 // clang-format off |
| 231 nlohmann::json json = { | |
| 232 {"query", query}, | |
| 233 {"variables", { | |
| 10 | 234 {"id", account.UserId()} |
| 9 | 235 }} |
| 236 }; | |
| 237 // clang-format on | |
| 175 | 238 |
| 239 auto res = SendJSONRequest(json); | |
| 240 | |
| 9 | 241 for (const auto& list : res["data"]["MediaListCollection"]["lists"].items()) { |
| 10 | 242 ParseList(list.value()); |
| 9 | 243 } |
| 244 return 1; | |
| 245 } | |
| 246 | |
|
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
247 int UpdateAnimeEntry(int id) { |
| 9 | 248 /** |
| 249 * possible values: | |
| 15 | 250 * |
| 9 | 251 * int mediaId, |
| 252 * MediaListStatus status, | |
| 253 * float score, | |
| 254 * int scoreRaw, | |
| 255 * int progress, | |
| 256 * int progressVolumes, | |
| 257 * int repeat, | |
| 258 * int priority, | |
| 259 * bool private, | |
| 260 * string notes, | |
| 261 * bool hiddenFromStatusLists, | |
| 262 * string[] customLists, | |
| 263 * float[] advancedScores, | |
| 264 * Date startedAt, | |
| 265 * Date completedAt | |
| 15 | 266 **/ |
|
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
267 Anime::Anime& anime = Anime::db.items[id]; |
| 77 | 268 const std::string query = "mutation ($media_id: Int, $progress: Int, $status: MediaListStatus, $score: Int, " |
| 175 | 269 "$notes: String, $start: FuzzyDateInput, $comp: FuzzyDateInput) {\n" |
| 270 " SaveMediaListEntry (mediaId: $media_id, progress: $progress, status: $status, " | |
| 271 "scoreRaw: $score, notes: $notes, startedAt: $start, completedAt: $comp) {\n" | |
| 272 " id\n" | |
| 273 " }\n" | |
| 274 "}\n"; | |
| 9 | 275 // clang-format off |
| 276 nlohmann::json json = { | |
| 277 {"query", query}, | |
| 278 {"variables", { | |
| 10 | 279 {"media_id", anime.GetId()}, |
| 280 {"progress", anime.GetUserProgress()}, | |
| 15 | 281 {"status", ListStatusToString(anime)}, |
| 10 | 282 {"score", anime.GetUserScore()}, |
| 77 | 283 {"notes", anime.GetUserNotes()}, |
| 284 {"start", anime.GetUserDateStarted().GetAsAniListJson()}, | |
| 285 {"comp", anime.GetUserDateCompleted().GetAsAniListJson()} | |
| 9 | 286 }} |
| 287 }; | |
| 288 // clang-format on | |
| 175 | 289 |
| 290 auto ret = SendJSONRequest(json); | |
| 291 | |
| 292 return JSON::GetNumber(ret, "/data/SaveMediaListEntry/id"_json_pointer, 0); | |
| 9 | 293 } |
| 294 | |
| 295 int ParseUser(const nlohmann::json& json) { | |
| 175 | 296 account.SetUsername(JSON::GetString<std::string>(json, "/name"_json_pointer, "")); |
| 297 account.SetUserId(JSON::GetNumber(json, "/id"_json_pointer, 0)); | |
| 10 | 298 return account.UserId(); |
| 9 | 299 } |
| 300 | |
|
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
301 bool AuthorizeUser() { |
| 9 | 302 /* Prompt for PIN */ |
| 36 | 303 QDesktopServices::openUrl( |
| 175 | 304 QUrl("https://anilist.co/api/v2/oauth/authorize?client_id=" CLIENT_ID "&response_type=token")); |
| 305 | |
| 9 | 306 bool ok; |
| 307 QString token = QInputDialog::getText( | |
| 175 | 308 0, "Credentials needed!", "Please enter the code given to you after logging in to AniList:", QLineEdit::Normal, |
| 309 "", &ok); | |
| 310 | |
| 311 if (!ok || token.isEmpty()) | |
|
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
312 return false; |
| 175 | 313 |
| 314 account.SetAuthToken(Strings::ToUtf8String(token)); | |
| 315 | |
| 9 | 316 const std::string query = "query {\n" |
| 175 | 317 " Viewer {\n" |
| 318 " id\n" | |
| 319 " name\n" | |
| 320 " mediaListOptions {\n" | |
| 321 " scoreFormat\n" // this will be used... eventually | |
| 322 " }\n" | |
| 323 " }\n" | |
| 324 "}\n"; | |
| 9 | 325 nlohmann::json json = { |
| 175 | 326 {"query", query} |
| 327 }; | |
| 328 | |
| 329 auto ret = SendJSONRequest(json); | |
| 330 | |
| 74 | 331 ParseUser(ret["data"]["Viewer"]); |
|
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
332 return true; |
| 9 | 333 } |
| 334 | |
| 63 | 335 } // namespace AniList |
| 336 } // namespace Services |
