Mercurial > minori
annotate src/services/anilist.cpp @ 70:64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
for some reason in C++11 unordered_maps don't work on enumerations.
so instead we'll just use a switch statement :P
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Tue, 03 Oct 2023 04:45:05 -0400 |
parents | 6481c5aed3e1 |
children | 5ccb99bfa605 |
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 if (anime.GetUserIsRewatching()) | |
102 return "REWATCHING"; | |
103 | |
70
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
104 switch (anime.GetUserStatus()) { |
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
105 case Anime::ListStatus::PLANNING: |
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
106 return "PLANNING"; |
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
107 case Anime::ListStatus::COMPLETED: |
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
108 return "COMPLETED"; |
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
109 case Anime::ListStatus::DROPPED: |
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
110 return "DROPPED"; |
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
111 case Anime::ListStatus::PAUSED: |
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
112 return "PAUSED"; |
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
113 default: |
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
114 break; |
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
115 } |
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
116 return "CURRENT"; |
15 | 117 } |
9 | 118 |
10 | 119 Date ParseDate(const nlohmann::json& json) { |
120 Date date; | |
64 | 121 /* JSON for Modern C++ warns here. I'm not too sure why, this code works when I set the |
122 standard to C++17 :/ */ | |
11 | 123 if (json.contains("/year"_json_pointer) && json.at("/year"_json_pointer).is_number()) |
9 | 124 date.SetYear(JSON::GetInt(json, "/year"_json_pointer)); |
125 else | |
126 date.VoidYear(); | |
127 | |
11 | 128 if (json.contains("/month"_json_pointer) && json.at("/month"_json_pointer).is_number()) |
9 | 129 date.SetMonth(JSON::GetInt(json, "/month"_json_pointer)); |
130 else | |
131 date.VoidMonth(); | |
132 | |
11 | 133 if (json.contains("/day"_json_pointer) && json.at("/day"_json_pointer).is_number()) |
9 | 134 date.SetDay(JSON::GetInt(json, "/day"_json_pointer)); |
135 else | |
136 date.VoidDay(); | |
10 | 137 return date; |
9 | 138 } |
139 | |
140 void ParseTitle(const nlohmann::json& json, Anime::Anime& anime) { | |
141 anime.SetNativeTitle(JSON::GetString(json, "/native"_json_pointer)); | |
142 anime.SetEnglishTitle(JSON::GetString(json, "/english"_json_pointer)); | |
143 anime.SetRomajiTitle(JSON::GetString(json, "/romaji"_json_pointer)); | |
144 } | |
145 | |
146 int ParseMediaJson(const nlohmann::json& json) { | |
147 int id = JSON::GetInt(json, "/id"_json_pointer); | |
148 if (!id) | |
149 return 0; | |
150 Anime::Anime& anime = Anime::db.items[id]; | |
151 anime.SetId(id); | |
152 | |
11 | 153 ParseTitle(json.at("/title"_json_pointer), anime); |
9 | 154 |
155 anime.SetEpisodes(JSON::GetInt(json, "/episodes"_json_pointer)); | |
15 | 156 anime.SetFormat(Translate::AniList::ToSeriesFormat(JSON::GetString(json, "/format"_json_pointer))); |
9 | 157 |
15 | 158 anime.SetAiringStatus(Translate::AniList::ToSeriesStatus(JSON::GetString(json, "/status"_json_pointer))); |
9 | 159 |
10 | 160 anime.SetAirDate(ParseDate(json["/startDate"_json_pointer])); |
9 | 161 |
66
6481c5aed3e1
posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
65
diff
changeset
|
162 anime.SetPosterUrl(JSON::GetString(json, "/coverImage/large"_json_pointer)); |
6481c5aed3e1
posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
65
diff
changeset
|
163 |
9 | 164 anime.SetAudienceScore(JSON::GetInt(json, "/averageScore"_json_pointer)); |
15 | 165 anime.SetSeason(Translate::AniList::ToSeriesSeason(JSON::GetString(json, "/season"_json_pointer))); |
9 | 166 anime.SetDuration(JSON::GetInt(json, "/duration"_json_pointer)); |
10 | 167 anime.SetSynopsis(Strings::TextifySynopsis(JSON::GetString(json, "/description"_json_pointer))); |
9 | 168 |
169 if (json.contains("/genres"_json_pointer) && json["/genres"_json_pointer].is_array()) | |
170 anime.SetGenres(json["/genres"_json_pointer].get<std::vector<std::string>>()); | |
171 if (json.contains("/synonyms"_json_pointer) && json["/synonyms"_json_pointer].is_array()) | |
10 | 172 anime.SetTitleSynonyms(json["/synonyms"_json_pointer].get<std::vector<std::string>>()); |
173 return id; | |
9 | 174 } |
175 | |
10 | 176 int ParseListItem(const nlohmann::json& json) { |
177 int id = ParseMediaJson(json["media"]); | |
178 | |
179 Anime::Anime& anime = Anime::db.items[id]; | |
180 | |
181 anime.AddToUserList(); | |
9 | 182 |
10 | 183 anime.SetUserScore(JSON::GetInt(json, "/score"_json_pointer)); |
184 anime.SetUserProgress(JSON::GetInt(json, "/progress"_json_pointer)); | |
15 | 185 ParseListStatus(JSON::GetString(json, "/status"_json_pointer), anime); |
10 | 186 anime.SetUserNotes(JSON::GetString(json, "/notes"_json_pointer)); |
9 | 187 |
10 | 188 anime.SetUserDateStarted(ParseDate(json["/startedAt"_json_pointer])); |
189 anime.SetUserDateCompleted(ParseDate(json["/completedAt"_json_pointer])); | |
9 | 190 |
10 | 191 anime.SetUserTimeUpdated(JSON::GetInt(json, "/updatedAt"_json_pointer)); |
192 | |
193 return id; | |
9 | 194 } |
195 | |
196 int ParseList(const nlohmann::json& json) { | |
197 for (const auto& entry : json["entries"].items()) { | |
198 ParseListItem(entry.value()); | |
199 } | |
10 | 200 return 1; |
9 | 201 } |
202 | |
10 | 203 int GetAnimeList() { |
9 | 204 /* NOTE: these should be in the qrc file */ |
205 const std::string query = "query ($id: Int) {\n" | |
15 | 206 " MediaListCollection (userId: $id, type: ANIME) {\n" |
207 " lists {\n" | |
208 " name\n" | |
209 " entries {\n" | |
210 " score\n" | |
211 " notes\n" | |
212 " status\n" | |
213 " progress\n" | |
214 " startedAt {\n" | |
215 " year\n" | |
216 " month\n" | |
217 " day\n" | |
218 " }\n" | |
219 " completedAt {\n" | |
220 " year\n" | |
221 " month\n" | |
222 " day\n" | |
223 " }\n" | |
224 " updatedAt\n" | |
225 " media {\n" | |
66
6481c5aed3e1
posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
65
diff
changeset
|
226 " coverImage {\n" |
6481c5aed3e1
posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
65
diff
changeset
|
227 " large\n" |
6481c5aed3e1
posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
65
diff
changeset
|
228 " }\n" |
15 | 229 " id\n" |
230 " title {\n" | |
231 " romaji\n" | |
232 " english\n" | |
233 " native\n" | |
234 " }\n" | |
235 " format\n" | |
236 " status\n" | |
237 " averageScore\n" | |
238 " season\n" | |
239 " startDate {\n" | |
240 " year\n" | |
241 " month\n" | |
242 " day\n" | |
243 " }\n" | |
244 " genres\n" | |
245 " episodes\n" | |
246 " duration\n" | |
247 " synonyms\n" | |
248 " description(asHtml: false)\n" | |
249 " }\n" | |
250 " }\n" | |
251 " }\n" | |
252 " }\n" | |
253 "}\n"; | |
9 | 254 // clang-format off |
255 nlohmann::json json = { | |
256 {"query", query}, | |
257 {"variables", { | |
10 | 258 {"id", account.UserId()} |
9 | 259 }} |
260 }; | |
261 // clang-format on | |
262 /* TODO: do a try catch here, catch any json errors and then call | |
263 Authorize() if needed */ | |
264 auto res = nlohmann::json::parse(SendRequest(json.dump())); | |
265 /* TODO: make sure that we actually need the wstring converter and see | |
266 if we can just get wide strings back from nlohmann::json */ | |
267 for (const auto& list : res["data"]["MediaListCollection"]["lists"].items()) { | |
10 | 268 ParseList(list.value()); |
9 | 269 } |
270 return 1; | |
271 } | |
272 | |
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
273 int UpdateAnimeEntry(int id) { |
9 | 274 /** |
275 * possible values: | |
15 | 276 * |
9 | 277 * int mediaId, |
278 * MediaListStatus status, | |
279 * float score, | |
280 * int scoreRaw, | |
281 * int progress, | |
282 * int progressVolumes, | |
283 * int repeat, | |
284 * int priority, | |
285 * bool private, | |
286 * string notes, | |
287 * bool hiddenFromStatusLists, | |
288 * string[] customLists, | |
289 * float[] advancedScores, | |
290 * Date startedAt, | |
291 * Date completedAt | |
15 | 292 **/ |
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
293 Anime::Anime& anime = Anime::db.items[id]; |
63 | 294 const std::string query = |
295 "mutation ($media_id: Int, $progress: Int, $status: MediaListStatus, $score: Int, $notes: String) {\n" | |
296 " SaveMediaListEntry (mediaId: $media_id, progress: $progress, status: $status, scoreRaw: $score, notes: " | |
297 "$notes) {\n" | |
298 " id\n" | |
299 " }\n" | |
300 "}\n"; | |
9 | 301 // clang-format off |
302 nlohmann::json json = { | |
303 {"query", query}, | |
304 {"variables", { | |
10 | 305 {"media_id", anime.GetId()}, |
306 {"progress", anime.GetUserProgress()}, | |
15 | 307 {"status", ListStatusToString(anime)}, |
10 | 308 {"score", anime.GetUserScore()}, |
309 {"notes", anime.GetUserNotes()} | |
9 | 310 }} |
311 }; | |
312 // clang-format on | |
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
313 auto ret = nlohmann::json::parse(SendRequest(json.dump())); |
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
314 return JSON::GetInt(ret, "/data/SaveMediaListEntry/id"_json_pointer); |
9 | 315 } |
316 | |
317 int ParseUser(const nlohmann::json& json) { | |
318 account.SetUsername(JSON::GetString(json, "/name"_json_pointer)); | |
319 account.SetUserId(JSON::GetInt(json, "/id"_json_pointer)); | |
10 | 320 return account.UserId(); |
9 | 321 } |
322 | |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
323 bool AuthorizeUser() { |
9 | 324 /* Prompt for PIN */ |
36 | 325 QDesktopServices::openUrl( |
326 QUrl("https://anilist.co/api/v2/oauth/authorize?client_id=" CLIENT_ID "&response_type=token")); | |
9 | 327 bool ok; |
328 QString token = QInputDialog::getText( | |
36 | 329 0, "Credentials needed!", "Please enter the code given to you after logging in to AniList:", QLineEdit::Normal, |
330 "", &ok); | |
9 | 331 if (ok && !token.isEmpty()) |
65
26721c28bf22
*: avoid usage of (to|from)StdString
Paper <mrpapersonic@gmail.com>
parents:
64
diff
changeset
|
332 account.SetAuthToken(Strings::ToUtf8String(token)); |
15 | 333 else // fail |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
334 return false; |
9 | 335 const std::string query = "query {\n" |
15 | 336 " Viewer {\n" |
337 " id\n" | |
338 " name\n" | |
339 " mediaListOptions {\n" | |
340 " scoreFormat\n" | |
341 " }\n" | |
342 " }\n" | |
343 "}\n"; | |
9 | 344 nlohmann::json json = { |
63 | 345 {"query", query} |
346 }; | |
9 | 347 auto ret = nlohmann::json::parse(SendRequest(json.dump())); |
10 | 348 ParseUser(json["Viewer"]); |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
349 return true; |
9 | 350 } |
351 | |
63 | 352 } // namespace AniList |
353 } // namespace Services |