Mercurial > minori
comparison src/services/anilist.cc @ 175:9b10175be389
dep/json: update to v3.11.3
anime/db: save anime list to database, very much untested and likely won't work as intended
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Thu, 30 Nov 2023 13:52:26 -0500 |
parents | 275da698697d |
children | 01d259b9c89f |
comparison
equal
deleted
inserted
replaced
174:f88eda79c60a | 175:9b10175be389 |
---|---|
13 #include <QLineEdit> | 13 #include <QLineEdit> |
14 #include <QMessageBox> | 14 #include <QMessageBox> |
15 #include <QUrl> | 15 #include <QUrl> |
16 #include <chrono> | 16 #include <chrono> |
17 #include <exception> | 17 #include <exception> |
18 | |
19 #include <iostream> | |
20 | |
18 #define CLIENT_ID "13706" | 21 #define CLIENT_ID "13706" |
19 | 22 |
20 using namespace nlohmann::literals::json_literals; | 23 using namespace nlohmann::literals::json_literals; |
21 | 24 |
22 namespace Services { | 25 namespace Services { |
32 | 35 |
33 std::string AuthToken() const { return session.config.auth.anilist.auth_token; } | 36 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; } | 37 void SetAuthToken(std::string const& auth_token) { session.config.auth.anilist.auth_token = auth_token; } |
35 | 38 |
36 bool Authenticated() const { return !AuthToken().empty(); } | 39 bool Authenticated() const { return !AuthToken().empty(); } |
40 bool IsValid() const { return UserId() && Authenticated(); } | |
37 }; | 41 }; |
38 | 42 |
39 static Account account; | 43 static Account account; |
40 | 44 |
41 std::string SendRequest(std::string data) { | 45 std::string SendRequest(std::string data) { |
42 std::vector<std::string> headers = {"Authorization: Bearer " + account.AuthToken(), "Accept: application/json", | 46 std::vector<std::string> headers = {"Authorization: Bearer " + account.AuthToken(), "Accept: application/json", |
43 "Content-Type: application/json"}; | 47 "Content-Type: application/json"}; |
44 return Strings::ToUtf8String(HTTP::Post("https://graphql.anilist.co", data, headers)); | 48 return Strings::ToUtf8String(HTTP::Post("https://graphql.anilist.co", data, headers)); |
49 } | |
50 | |
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; | |
45 } | 72 } |
46 | 73 |
47 void ParseListStatus(std::string status, Anime::Anime& anime) { | 74 void ParseListStatus(std::string status, Anime::Anime& anime) { |
48 std::unordered_map<std::string, Anime::ListStatus> map = { | 75 std::unordered_map<std::string, Anime::ListStatus> map = { |
49 {"CURRENT", Anime::ListStatus::CURRENT }, | 76 {"CURRENT", Anime::ListStatus::CURRENT }, |
50 {"PLANNING", Anime::ListStatus::PLANNING }, | 77 {"PLANNING", Anime::ListStatus::PLANNING }, |
51 {"COMPLETED", Anime::ListStatus::COMPLETED}, | 78 {"COMPLETED", Anime::ListStatus::COMPLETED}, |
52 {"DROPPED", Anime::ListStatus::DROPPED }, | 79 {"DROPPED", Anime::ListStatus::DROPPED }, |
53 {"PAUSED", Anime::ListStatus::PAUSED } | 80 {"PAUSED", Anime::ListStatus::PAUSED } |
54 }; | 81 }; |
55 | 82 |
56 if (status == "REPEATING") { | 83 if (status == "REPEATING") { |
57 anime.SetUserIsRewatching(true); | 84 anime.SetUserIsRewatching(true); |
58 anime.SetUserStatus(Anime::ListStatus::CURRENT); | 85 anime.SetUserStatus(Anime::ListStatus::CURRENT); |
59 return; | 86 return; |
79 default: break; | 106 default: break; |
80 } | 107 } |
81 return "CURRENT"; | 108 return "CURRENT"; |
82 } | 109 } |
83 | 110 |
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) { | 111 void ParseTitle(const nlohmann::json& json, Anime::Anime& anime) { |
106 anime.SetNativeTitle(JSON::GetString(json, "/native"_json_pointer)); | 112 anime.SetNativeTitle(JSON::GetString<std::string>(json, "/native"_json_pointer, "")); |
107 anime.SetEnglishTitle(JSON::GetString(json, "/english"_json_pointer)); | 113 anime.SetEnglishTitle(JSON::GetString<std::string>(json, "/english"_json_pointer, "")); |
108 anime.SetRomajiTitle(JSON::GetString(json, "/romaji"_json_pointer)); | 114 anime.SetRomajiTitle(JSON::GetString<std::string>(json, "/romaji"_json_pointer, "")); |
109 } | 115 } |
110 | 116 |
111 int ParseMediaJson(const nlohmann::json& json) { | 117 int ParseMediaJson(const nlohmann::json& json) { |
112 int id = JSON::GetInt(json, "/id"_json_pointer); | 118 int id = JSON::GetNumber(json, "/id"_json_pointer); |
113 if (!id) | 119 if (!id) |
114 return 0; | 120 return 0; |
121 | |
115 Anime::Anime& anime = Anime::db.items[id]; | 122 Anime::Anime& anime = Anime::db.items[id]; |
116 anime.SetId(id); | 123 anime.SetId(id); |
117 | 124 |
118 ParseTitle(json.at("/title"_json_pointer), anime); | 125 ParseTitle(json.at("/title"_json_pointer), anime); |
119 | 126 |
120 anime.SetEpisodes(JSON::GetInt(json, "/episodes"_json_pointer)); | 127 anime.SetEpisodes(JSON::GetNumber(json, "/episodes"_json_pointer, 0)); |
121 anime.SetFormat(Translate::AniList::ToSeriesFormat(JSON::GetString(json, "/format"_json_pointer))); | 128 anime.SetFormat(Translate::AniList::ToSeriesFormat(JSON::GetString<std::string>(json, "/format"_json_pointer, ""))); |
122 | 129 |
123 anime.SetAiringStatus(Translate::AniList::ToSeriesStatus(JSON::GetString(json, "/status"_json_pointer))); | 130 anime.SetAiringStatus(Translate::AniList::ToSeriesStatus(JSON::GetString<std::string>(json, "/status"_json_pointer, ""))); |
124 | 131 |
125 anime.SetAirDate(ParseDate(json["/startDate"_json_pointer])); | 132 anime.SetAirDate(Date(json["/startDate"_json_pointer])); |
126 | 133 |
127 anime.SetPosterUrl(JSON::GetString(json, "/coverImage/large"_json_pointer)); | 134 anime.SetPosterUrl(JSON::GetString<std::string>(json, "/coverImage/large"_json_pointer, "")); |
128 | 135 |
129 anime.SetAudienceScore(JSON::GetInt(json, "/averageScore"_json_pointer)); | 136 anime.SetAudienceScore(JSON::GetNumber(json, "/averageScore"_json_pointer, 0)); |
130 anime.SetSeason(Translate::AniList::ToSeriesSeason(JSON::GetString(json, "/season"_json_pointer))); | 137 anime.SetSeason(Translate::AniList::ToSeriesSeason(JSON::GetString<std::string>(json, "/season"_json_pointer, ""))); |
131 anime.SetDuration(JSON::GetInt(json, "/duration"_json_pointer)); | 138 anime.SetDuration(JSON::GetNumber(json, "/duration"_json_pointer, 0)); |
132 anime.SetSynopsis(Strings::TextifySynopsis(JSON::GetString(json, "/description"_json_pointer))); | 139 anime.SetSynopsis(Strings::TextifySynopsis(JSON::GetString<std::string>(json, "/description"_json_pointer, ""))); |
133 | 140 |
134 if (json.contains("/genres"_json_pointer) && json["/genres"_json_pointer].is_array()) | 141 anime.SetGenres(JSON::GetArray<std::vector<std::string>>(json, "/genres"_json_pointer, {})); |
135 anime.SetGenres(json["/genres"_json_pointer].get<std::vector<std::string>>()); | 142 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()) | 143 |
137 anime.SetTitleSynonyms(json["/synonyms"_json_pointer].get<std::vector<std::string>>()); | |
138 return id; | 144 return id; |
139 } | 145 } |
140 | 146 |
141 int ParseListItem(const nlohmann::json& json) { | 147 int ParseListItem(const nlohmann::json& json) { |
142 int id = ParseMediaJson(json["media"]); | 148 int id = ParseMediaJson(json["media"]); |
143 | 149 |
144 Anime::Anime& anime = Anime::db.items[id]; | 150 Anime::Anime& anime = Anime::db.items[id]; |
145 | 151 |
146 anime.AddToUserList(); | 152 anime.AddToUserList(); |
147 | 153 |
148 anime.SetUserScore(JSON::GetInt(json, "/score"_json_pointer)); | 154 anime.SetUserScore(JSON::GetNumber(json, "/score"_json_pointer, 0)); |
149 anime.SetUserProgress(JSON::GetInt(json, "/progress"_json_pointer)); | 155 anime.SetUserProgress(JSON::GetNumber(json, "/progress"_json_pointer, 0)); |
150 ParseListStatus(JSON::GetString(json, "/status"_json_pointer), anime); | 156 ParseListStatus(JSON::GetString<std::string>(json, "/status"_json_pointer, ""), anime); |
151 anime.SetUserNotes(JSON::GetString(json, "/notes"_json_pointer)); | 157 anime.SetUserNotes(JSON::GetString<std::string>(json, "/notes"_json_pointer, "")); |
152 | 158 |
153 anime.SetUserDateStarted(ParseDate(json["/startedAt"_json_pointer])); | 159 anime.SetUserDateStarted(Date(json["/startedAt"_json_pointer])); |
154 anime.SetUserDateCompleted(ParseDate(json["/completedAt"_json_pointer])); | 160 anime.SetUserDateCompleted(Date(json["/completedAt"_json_pointer])); |
155 | 161 |
156 anime.SetUserTimeUpdated(JSON::GetInt(json, "/updatedAt"_json_pointer)); | 162 anime.SetUserTimeUpdated(JSON::GetNumber(json, "/updatedAt"_json_pointer, 0)); |
157 | 163 |
158 return id; | 164 return id; |
159 } | 165 } |
160 | 166 |
161 int ParseList(const nlohmann::json& json) { | 167 int ParseList(const nlohmann::json& json) { |
164 } | 170 } |
165 return 1; | 171 return 1; |
166 } | 172 } |
167 | 173 |
168 int GetAnimeList() { | 174 int GetAnimeList() { |
175 if (!account.IsValid()) { | |
176 std::cerr << "AniList: Account isn't valid!" << std::endl; | |
177 return 0; | |
178 } | |
179 | |
169 /* NOTE: these should be in the qrc file */ | 180 /* NOTE: these should be in the qrc file */ |
170 const std::string query = "query ($id: Int) {\n" | 181 const std::string query = "query ($id: Int) {\n" |
171 " MediaListCollection (userId: $id, type: ANIME) {\n" | 182 " MediaListCollection (userId: $id, type: ANIME) {\n" |
172 " lists {\n" | 183 " lists {\n" |
173 " name\n" | 184 " name\n" |
174 " entries {\n" | 185 " entries {\n" |
175 " score\n" | 186 " score\n" |
176 " notes\n" | 187 " notes\n" |
177 " status\n" | 188 " status\n" |
178 " progress\n" | 189 " progress\n" |
179 " startedAt {\n" | 190 " startedAt {\n" |
180 " year\n" | 191 " year\n" |
181 " month\n" | 192 " month\n" |
182 " day\n" | 193 " day\n" |
183 " }\n" | 194 " }\n" |
184 " completedAt {\n" | 195 " completedAt {\n" |
185 " year\n" | 196 " year\n" |
186 " month\n" | 197 " month\n" |
187 " day\n" | 198 " day\n" |
188 " }\n" | 199 " }\n" |
189 " updatedAt\n" | 200 " updatedAt\n" |
190 " media {\n" | 201 " media {\n" |
191 " coverImage {\n" | 202 " coverImage {\n" |
192 " large\n" | 203 " large\n" |
193 " }\n" | 204 " }\n" |
194 " id\n" | 205 " id\n" |
195 " title {\n" | 206 " title {\n" |
196 " romaji\n" | 207 " romaji\n" |
197 " english\n" | 208 " english\n" |
198 " native\n" | 209 " native\n" |
199 " }\n" | 210 " }\n" |
200 " format\n" | 211 " format\n" |
201 " status\n" | 212 " status\n" |
202 " averageScore\n" | 213 " averageScore\n" |
203 " season\n" | 214 " season\n" |
204 " startDate {\n" | 215 " startDate {\n" |
205 " year\n" | 216 " year\n" |
206 " month\n" | 217 " month\n" |
207 " day\n" | 218 " day\n" |
208 " }\n" | 219 " }\n" |
209 " genres\n" | 220 " genres\n" |
210 " episodes\n" | 221 " episodes\n" |
211 " duration\n" | 222 " duration\n" |
212 " synonyms\n" | 223 " synonyms\n" |
213 " description(asHtml: false)\n" | 224 " description(asHtml: false)\n" |
214 " }\n" | 225 " }\n" |
215 " }\n" | 226 " }\n" |
216 " }\n" | 227 " }\n" |
217 " }\n" | 228 " }\n" |
218 "}\n"; | 229 "}\n"; |
219 // clang-format off | 230 // clang-format off |
220 nlohmann::json json = { | 231 nlohmann::json json = { |
221 {"query", query}, | 232 {"query", query}, |
222 {"variables", { | 233 {"variables", { |
223 {"id", account.UserId()} | 234 {"id", account.UserId()} |
224 }} | 235 }} |
225 }; | 236 }; |
226 // clang-format on | 237 // clang-format on |
227 /* TODO: do a try catch here, catch any json errors and then call | 238 |
228 Authorize() if needed */ | 239 auto res = SendJSONRequest(json); |
229 auto res = nlohmann::json::parse(SendRequest(json.dump())); | 240 |
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()) { | 241 for (const auto& list : res["data"]["MediaListCollection"]["lists"].items()) { |
233 ParseList(list.value()); | 242 ParseList(list.value()); |
234 } | 243 } |
235 return 1; | 244 return 1; |
236 } | 245 } |
255 * Date startedAt, | 264 * Date startedAt, |
256 * Date completedAt | 265 * Date completedAt |
257 **/ | 266 **/ |
258 Anime::Anime& anime = Anime::db.items[id]; | 267 Anime::Anime& anime = Anime::db.items[id]; |
259 const std::string query = "mutation ($media_id: Int, $progress: Int, $status: MediaListStatus, $score: Int, " | 268 const std::string query = "mutation ($media_id: Int, $progress: Int, $status: MediaListStatus, $score: Int, " |
260 "$notes: String, $start: FuzzyDateInput, $comp: FuzzyDateInput) {\n" | 269 "$notes: String, $start: FuzzyDateInput, $comp: FuzzyDateInput) {\n" |
261 " SaveMediaListEntry (mediaId: $media_id, progress: $progress, status: $status, " | 270 " SaveMediaListEntry (mediaId: $media_id, progress: $progress, status: $status, " |
262 "scoreRaw: $score, notes: $notes, startedAt: $start, completedAt: $comp) {\n" | 271 "scoreRaw: $score, notes: $notes, startedAt: $start, completedAt: $comp) {\n" |
263 " id\n" | 272 " id\n" |
264 " }\n" | 273 " }\n" |
265 "}\n"; | 274 "}\n"; |
266 // clang-format off | 275 // clang-format off |
267 nlohmann::json json = { | 276 nlohmann::json json = { |
268 {"query", query}, | 277 {"query", query}, |
269 {"variables", { | 278 {"variables", { |
270 {"media_id", anime.GetId()}, | 279 {"media_id", anime.GetId()}, |
275 {"start", anime.GetUserDateStarted().GetAsAniListJson()}, | 284 {"start", anime.GetUserDateStarted().GetAsAniListJson()}, |
276 {"comp", anime.GetUserDateCompleted().GetAsAniListJson()} | 285 {"comp", anime.GetUserDateCompleted().GetAsAniListJson()} |
277 }} | 286 }} |
278 }; | 287 }; |
279 // clang-format on | 288 // clang-format on |
280 auto ret = nlohmann::json::parse(SendRequest(json.dump())); | 289 |
281 return JSON::GetInt(ret, "/data/SaveMediaListEntry/id"_json_pointer); | 290 auto ret = SendJSONRequest(json); |
291 | |
292 return JSON::GetNumber(ret, "/data/SaveMediaListEntry/id"_json_pointer, 0); | |
282 } | 293 } |
283 | 294 |
284 int ParseUser(const nlohmann::json& json) { | 295 int ParseUser(const nlohmann::json& json) { |
285 account.SetUsername(JSON::GetString(json, "/name"_json_pointer)); | 296 account.SetUsername(JSON::GetString<std::string>(json, "/name"_json_pointer, "")); |
286 account.SetUserId(JSON::GetInt(json, "/id"_json_pointer)); | 297 account.SetUserId(JSON::GetNumber(json, "/id"_json_pointer, 0)); |
287 return account.UserId(); | 298 return account.UserId(); |
288 } | 299 } |
289 | 300 |
290 bool AuthorizeUser() { | 301 bool AuthorizeUser() { |
291 /* Prompt for PIN */ | 302 /* Prompt for PIN */ |
292 QDesktopServices::openUrl( | 303 QDesktopServices::openUrl( |
293 QUrl("https://anilist.co/api/v2/oauth/authorize?client_id=" CLIENT_ID "&response_type=token")); | 304 QUrl("https://anilist.co/api/v2/oauth/authorize?client_id=" CLIENT_ID "&response_type=token")); |
305 | |
294 bool ok; | 306 bool ok; |
295 QString token = QInputDialog::getText( | 307 QString token = QInputDialog::getText( |
296 0, "Credentials needed!", "Please enter the code given to you after logging in to AniList:", QLineEdit::Normal, | 308 0, "Credentials needed!", "Please enter the code given to you after logging in to AniList:", QLineEdit::Normal, |
297 "", &ok); | 309 "", &ok); |
298 if (ok && !token.isEmpty()) | 310 |
299 account.SetAuthToken(Strings::ToUtf8String(token)); | 311 if (!ok || token.isEmpty()) |
300 else // fail | |
301 return false; | 312 return false; |
313 | |
314 account.SetAuthToken(Strings::ToUtf8String(token)); | |
315 | |
302 const std::string query = "query {\n" | 316 const std::string query = "query {\n" |
303 " Viewer {\n" | 317 " Viewer {\n" |
304 " id\n" | 318 " id\n" |
305 " name\n" | 319 " name\n" |
306 " mediaListOptions {\n" | 320 " mediaListOptions {\n" |
307 " scoreFormat\n" | 321 " scoreFormat\n" // this will be used... eventually |
308 " }\n" | 322 " }\n" |
309 " }\n" | 323 " }\n" |
310 "}\n"; | 324 "}\n"; |
311 nlohmann::json json = { | 325 nlohmann::json json = { |
312 {"query", query} | 326 {"query", query} |
313 }; | 327 }; |
314 auto ret = nlohmann::json::parse(SendRequest(json.dump())); | 328 |
329 auto ret = SendJSONRequest(json); | |
330 | |
315 ParseUser(ret["data"]["Viewer"]); | 331 ParseUser(ret["data"]["Viewer"]); |
316 return true; | 332 return true; |
317 } | 333 } |
318 | 334 |
319 } // namespace AniList | 335 } // namespace AniList |