Mercurial > minori
comparison src/services/anilist.cc @ 81:9b2b41f83a5e
boring: mass rename to cc
because this is a very unix-y project, it makes more sense to use the
'cc' extension
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Mon, 23 Oct 2023 12:07:27 -0400 |
parents | src/services/anilist.cpp@6f7385bd334c |
children | 275da698697d |
comparison
equal
deleted
inserted
replaced
80:825506f0e221 | 81:9b2b41f83a5e |
---|---|
1 #include "services/anilist.h" | |
2 #include "core/anime.h" | |
3 #include "core/anime_db.h" | |
4 #include "core/config.h" | |
5 #include "core/http.h" | |
6 #include "core/json.h" | |
7 #include "core/session.h" | |
8 #include "core/strings.h" | |
9 #include "gui/translate/anilist.h" | |
10 #include <QByteArray> | |
11 #include <QDesktopServices> | |
12 #include <QInputDialog> | |
13 #include <QLineEdit> | |
14 #include <QMessageBox> | |
15 #include <QUrl> | |
16 #include <chrono> | |
17 #include <exception> | |
18 #define CLIENT_ID "13706" | |
19 | |
20 using namespace nlohmann::literals::json_literals; | |
21 | |
22 namespace Services { | |
23 namespace AniList { | |
24 | |
25 class Account { | |
26 public: | |
27 std::string Username() const { return session.config.anilist.username; } | |
28 void SetUsername(std::string const& username) { session.config.anilist.username = username; } | |
29 | |
30 int UserId() const { return session.config.anilist.user_id; } | |
31 void SetUserId(const int id) { session.config.anilist.user_id = id; } | |
32 | |
33 std::string AuthToken() const { return session.config.anilist.auth_token; } | |
34 void SetAuthToken(std::string const& auth_token) { session.config.anilist.auth_token = auth_token; } | |
35 | |
36 bool Authenticated() const { return !AuthToken().empty(); } | |
37 }; | |
38 | |
39 static Account account; | |
40 | |
41 std::string SendRequest(std::string data) { | |
42 std::vector<std::string> headers = {"Authorization: Bearer " + account.AuthToken(), "Accept: application/json", | |
43 "Content-Type: application/json"}; | |
44 return Strings::ToUtf8String(HTTP::Post("https://graphql.anilist.co", data, headers)); | |
45 } | |
46 | |
47 void ParseListStatus(std::string status, Anime::Anime& anime) { | |
48 std::unordered_map<std::string, Anime::ListStatus> map = { | |
49 {"CURRENT", Anime::ListStatus::CURRENT }, | |
50 {"PLANNING", Anime::ListStatus::PLANNING }, | |
51 {"COMPLETED", Anime::ListStatus::COMPLETED}, | |
52 {"DROPPED", Anime::ListStatus::DROPPED }, | |
53 {"PAUSED", Anime::ListStatus::PAUSED } | |
54 }; | |
55 | |
56 if (status == "REPEATING") { | |
57 anime.SetUserIsRewatching(true); | |
58 anime.SetUserStatus(Anime::ListStatus::CURRENT); | |
59 return; | |
60 } | |
61 | |
62 if (map.find(status) == map.end()) { | |
63 anime.SetUserStatus(Anime::ListStatus::NOT_IN_LIST); | |
64 return; | |
65 } | |
66 | |
67 anime.SetUserStatus(map[status]); | |
68 } | |
69 | |
70 std::string ListStatusToString(const Anime::Anime& anime) { | |
71 if (anime.GetUserIsRewatching()) | |
72 return "REWATCHING"; | |
73 | |
74 switch (anime.GetUserStatus()) { | |
75 case Anime::ListStatus::PLANNING: return "PLANNING"; | |
76 case Anime::ListStatus::COMPLETED: return "COMPLETED"; | |
77 case Anime::ListStatus::DROPPED: return "DROPPED"; | |
78 case Anime::ListStatus::PAUSED: return "PAUSED"; | |
79 default: break; | |
80 } | |
81 return "CURRENT"; | |
82 } | |
83 | |
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) { | |
106 anime.SetNativeTitle(JSON::GetString(json, "/native"_json_pointer)); | |
107 anime.SetEnglishTitle(JSON::GetString(json, "/english"_json_pointer)); | |
108 anime.SetRomajiTitle(JSON::GetString(json, "/romaji"_json_pointer)); | |
109 } | |
110 | |
111 int ParseMediaJson(const nlohmann::json& json) { | |
112 int id = JSON::GetInt(json, "/id"_json_pointer); | |
113 if (!id) | |
114 return 0; | |
115 Anime::Anime& anime = Anime::db.items[id]; | |
116 anime.SetId(id); | |
117 | |
118 ParseTitle(json.at("/title"_json_pointer), anime); | |
119 | |
120 anime.SetEpisodes(JSON::GetInt(json, "/episodes"_json_pointer)); | |
121 anime.SetFormat(Translate::AniList::ToSeriesFormat(JSON::GetString(json, "/format"_json_pointer))); | |
122 | |
123 anime.SetAiringStatus(Translate::AniList::ToSeriesStatus(JSON::GetString(json, "/status"_json_pointer))); | |
124 | |
125 anime.SetAirDate(ParseDate(json["/startDate"_json_pointer])); | |
126 | |
127 anime.SetPosterUrl(JSON::GetString(json, "/coverImage/large"_json_pointer)); | |
128 | |
129 anime.SetAudienceScore(JSON::GetInt(json, "/averageScore"_json_pointer)); | |
130 anime.SetSeason(Translate::AniList::ToSeriesSeason(JSON::GetString(json, "/season"_json_pointer))); | |
131 anime.SetDuration(JSON::GetInt(json, "/duration"_json_pointer)); | |
132 anime.SetSynopsis(Strings::TextifySynopsis(JSON::GetString(json, "/description"_json_pointer))); | |
133 | |
134 if (json.contains("/genres"_json_pointer) && json["/genres"_json_pointer].is_array()) | |
135 anime.SetGenres(json["/genres"_json_pointer].get<std::vector<std::string>>()); | |
136 if (json.contains("/synonyms"_json_pointer) && json["/synonyms"_json_pointer].is_array()) | |
137 anime.SetTitleSynonyms(json["/synonyms"_json_pointer].get<std::vector<std::string>>()); | |
138 return id; | |
139 } | |
140 | |
141 int ParseListItem(const nlohmann::json& json) { | |
142 int id = ParseMediaJson(json["media"]); | |
143 | |
144 Anime::Anime& anime = Anime::db.items[id]; | |
145 | |
146 anime.AddToUserList(); | |
147 | |
148 anime.SetUserScore(JSON::GetInt(json, "/score"_json_pointer)); | |
149 anime.SetUserProgress(JSON::GetInt(json, "/progress"_json_pointer)); | |
150 ParseListStatus(JSON::GetString(json, "/status"_json_pointer), anime); | |
151 anime.SetUserNotes(JSON::GetString(json, "/notes"_json_pointer)); | |
152 | |
153 anime.SetUserDateStarted(ParseDate(json["/startedAt"_json_pointer])); | |
154 anime.SetUserDateCompleted(ParseDate(json["/completedAt"_json_pointer])); | |
155 | |
156 anime.SetUserTimeUpdated(JSON::GetInt(json, "/updatedAt"_json_pointer)); | |
157 | |
158 return id; | |
159 } | |
160 | |
161 int ParseList(const nlohmann::json& json) { | |
162 for (const auto& entry : json["entries"].items()) { | |
163 ParseListItem(entry.value()); | |
164 } | |
165 return 1; | |
166 } | |
167 | |
168 int GetAnimeList() { | |
169 /* NOTE: these should be in the qrc file */ | |
170 const std::string query = "query ($id: Int) {\n" | |
171 " MediaListCollection (userId: $id, type: ANIME) {\n" | |
172 " lists {\n" | |
173 " name\n" | |
174 " entries {\n" | |
175 " score\n" | |
176 " notes\n" | |
177 " status\n" | |
178 " progress\n" | |
179 " startedAt {\n" | |
180 " year\n" | |
181 " month\n" | |
182 " day\n" | |
183 " }\n" | |
184 " completedAt {\n" | |
185 " year\n" | |
186 " month\n" | |
187 " day\n" | |
188 " }\n" | |
189 " updatedAt\n" | |
190 " media {\n" | |
191 " coverImage {\n" | |
192 " large\n" | |
193 " }\n" | |
194 " id\n" | |
195 " title {\n" | |
196 " romaji\n" | |
197 " english\n" | |
198 " native\n" | |
199 " }\n" | |
200 " format\n" | |
201 " status\n" | |
202 " averageScore\n" | |
203 " season\n" | |
204 " startDate {\n" | |
205 " year\n" | |
206 " month\n" | |
207 " day\n" | |
208 " }\n" | |
209 " genres\n" | |
210 " episodes\n" | |
211 " duration\n" | |
212 " synonyms\n" | |
213 " description(asHtml: false)\n" | |
214 " }\n" | |
215 " }\n" | |
216 " }\n" | |
217 " }\n" | |
218 "}\n"; | |
219 // clang-format off | |
220 nlohmann::json json = { | |
221 {"query", query}, | |
222 {"variables", { | |
223 {"id", account.UserId()} | |
224 }} | |
225 }; | |
226 // clang-format on | |
227 /* TODO: do a try catch here, catch any json errors and then call | |
228 Authorize() if needed */ | |
229 auto res = nlohmann::json::parse(SendRequest(json.dump())); | |
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()) { | |
233 ParseList(list.value()); | |
234 } | |
235 return 1; | |
236 } | |
237 | |
238 int UpdateAnimeEntry(int id) { | |
239 /** | |
240 * possible values: | |
241 * | |
242 * int mediaId, | |
243 * MediaListStatus status, | |
244 * float score, | |
245 * int scoreRaw, | |
246 * int progress, | |
247 * int progressVolumes, | |
248 * int repeat, | |
249 * int priority, | |
250 * bool private, | |
251 * string notes, | |
252 * bool hiddenFromStatusLists, | |
253 * string[] customLists, | |
254 * float[] advancedScores, | |
255 * Date startedAt, | |
256 * Date completedAt | |
257 **/ | |
258 Anime::Anime& anime = Anime::db.items[id]; | |
259 const std::string query = "mutation ($media_id: Int, $progress: Int, $status: MediaListStatus, $score: Int, " | |
260 "$notes: String, $start: FuzzyDateInput, $comp: FuzzyDateInput) {\n" | |
261 " SaveMediaListEntry (mediaId: $media_id, progress: $progress, status: $status, " | |
262 "scoreRaw: $score, notes: $notes, startedAt: $start, completedAt: $comp) {\n" | |
263 " id\n" | |
264 " }\n" | |
265 "}\n"; | |
266 // clang-format off | |
267 nlohmann::json json = { | |
268 {"query", query}, | |
269 {"variables", { | |
270 {"media_id", anime.GetId()}, | |
271 {"progress", anime.GetUserProgress()}, | |
272 {"status", ListStatusToString(anime)}, | |
273 {"score", anime.GetUserScore()}, | |
274 {"notes", anime.GetUserNotes()}, | |
275 {"start", anime.GetUserDateStarted().GetAsAniListJson()}, | |
276 {"comp", anime.GetUserDateCompleted().GetAsAniListJson()} | |
277 }} | |
278 }; | |
279 // clang-format on | |
280 auto ret = nlohmann::json::parse(SendRequest(json.dump())); | |
281 return JSON::GetInt(ret, "/data/SaveMediaListEntry/id"_json_pointer); | |
282 } | |
283 | |
284 int ParseUser(const nlohmann::json& json) { | |
285 account.SetUsername(JSON::GetString(json, "/name"_json_pointer)); | |
286 account.SetUserId(JSON::GetInt(json, "/id"_json_pointer)); | |
287 return account.UserId(); | |
288 } | |
289 | |
290 bool AuthorizeUser() { | |
291 /* Prompt for PIN */ | |
292 QDesktopServices::openUrl( | |
293 QUrl("https://anilist.co/api/v2/oauth/authorize?client_id=" CLIENT_ID "&response_type=token")); | |
294 bool ok; | |
295 QString token = QInputDialog::getText( | |
296 0, "Credentials needed!", "Please enter the code given to you after logging in to AniList:", QLineEdit::Normal, | |
297 "", &ok); | |
298 if (ok && !token.isEmpty()) | |
299 account.SetAuthToken(Strings::ToUtf8String(token)); | |
300 else // fail | |
301 return false; | |
302 const std::string query = "query {\n" | |
303 " Viewer {\n" | |
304 " id\n" | |
305 " name\n" | |
306 " mediaListOptions {\n" | |
307 " scoreFormat\n" | |
308 " }\n" | |
309 " }\n" | |
310 "}\n"; | |
311 nlohmann::json json = { | |
312 {"query", query} | |
313 }; | |
314 auto ret = nlohmann::json::parse(SendRequest(json.dump())); | |
315 ParseUser(ret["data"]["Viewer"]); | |
316 return true; | |
317 } | |
318 | |
319 } // namespace AniList | |
320 } // namespace Services |