Mercurial > minori
annotate src/services/anilist.cpp @ 75:d3e9310598b1
*: refactor some stuff
text: "TextParagraph"s are now called sections, because that's the
actual word for it :P
text: new classes: Line and OneLineSection, solves many problems with
paragraphs that are only one line long (ex. going out of bounds)
http: reworked http stuff to allow threaded get requests, also moved it
to its own file to (hopefully) remove clutter
eventually I'll make a threaded post request method and use that in
the "basic" function
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Wed, 04 Oct 2023 01:42:30 -0400 |
| parents | 5ccb99bfa605 |
| children | 3364fadc8a36 |
| 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" | |
| 75 | 6 #include "core/http.h" |
| 9 | 7 #include "core/session.h" |
| 8 #include "core/strings.h" | |
| 15 | 9 #include "gui/translate/anilist.h" |
| 9 | 10 #include <QDesktopServices> |
| 11 #include <QInputDialog> | |
| 12 #include <QLineEdit> | |
| 13 #include <QMessageBox> | |
| 10 | 14 #include <QUrl> |
| 9 | 15 #include <chrono> |
| 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 std::string SendRequest(std::string data) { | |
| 75 | 41 std::vector<std::string> headers = { |
| 42 "Authorization: Bearer " + account.AuthToken(), | |
| 43 "Accept: application/json", | |
| 44 "Content-Type: application/json" | |
| 45 }; | |
| 46 return HTTP::PerformBasicPostRequest("https://graphql.anilist.co", data, headers); | |
| 9 | 47 } |
| 48 | |
| 15 | 49 void ParseListStatus(std::string status, Anime::Anime& anime) { |
| 50 std::unordered_map<std::string, Anime::ListStatus> map = { | |
| 51 {"CURRENT", Anime::ListStatus::CURRENT }, | |
| 52 {"PLANNING", Anime::ListStatus::PLANNING }, | |
| 53 {"COMPLETED", Anime::ListStatus::COMPLETED}, | |
| 54 {"DROPPED", Anime::ListStatus::DROPPED }, | |
| 55 {"PAUSED", Anime::ListStatus::PAUSED } | |
| 56 }; | |
| 9 | 57 |
| 15 | 58 if (status == "REPEATING") { |
| 59 anime.SetUserIsRewatching(true); | |
| 60 anime.SetUserStatus(Anime::ListStatus::CURRENT); | |
| 61 return; | |
| 62 } | |
| 9 | 63 |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
64 if (map.find(status) == map.end()) { |
| 15 | 65 anime.SetUserStatus(Anime::ListStatus::NOT_IN_LIST); |
| 66 return; | |
| 67 } | |
| 9 | 68 |
| 15 | 69 anime.SetUserStatus(map[status]); |
| 70 } | |
| 9 | 71 |
| 15 | 72 std::string ListStatusToString(const Anime::Anime& anime) { |
| 73 if (anime.GetUserIsRewatching()) | |
| 74 return "REWATCHING"; | |
| 75 | |
|
70
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
76 switch (anime.GetUserStatus()) { |
|
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
77 case Anime::ListStatus::PLANNING: |
|
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
78 return "PLANNING"; |
|
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
79 case Anime::ListStatus::COMPLETED: |
|
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
80 return "COMPLETED"; |
|
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
81 case Anime::ListStatus::DROPPED: |
|
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
82 return "DROPPED"; |
|
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
83 case Anime::ListStatus::PAUSED: |
|
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
84 return "PAUSED"; |
|
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
85 default: |
|
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
86 break; |
|
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
87 } |
|
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
88 return "CURRENT"; |
| 15 | 89 } |
| 9 | 90 |
| 10 | 91 Date ParseDate(const nlohmann::json& json) { |
| 92 Date date; | |
| 64 | 93 /* JSON for Modern C++ warns here. I'm not too sure why, this code works when I set the |
| 94 standard to C++17 :/ */ | |
| 11 | 95 if (json.contains("/year"_json_pointer) && json.at("/year"_json_pointer).is_number()) |
| 9 | 96 date.SetYear(JSON::GetInt(json, "/year"_json_pointer)); |
| 97 else | |
| 98 date.VoidYear(); | |
| 99 | |
| 11 | 100 if (json.contains("/month"_json_pointer) && json.at("/month"_json_pointer).is_number()) |
| 9 | 101 date.SetMonth(JSON::GetInt(json, "/month"_json_pointer)); |
| 102 else | |
| 103 date.VoidMonth(); | |
| 104 | |
| 11 | 105 if (json.contains("/day"_json_pointer) && json.at("/day"_json_pointer).is_number()) |
| 9 | 106 date.SetDay(JSON::GetInt(json, "/day"_json_pointer)); |
| 107 else | |
| 108 date.VoidDay(); | |
| 10 | 109 return date; |
| 9 | 110 } |
| 111 | |
| 112 void ParseTitle(const nlohmann::json& json, Anime::Anime& anime) { | |
| 113 anime.SetNativeTitle(JSON::GetString(json, "/native"_json_pointer)); | |
| 114 anime.SetEnglishTitle(JSON::GetString(json, "/english"_json_pointer)); | |
| 115 anime.SetRomajiTitle(JSON::GetString(json, "/romaji"_json_pointer)); | |
| 116 } | |
| 117 | |
| 118 int ParseMediaJson(const nlohmann::json& json) { | |
| 119 int id = JSON::GetInt(json, "/id"_json_pointer); | |
| 120 if (!id) | |
| 121 return 0; | |
| 122 Anime::Anime& anime = Anime::db.items[id]; | |
| 123 anime.SetId(id); | |
| 124 | |
| 11 | 125 ParseTitle(json.at("/title"_json_pointer), anime); |
| 9 | 126 |
| 127 anime.SetEpisodes(JSON::GetInt(json, "/episodes"_json_pointer)); | |
| 15 | 128 anime.SetFormat(Translate::AniList::ToSeriesFormat(JSON::GetString(json, "/format"_json_pointer))); |
| 9 | 129 |
| 15 | 130 anime.SetAiringStatus(Translate::AniList::ToSeriesStatus(JSON::GetString(json, "/status"_json_pointer))); |
| 9 | 131 |
| 10 | 132 anime.SetAirDate(ParseDate(json["/startDate"_json_pointer])); |
| 9 | 133 |
|
66
6481c5aed3e1
posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
65
diff
changeset
|
134 anime.SetPosterUrl(JSON::GetString(json, "/coverImage/large"_json_pointer)); |
|
6481c5aed3e1
posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
65
diff
changeset
|
135 |
| 9 | 136 anime.SetAudienceScore(JSON::GetInt(json, "/averageScore"_json_pointer)); |
| 15 | 137 anime.SetSeason(Translate::AniList::ToSeriesSeason(JSON::GetString(json, "/season"_json_pointer))); |
| 9 | 138 anime.SetDuration(JSON::GetInt(json, "/duration"_json_pointer)); |
| 10 | 139 anime.SetSynopsis(Strings::TextifySynopsis(JSON::GetString(json, "/description"_json_pointer))); |
| 9 | 140 |
| 141 if (json.contains("/genres"_json_pointer) && json["/genres"_json_pointer].is_array()) | |
| 142 anime.SetGenres(json["/genres"_json_pointer].get<std::vector<std::string>>()); | |
| 143 if (json.contains("/synonyms"_json_pointer) && json["/synonyms"_json_pointer].is_array()) | |
| 10 | 144 anime.SetTitleSynonyms(json["/synonyms"_json_pointer].get<std::vector<std::string>>()); |
| 145 return id; | |
| 9 | 146 } |
| 147 | |
| 10 | 148 int ParseListItem(const nlohmann::json& json) { |
| 149 int id = ParseMediaJson(json["media"]); | |
| 150 | |
| 151 Anime::Anime& anime = Anime::db.items[id]; | |
| 152 | |
| 153 anime.AddToUserList(); | |
| 9 | 154 |
| 10 | 155 anime.SetUserScore(JSON::GetInt(json, "/score"_json_pointer)); |
| 156 anime.SetUserProgress(JSON::GetInt(json, "/progress"_json_pointer)); | |
| 15 | 157 ParseListStatus(JSON::GetString(json, "/status"_json_pointer), anime); |
| 10 | 158 anime.SetUserNotes(JSON::GetString(json, "/notes"_json_pointer)); |
| 9 | 159 |
| 10 | 160 anime.SetUserDateStarted(ParseDate(json["/startedAt"_json_pointer])); |
| 161 anime.SetUserDateCompleted(ParseDate(json["/completedAt"_json_pointer])); | |
| 9 | 162 |
| 10 | 163 anime.SetUserTimeUpdated(JSON::GetInt(json, "/updatedAt"_json_pointer)); |
| 164 | |
| 165 return id; | |
| 9 | 166 } |
| 167 | |
| 168 int ParseList(const nlohmann::json& json) { | |
| 169 for (const auto& entry : json["entries"].items()) { | |
| 170 ParseListItem(entry.value()); | |
| 171 } | |
| 10 | 172 return 1; |
| 9 | 173 } |
| 174 | |
| 10 | 175 int GetAnimeList() { |
| 9 | 176 /* NOTE: these should be in the qrc file */ |
| 177 const std::string query = "query ($id: Int) {\n" | |
| 15 | 178 " MediaListCollection (userId: $id, type: ANIME) {\n" |
| 179 " lists {\n" | |
| 180 " name\n" | |
| 181 " entries {\n" | |
| 182 " score\n" | |
| 183 " notes\n" | |
| 184 " status\n" | |
| 185 " progress\n" | |
| 186 " startedAt {\n" | |
| 187 " year\n" | |
| 188 " month\n" | |
| 189 " day\n" | |
| 190 " }\n" | |
| 191 " completedAt {\n" | |
| 192 " year\n" | |
| 193 " month\n" | |
| 194 " day\n" | |
| 195 " }\n" | |
| 196 " updatedAt\n" | |
| 197 " media {\n" | |
|
66
6481c5aed3e1
posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
65
diff
changeset
|
198 " coverImage {\n" |
|
6481c5aed3e1
posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
65
diff
changeset
|
199 " large\n" |
|
6481c5aed3e1
posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
65
diff
changeset
|
200 " }\n" |
| 15 | 201 " id\n" |
| 202 " title {\n" | |
| 203 " romaji\n" | |
| 204 " english\n" | |
| 205 " native\n" | |
| 206 " }\n" | |
| 207 " format\n" | |
| 208 " status\n" | |
| 209 " averageScore\n" | |
| 210 " season\n" | |
| 211 " startDate {\n" | |
| 212 " year\n" | |
| 213 " month\n" | |
| 214 " day\n" | |
| 215 " }\n" | |
| 216 " genres\n" | |
| 217 " episodes\n" | |
| 218 " duration\n" | |
| 219 " synonyms\n" | |
| 220 " description(asHtml: false)\n" | |
| 221 " }\n" | |
| 222 " }\n" | |
| 223 " }\n" | |
| 224 " }\n" | |
| 225 "}\n"; | |
| 9 | 226 // clang-format off |
| 227 nlohmann::json json = { | |
| 228 {"query", query}, | |
| 229 {"variables", { | |
| 10 | 230 {"id", account.UserId()} |
| 9 | 231 }} |
| 232 }; | |
| 233 // clang-format on | |
| 234 /* TODO: do a try catch here, catch any json errors and then call | |
| 235 Authorize() if needed */ | |
| 236 auto res = nlohmann::json::parse(SendRequest(json.dump())); | |
| 237 /* TODO: make sure that we actually need the wstring converter and see | |
| 238 if we can just get wide strings back from nlohmann::json */ | |
| 239 for (const auto& list : res["data"]["MediaListCollection"]["lists"].items()) { | |
| 10 | 240 ParseList(list.value()); |
| 9 | 241 } |
| 242 return 1; | |
| 243 } | |
| 244 | |
|
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
245 int UpdateAnimeEntry(int id) { |
| 9 | 246 /** |
| 247 * possible values: | |
| 15 | 248 * |
| 9 | 249 * int mediaId, |
| 250 * MediaListStatus status, | |
| 251 * float score, | |
| 252 * int scoreRaw, | |
| 253 * int progress, | |
| 254 * int progressVolumes, | |
| 255 * int repeat, | |
| 256 * int priority, | |
| 257 * bool private, | |
| 258 * string notes, | |
| 259 * bool hiddenFromStatusLists, | |
| 260 * string[] customLists, | |
| 261 * float[] advancedScores, | |
| 262 * Date startedAt, | |
| 263 * Date completedAt | |
| 15 | 264 **/ |
|
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
265 Anime::Anime& anime = Anime::db.items[id]; |
| 63 | 266 const std::string query = |
| 267 "mutation ($media_id: Int, $progress: Int, $status: MediaListStatus, $score: Int, $notes: String) {\n" | |
| 268 " SaveMediaListEntry (mediaId: $media_id, progress: $progress, status: $status, scoreRaw: $score, notes: " | |
| 269 "$notes) {\n" | |
| 270 " id\n" | |
| 271 " }\n" | |
| 272 "}\n"; | |
| 9 | 273 // clang-format off |
| 274 nlohmann::json json = { | |
| 275 {"query", query}, | |
| 276 {"variables", { | |
| 10 | 277 {"media_id", anime.GetId()}, |
| 278 {"progress", anime.GetUserProgress()}, | |
| 15 | 279 {"status", ListStatusToString(anime)}, |
| 10 | 280 {"score", anime.GetUserScore()}, |
| 281 {"notes", anime.GetUserNotes()} | |
| 9 | 282 }} |
| 283 }; | |
| 284 // clang-format on | |
|
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
285 auto ret = nlohmann::json::parse(SendRequest(json.dump())); |
|
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
286 return JSON::GetInt(ret, "/data/SaveMediaListEntry/id"_json_pointer); |
| 9 | 287 } |
| 288 | |
| 289 int ParseUser(const nlohmann::json& json) { | |
| 290 account.SetUsername(JSON::GetString(json, "/name"_json_pointer)); | |
| 291 account.SetUserId(JSON::GetInt(json, "/id"_json_pointer)); | |
| 10 | 292 return account.UserId(); |
| 9 | 293 } |
| 294 | |
|
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
295 bool AuthorizeUser() { |
| 9 | 296 /* Prompt for PIN */ |
| 36 | 297 QDesktopServices::openUrl( |
| 298 QUrl("https://anilist.co/api/v2/oauth/authorize?client_id=" CLIENT_ID "&response_type=token")); | |
| 9 | 299 bool ok; |
| 300 QString token = QInputDialog::getText( | |
| 36 | 301 0, "Credentials needed!", "Please enter the code given to you after logging in to AniList:", QLineEdit::Normal, |
| 302 "", &ok); | |
| 9 | 303 if (ok && !token.isEmpty()) |
|
65
26721c28bf22
*: avoid usage of (to|from)StdString
Paper <mrpapersonic@gmail.com>
parents:
64
diff
changeset
|
304 account.SetAuthToken(Strings::ToUtf8String(token)); |
| 15 | 305 else // fail |
|
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
306 return false; |
| 9 | 307 const std::string query = "query {\n" |
| 15 | 308 " Viewer {\n" |
| 309 " id\n" | |
| 310 " name\n" | |
| 311 " mediaListOptions {\n" | |
| 312 " scoreFormat\n" | |
| 313 " }\n" | |
| 314 " }\n" | |
| 315 "}\n"; | |
| 9 | 316 nlohmann::json json = { |
| 63 | 317 {"query", query} |
| 318 }; | |
| 9 | 319 auto ret = nlohmann::json::parse(SendRequest(json.dump())); |
| 74 | 320 ParseUser(ret["data"]["Viewer"]); |
|
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
321 return true; |
| 9 | 322 } |
| 323 | |
| 63 | 324 } // namespace AniList |
| 325 } // namespace Services |
