comparison src/services/anilist.cc @ 202:71832ffe425a

animia: re-add kvm fd source this is all being merged from my wildly out-of-date laptop. SORRY! in other news, I edited the CI file to install the wayland client as well, so the linux CI build might finally get wayland stuff.
author Paper <paper@paper.us.eu.org>
date Tue, 02 Jan 2024 06:05:06 -0500
parents 9613d72b097e
children 7cf53145de11
comparison
equal deleted inserted replaced
201:8f6f8dd2eb23 202:71832ffe425a
5 #include "core/http.h" 5 #include "core/http.h"
6 #include "core/json.h" 6 #include "core/json.h"
7 #include "core/session.h" 7 #include "core/session.h"
8 #include "core/strings.h" 8 #include "core/strings.h"
9 #include "gui/translate/anilist.h" 9 #include "gui/translate/anilist.h"
10
11 #include <QDate>
10 #include <QByteArray> 12 #include <QByteArray>
11 #include <QDesktopServices> 13 #include <QDesktopServices>
12 #include <QInputDialog> 14 #include <QInputDialog>
13 #include <QLineEdit> 15 #include <QLineEdit>
14 #include <QMessageBox> 16 #include <QMessageBox>
15 #include <QUrl> 17 #include <QUrl>
18
16 #include <chrono> 19 #include <chrono>
17 #include <exception> 20 #include <exception>
18 #define CLIENT_ID "13706" 21
22 #include <iostream>
19 23
20 using namespace nlohmann::literals::json_literals; 24 using namespace nlohmann::literals::json_literals;
21 25
22 namespace Services { 26 namespace Services {
23 namespace AniList { 27 namespace AniList {
28
29 constexpr int CLIENT_ID = 13706;
24 30
25 class Account { 31 class Account {
26 public: 32 public:
27 std::string Username() const { return session.config.auth.anilist.username; } 33 std::string Username() const { return session.config.auth.anilist.username; }
28 void SetUsername(std::string const& username) { session.config.auth.anilist.username = username; } 34 void SetUsername(std::string const& username) { session.config.auth.anilist.username = username; }
32 38
33 std::string AuthToken() const { return session.config.auth.anilist.auth_token; } 39 std::string AuthToken() const { return session.config.auth.anilist.auth_token; }
34 void SetAuthToken(std::string const& auth_token) { session.config.auth.anilist.auth_token = auth_token; } 40 void SetAuthToken(std::string const& auth_token) { session.config.auth.anilist.auth_token = auth_token; }
35 41
36 bool Authenticated() const { return !AuthToken().empty(); } 42 bool Authenticated() const { return !AuthToken().empty(); }
43 bool IsValid() const { return UserId() && Authenticated(); }
37 }; 44 };
38 45
39 static Account account; 46 static Account account;
40 47
41 std::string SendRequest(std::string data) { 48 std::string SendRequest(std::string data) {
42 std::vector<std::string> headers = {"Authorization: Bearer " + account.AuthToken(), "Accept: application/json", 49 std::vector<std::string> headers = {"Authorization: Bearer " + account.AuthToken(), "Accept: application/json",
43 "Content-Type: application/json"}; 50 "Content-Type: application/json"};
44 return Strings::ToUtf8String(HTTP::Post("https://graphql.anilist.co", data, headers)); 51 return Strings::ToUtf8String(HTTP::Post("https://graphql.anilist.co", data, headers));
45 } 52 }
46 53
54 nlohmann::json SendJSONRequest(nlohmann::json data) {
55 std::string request = SendRequest(data.dump());
56 if (request.empty()) {
57 std::cerr << "[AniList] JSON Request returned an empty result!" << std::endl;
58 return {};
59 }
60
61 auto ret = nlohmann::json::parse(request, nullptr, false);
62 if (ret.is_discarded()) {
63 std::cerr << "[AniList] Failed to parse request JSON!" << std::endl;
64 return {};
65 }
66
67 if (ret.contains("/errors"_json_pointer) && ret.at("/errors"_json_pointer).is_array()) {
68 for (const auto& error : ret.at("/errors"_json_pointer))
69 std::cerr << "[AniList] Received an error in response: " << JSON::GetString<std::string>(error, "/message"_json_pointer, "") << std::endl;
70
71 return {};
72 }
73
74 return ret;
75 }
76
47 void ParseListStatus(std::string status, Anime::Anime& anime) { 77 void ParseListStatus(std::string status, Anime::Anime& anime) {
48 std::unordered_map<std::string, Anime::ListStatus> map = { 78 static const std::unordered_map<std::string, Anime::ListStatus> map = {
49 {"CURRENT", Anime::ListStatus::CURRENT }, 79 {"CURRENT", Anime::ListStatus::CURRENT },
50 {"PLANNING", Anime::ListStatus::PLANNING }, 80 {"PLANNING", Anime::ListStatus::PLANNING },
51 {"COMPLETED", Anime::ListStatus::COMPLETED}, 81 {"COMPLETED", Anime::ListStatus::COMPLETED},
52 {"DROPPED", Anime::ListStatus::DROPPED }, 82 {"DROPPED", Anime::ListStatus::DROPPED },
53 {"PAUSED", Anime::ListStatus::PAUSED } 83 {"PAUSED", Anime::ListStatus::PAUSED }
54 }; 84 };
55 85
56 if (status == "REPEATING") { 86 if (status == "REPEATING") {
57 anime.SetUserIsRewatching(true); 87 anime.SetUserIsRewatching(true);
58 anime.SetUserStatus(Anime::ListStatus::CURRENT); 88 anime.SetUserStatus(Anime::ListStatus::CURRENT);
59 return; 89 return;
62 if (map.find(status) == map.end()) { 92 if (map.find(status) == map.end()) {
63 anime.SetUserStatus(Anime::ListStatus::NOT_IN_LIST); 93 anime.SetUserStatus(Anime::ListStatus::NOT_IN_LIST);
64 return; 94 return;
65 } 95 }
66 96
67 anime.SetUserStatus(map[status]); 97 anime.SetUserStatus(map.at(status));
68 } 98 }
69 99
70 std::string ListStatusToString(const Anime::Anime& anime) { 100 std::string ListStatusToString(const Anime::Anime& anime) {
71 if (anime.GetUserIsRewatching()) 101 if (anime.GetUserIsRewatching())
72 return "REWATCHING"; 102 return "REWATCHING";
79 default: break; 109 default: break;
80 } 110 }
81 return "CURRENT"; 111 return "CURRENT";
82 } 112 }
83 113
84 Date ParseDate(const nlohmann::json& json) {
85 Date date;
86 /* JSON for Modern C++ warns here. I'm not too sure why, this code works when I set the
87 standard to C++17 :/ */
88 if (json.contains("/year"_json_pointer) && json.at("/year"_json_pointer).is_number())
89 date.SetYear(JSON::GetInt(json, "/year"_json_pointer));
90 else
91 date.VoidYear();
92
93 if (json.contains("/month"_json_pointer) && json.at("/month"_json_pointer).is_number())
94 date.SetMonth(JSON::GetInt(json, "/month"_json_pointer));
95 else
96 date.VoidMonth();
97
98 if (json.contains("/day"_json_pointer) && json.at("/day"_json_pointer).is_number())
99 date.SetDay(JSON::GetInt(json, "/day"_json_pointer));
100 else
101 date.VoidDay();
102 return date;
103 }
104
105 void ParseTitle(const nlohmann::json& json, Anime::Anime& anime) { 114 void ParseTitle(const nlohmann::json& json, Anime::Anime& anime) {
106 anime.SetNativeTitle(JSON::GetString(json, "/native"_json_pointer)); 115 anime.SetNativeTitle(JSON::GetString<std::string>(json, "/native"_json_pointer, ""));
107 anime.SetEnglishTitle(JSON::GetString(json, "/english"_json_pointer)); 116 anime.SetEnglishTitle(JSON::GetString<std::string>(json, "/english"_json_pointer, ""));
108 anime.SetRomajiTitle(JSON::GetString(json, "/romaji"_json_pointer)); 117 anime.SetRomajiTitle(JSON::GetString<std::string>(json, "/romaji"_json_pointer, ""));
109 } 118 }
110 119
111 int ParseMediaJson(const nlohmann::json& json) { 120 int ParseMediaJson(const nlohmann::json& json) {
112 int id = JSON::GetInt(json, "/id"_json_pointer); 121 int id = JSON::GetNumber(json, "/id"_json_pointer);
113 if (!id) 122 if (!id)
114 return 0; 123 return 0;
124
115 Anime::Anime& anime = Anime::db.items[id]; 125 Anime::Anime& anime = Anime::db.items[id];
116 anime.SetId(id); 126 anime.SetId(id);
117 127
118 ParseTitle(json.at("/title"_json_pointer), anime); 128 ParseTitle(json.at("/title"_json_pointer), anime);
119 129
120 anime.SetEpisodes(JSON::GetInt(json, "/episodes"_json_pointer)); 130 anime.SetEpisodes(JSON::GetNumber(json, "/episodes"_json_pointer, 0));
121 anime.SetFormat(Translate::AniList::ToSeriesFormat(JSON::GetString(json, "/format"_json_pointer))); 131 anime.SetFormat(Translate::AniList::ToSeriesFormat(JSON::GetString<std::string>(json, "/format"_json_pointer, "")));
122 132
123 anime.SetAiringStatus(Translate::AniList::ToSeriesStatus(JSON::GetString(json, "/status"_json_pointer))); 133 anime.SetAiringStatus(Translate::AniList::ToSeriesStatus(JSON::GetString<std::string>(json, "/status"_json_pointer, "")));
124 134
125 anime.SetAirDate(ParseDate(json["/startDate"_json_pointer])); 135 anime.SetAirDate(Date(json["/startDate"_json_pointer]));
126 136
127 anime.SetPosterUrl(JSON::GetString(json, "/coverImage/large"_json_pointer)); 137 anime.SetPosterUrl(JSON::GetString<std::string>(json, "/coverImage/large"_json_pointer, ""));
128 138
129 anime.SetAudienceScore(JSON::GetInt(json, "/averageScore"_json_pointer)); 139 anime.SetAudienceScore(JSON::GetNumber(json, "/averageScore"_json_pointer, 0));
130 anime.SetSeason(Translate::AniList::ToSeriesSeason(JSON::GetString(json, "/season"_json_pointer))); 140 anime.SetSeason(Translate::AniList::ToSeriesSeason(JSON::GetString<std::string>(json, "/season"_json_pointer, "")));
131 anime.SetDuration(JSON::GetInt(json, "/duration"_json_pointer)); 141 anime.SetDuration(JSON::GetNumber(json, "/duration"_json_pointer, 0));
132 anime.SetSynopsis(Strings::TextifySynopsis(JSON::GetString(json, "/description"_json_pointer))); 142 anime.SetSynopsis(Strings::TextifySynopsis(JSON::GetString<std::string>(json, "/description"_json_pointer, "")));
133 143
134 if (json.contains("/genres"_json_pointer) && json["/genres"_json_pointer].is_array()) 144 anime.SetGenres(JSON::GetArray<std::vector<std::string>>(json, "/genres"_json_pointer, {}));
135 anime.SetGenres(json["/genres"_json_pointer].get<std::vector<std::string>>()); 145 anime.SetTitleSynonyms(JSON::GetArray<std::vector<std::string>>(json, "/synonyms"_json_pointer, {}));
136 if (json.contains("/synonyms"_json_pointer) && json["/synonyms"_json_pointer].is_array()) 146
137 anime.SetTitleSynonyms(json["/synonyms"_json_pointer].get<std::vector<std::string>>());
138 return id; 147 return id;
139 } 148 }
140 149
141 int ParseListItem(const nlohmann::json& json) { 150 int ParseListItem(const nlohmann::json& json) {
142 int id = ParseMediaJson(json["media"]); 151 int id = ParseMediaJson(json["media"]);
143 152
144 Anime::Anime& anime = Anime::db.items[id]; 153 Anime::Anime& anime = Anime::db.items[id];
145 154
146 anime.AddToUserList(); 155 anime.AddToUserList();
147 156
148 anime.SetUserScore(JSON::GetInt(json, "/score"_json_pointer)); 157 anime.SetUserScore(JSON::GetNumber(json, "/score"_json_pointer, 0));
149 anime.SetUserProgress(JSON::GetInt(json, "/progress"_json_pointer)); 158 anime.SetUserProgress(JSON::GetNumber(json, "/progress"_json_pointer, 0));
150 ParseListStatus(JSON::GetString(json, "/status"_json_pointer), anime); 159 ParseListStatus(JSON::GetString<std::string>(json, "/status"_json_pointer, ""), anime);
151 anime.SetUserNotes(JSON::GetString(json, "/notes"_json_pointer)); 160 anime.SetUserNotes(JSON::GetString<std::string>(json, "/notes"_json_pointer, ""));
152 161
153 anime.SetUserDateStarted(ParseDate(json["/startedAt"_json_pointer])); 162 anime.SetUserDateStarted(Date(json["/startedAt"_json_pointer]));
154 anime.SetUserDateCompleted(ParseDate(json["/completedAt"_json_pointer])); 163 anime.SetUserDateCompleted(Date(json["/completedAt"_json_pointer]));
155 164
156 anime.SetUserTimeUpdated(JSON::GetInt(json, "/updatedAt"_json_pointer)); 165 anime.SetUserTimeUpdated(JSON::GetNumber(json, "/updatedAt"_json_pointer, 0));
157 166
158 return id; 167 return id;
159 } 168 }
160 169
161 int ParseList(const nlohmann::json& json) { 170 int ParseList(const nlohmann::json& json) {
164 } 173 }
165 return 1; 174 return 1;
166 } 175 }
167 176
168 int GetAnimeList() { 177 int GetAnimeList() {
169 /* NOTE: these should be in the qrc file */ 178 if (!account.IsValid()) {
170 const std::string query = "query ($id: Int) {\n" 179 std::cerr << "AniList: Account isn't valid!" << std::endl;
171 " MediaListCollection (userId: $id, type: ANIME) {\n" 180 return 0;
172 " lists {\n" 181 }
173 " name\n" 182
174 " entries {\n" 183 /* NOTE: these really ought to be in the qrc file */
175 " score\n" 184 constexpr std::string_view query = "query ($id: Int) {\n"
176 " notes\n" 185 " MediaListCollection (userId: $id, type: ANIME) {\n"
177 " status\n" 186 " lists {\n"
178 " progress\n" 187 " name\n"
179 " startedAt {\n" 188 " entries {\n"
180 " year\n" 189 " score\n"
181 " month\n" 190 " notes\n"
182 " day\n" 191 " status\n"
183 " }\n" 192 " progress\n"
184 " completedAt {\n" 193 " startedAt {\n"
185 " year\n" 194 " year\n"
186 " month\n" 195 " month\n"
187 " day\n" 196 " day\n"
188 " }\n" 197 " }\n"
189 " updatedAt\n" 198 " completedAt {\n"
190 " media {\n" 199 " year\n"
191 " coverImage {\n" 200 " month\n"
192 " large\n" 201 " day\n"
193 " }\n" 202 " }\n"
194 " id\n" 203 " updatedAt\n"
195 " title {\n" 204 " media {\n"
196 " romaji\n" 205 " coverImage {\n"
197 " english\n" 206 " large\n"
198 " native\n" 207 " }\n"
199 " }\n" 208 " id\n"
200 " format\n" 209 " title {\n"
201 " status\n" 210 " romaji\n"
202 " averageScore\n" 211 " english\n"
203 " season\n" 212 " native\n"
204 " startDate {\n" 213 " }\n"
205 " year\n" 214 " format\n"
206 " month\n" 215 " status\n"
207 " day\n" 216 " averageScore\n"
208 " }\n" 217 " season\n"
209 " genres\n" 218 " startDate {\n"
210 " episodes\n" 219 " year\n"
211 " duration\n" 220 " month\n"
212 " synonyms\n" 221 " day\n"
213 " description(asHtml: false)\n" 222 " }\n"
214 " }\n" 223 " genres\n"
215 " }\n" 224 " episodes\n"
216 " }\n" 225 " duration\n"
217 " }\n" 226 " synonyms\n"
218 "}\n"; 227 " description(asHtml: false)\n"
228 " }\n"
229 " }\n"
230 " }\n"
231 " }\n"
232 "}\n";
219 // clang-format off 233 // clang-format off
220 nlohmann::json json = { 234 nlohmann::json json = {
221 {"query", query}, 235 {"query", query},
222 {"variables", { 236 {"variables", {
223 {"id", account.UserId()} 237 {"id", account.UserId()}
224 }} 238 }}
225 }; 239 };
226 // clang-format on 240 // clang-format on
227 /* TODO: do a try catch here, catch any json errors and then call 241
228 Authorize() if needed */ 242 auto res = SendJSONRequest(json);
229 auto res = nlohmann::json::parse(SendRequest(json.dump())); 243
230 /* TODO: make sure that we actually need the wstring converter and see
231 if we can just get wide strings back from nlohmann::json */
232 for (const auto& list : res["data"]["MediaListCollection"]["lists"].items()) { 244 for (const auto& list : res["data"]["MediaListCollection"]["lists"].items()) {
233 ParseList(list.value()); 245 ParseList(list.value());
234 } 246 }
235 return 1; 247 return 1;
236 } 248 }
242 * int mediaId, 254 * int mediaId,
243 * MediaListStatus status, 255 * MediaListStatus status,
244 * float score, 256 * float score,
245 * int scoreRaw, 257 * int scoreRaw,
246 * int progress, 258 * int progress,
247 * int progressVolumes, 259 * int progressVolumes, // manga-specific.
248 * int repeat, 260 * int repeat, // rewatch
249 * int priority, 261 * int priority,
250 * bool private, 262 * bool private,
251 * string notes, 263 * string notes,
252 * bool hiddenFromStatusLists, 264 * bool hiddenFromStatusLists,
253 * string[] customLists, 265 * string[] customLists,
254 * float[] advancedScores, 266 * float[] advancedScores,
255 * Date startedAt, 267 * Date startedAt,
256 * Date completedAt 268 * Date completedAt
257 **/ 269 **/
258 Anime::Anime& anime = Anime::db.items[id]; 270 Anime::Anime& anime = Anime::db.items[id];
259 const std::string query = "mutation ($media_id: Int, $progress: Int, $status: MediaListStatus, $score: Int, " 271 if (!anime.IsInUserList())
260 "$notes: String, $start: FuzzyDateInput, $comp: FuzzyDateInput) {\n" 272 return 0;
261 " SaveMediaListEntry (mediaId: $media_id, progress: $progress, status: $status, " 273
262 "scoreRaw: $score, notes: $notes, startedAt: $start, completedAt: $comp) {\n" 274 constexpr std::string_view query = "mutation ($media_id: Int, $progress: Int, $status: MediaListStatus, $score: Int, "
263 " id\n" 275 "$notes: String, $start: FuzzyDateInput, $comp: FuzzyDateInput, $repeat: Int) {\n"
264 " }\n" 276 " SaveMediaListEntry (mediaId: $media_id, progress: $progress, status: $status, "
265 "}\n"; 277 "scoreRaw: $score, notes: $notes, startedAt: $start, completedAt: $comp, repeat: $repeat) {\n"
278 " id\n"
279 " }\n"
280 "}\n";
266 // clang-format off 281 // clang-format off
267 nlohmann::json json = { 282 nlohmann::json json = {
268 {"query", query}, 283 {"query", query},
269 {"variables", { 284 {"variables", {
270 {"media_id", anime.GetId()}, 285 {"media_id", anime.GetId()},
271 {"progress", anime.GetUserProgress()}, 286 {"progress", anime.GetUserProgress()},
272 {"status", ListStatusToString(anime)}, 287 {"status", ListStatusToString(anime)},
273 {"score", anime.GetUserScore()}, 288 {"score", anime.GetUserScore()},
274 {"notes", anime.GetUserNotes()}, 289 {"notes", anime.GetUserNotes()},
275 {"start", anime.GetUserDateStarted().GetAsAniListJson()}, 290 {"start", anime.GetUserDateStarted().GetAsAniListJson()},
276 {"comp", anime.GetUserDateCompleted().GetAsAniListJson()} 291 {"comp", anime.GetUserDateCompleted().GetAsAniListJson()},
292 {"repeat", anime.GetUserRewatchedTimes()}
277 }} 293 }}
278 }; 294 };
279 // clang-format on 295 // clang-format on
280 auto ret = nlohmann::json::parse(SendRequest(json.dump())); 296
281 return JSON::GetInt(ret, "/data/SaveMediaListEntry/id"_json_pointer); 297 auto ret = SendJSONRequest(json);
298
299 return JSON::GetNumber(ret, "/data/SaveMediaListEntry/id"_json_pointer, 0);
282 } 300 }
283 301
284 int ParseUser(const nlohmann::json& json) { 302 int ParseUser(const nlohmann::json& json) {
285 account.SetUsername(JSON::GetString(json, "/name"_json_pointer)); 303 account.SetUsername(JSON::GetString<std::string>(json, "/name"_json_pointer, ""));
286 account.SetUserId(JSON::GetInt(json, "/id"_json_pointer)); 304 account.SetUserId(JSON::GetNumber(json, "/id"_json_pointer, 0));
287 return account.UserId(); 305 return account.UserId();
288 } 306 }
289 307
290 bool AuthorizeUser() { 308 bool AuthorizeUser() {
291 /* Prompt for PIN */ 309 /* Prompt for PIN */
292 QDesktopServices::openUrl( 310 QDesktopServices::openUrl(
293 QUrl("https://anilist.co/api/v2/oauth/authorize?client_id=" CLIENT_ID "&response_type=token")); 311 QUrl(Strings::ToQString("https://anilist.co/api/v2/oauth/authorize?client_id=" + std::to_string(CLIENT_ID) + "&response_type=token")));
312
294 bool ok; 313 bool ok;
295 QString token = QInputDialog::getText( 314 QString token = QInputDialog::getText(
296 0, "Credentials needed!", "Please enter the code given to you after logging in to AniList:", QLineEdit::Normal, 315 0, "Credentials needed!", "Please enter the code given to you after logging in to AniList:", QLineEdit::Normal,
297 "", &ok); 316 "", &ok);
298 if (ok && !token.isEmpty()) 317
299 account.SetAuthToken(Strings::ToUtf8String(token)); 318 if (!ok || token.isEmpty())
300 else // fail
301 return false; 319 return false;
302 const std::string query = "query {\n" 320
303 " Viewer {\n" 321 account.SetAuthToken(Strings::ToUtf8String(token));
304 " id\n" 322
305 " name\n" 323 constexpr std::string_view query = "query {\n"
306 " mediaListOptions {\n" 324 " Viewer {\n"
307 " scoreFormat\n" 325 " id\n"
308 " }\n" 326 " name\n"
309 " }\n" 327 " mediaListOptions {\n"
310 "}\n"; 328 " scoreFormat\n" // this will be used... eventually
329 " }\n"
330 " }\n"
331 "}\n";
311 nlohmann::json json = { 332 nlohmann::json json = {
312 {"query", query} 333 {"query", query}
313 }; 334 };
314 auto ret = nlohmann::json::parse(SendRequest(json.dump())); 335
336 auto ret = SendJSONRequest(json);
337
315 ParseUser(ret["data"]["Viewer"]); 338 ParseUser(ret["data"]["Viewer"]);
316 return true; 339 return true;
317 } 340 }
318 341
319 } // namespace AniList 342 } // namespace AniList