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