Mercurial > minori
annotate src/services/anilist.cc @ 184:09492158bcc5
anime: etc. comments and changes
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Mon, 04 Dec 2023 13:44:42 -0500 |
parents | 01d259b9c89f |
children | 62e336597bb7 |
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 | |
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
180 /* NOTE: these really ought to be in the qrc file */ |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
181 constexpr std::string_view 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, | |
184
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
256 * int progressVolumes, // manga-specific. |
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
257 * int repeat, // rewatch |
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
258 * int priority, |
9 | 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]; |
184
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
268 if (!anime.IsInUserList()) |
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
269 return 0; |
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
270 |
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
271 constexpr std::string_view query = "mutation ($media_id: Int, $progress: Int, $status: MediaListStatus, $score: Int, " |
184
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
272 "$notes: String, $start: FuzzyDateInput, $comp: FuzzyDateInput, $repeat: Int) {\n" |
175 | 273 " SaveMediaListEntry (mediaId: $media_id, progress: $progress, status: $status, " |
184
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
274 "scoreRaw: $score, notes: $notes, startedAt: $start, completedAt: $comp, repeat: $repeat) {\n" |
175 | 275 " id\n" |
276 " }\n" | |
277 "}\n"; | |
9 | 278 // clang-format off |
279 nlohmann::json json = { | |
280 {"query", query}, | |
281 {"variables", { | |
10 | 282 {"media_id", anime.GetId()}, |
283 {"progress", anime.GetUserProgress()}, | |
15 | 284 {"status", ListStatusToString(anime)}, |
10 | 285 {"score", anime.GetUserScore()}, |
77 | 286 {"notes", anime.GetUserNotes()}, |
287 {"start", anime.GetUserDateStarted().GetAsAniListJson()}, | |
184
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
288 {"comp", anime.GetUserDateCompleted().GetAsAniListJson()}, |
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
289 {"repeat", anime.GetUserRewatchedTimes()} |
9 | 290 }} |
291 }; | |
292 // clang-format on | |
175 | 293 |
294 auto ret = SendJSONRequest(json); | |
295 | |
296 return JSON::GetNumber(ret, "/data/SaveMediaListEntry/id"_json_pointer, 0); | |
9 | 297 } |
298 | |
299 int ParseUser(const nlohmann::json& json) { | |
175 | 300 account.SetUsername(JSON::GetString<std::string>(json, "/name"_json_pointer, "")); |
301 account.SetUserId(JSON::GetNumber(json, "/id"_json_pointer, 0)); | |
10 | 302 return account.UserId(); |
9 | 303 } |
304 | |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
305 bool AuthorizeUser() { |
9 | 306 /* Prompt for PIN */ |
36 | 307 QDesktopServices::openUrl( |
175 | 308 QUrl("https://anilist.co/api/v2/oauth/authorize?client_id=" CLIENT_ID "&response_type=token")); |
309 | |
9 | 310 bool ok; |
311 QString token = QInputDialog::getText( | |
175 | 312 0, "Credentials needed!", "Please enter the code given to you after logging in to AniList:", QLineEdit::Normal, |
313 "", &ok); | |
314 | |
315 if (!ok || token.isEmpty()) | |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
316 return false; |
175 | 317 |
318 account.SetAuthToken(Strings::ToUtf8String(token)); | |
319 | |
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
320 constexpr std::string_view query = "query {\n" |
175 | 321 " Viewer {\n" |
322 " id\n" | |
323 " name\n" | |
324 " mediaListOptions {\n" | |
325 " scoreFormat\n" // this will be used... eventually | |
326 " }\n" | |
327 " }\n" | |
328 "}\n"; | |
9 | 329 nlohmann::json json = { |
175 | 330 {"query", query} |
331 }; | |
332 | |
333 auto ret = SendJSONRequest(json); | |
334 | |
74 | 335 ParseUser(ret["data"]["Viewer"]); |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
336 return true; |
9 | 337 } |
338 | |
63 | 339 } // namespace AniList |
340 } // namespace Services |