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
|
15
|
19 using nlohmann::literals::operator"" _json_pointer;
|
11
|
20
|
9
|
21 namespace Services::AniList {
|
|
22
|
|
23 class Account {
|
|
24 public:
|
10
|
25 std::string Username() const { return session.config.anilist.username; }
|
36
|
26 void SetUsername(std::string const& username) { session.config.anilist.username = username; }
|
9
|
27
|
10
|
28 int UserId() const { return session.config.anilist.user_id; }
|
|
29 void SetUserId(const int id) { session.config.anilist.user_id = id; }
|
9
|
30
|
10
|
31 std::string AuthToken() const { return session.config.anilist.auth_token; }
|
36
|
32 void SetAuthToken(std::string const& auth_token) { session.config.anilist.auth_token = auth_token; }
|
9
|
33
|
|
34 bool Authenticated() const { return !AuthToken().empty(); }
|
10
|
35 };
|
9
|
36
|
|
37 static Account account;
|
|
38
|
|
39 static size_t CurlWriteCallback(void* contents, size_t size, size_t nmemb, void* userdata) {
|
|
40 ((std::string*)userdata)->append((char*)contents, size * nmemb);
|
|
41 return size * nmemb;
|
|
42 }
|
|
43
|
|
44 /* A wrapper around cURL to send requests to AniList */
|
|
45 std::string SendRequest(std::string data) {
|
|
46 struct curl_slist* list = NULL;
|
|
47 std::string userdata;
|
|
48 CURL* curl = curl_easy_init();
|
|
49 if (curl) {
|
15
|
50 std::string bearer = "Authorization: Bearer " + account.AuthToken();
|
9
|
51 list = curl_slist_append(list, "Accept: application/json");
|
|
52 list = curl_slist_append(list, "Content-Type: application/json");
|
|
53 list = curl_slist_append(list, bearer.c_str());
|
|
54 curl_easy_setopt(curl, CURLOPT_URL, "https://graphql.anilist.co");
|
|
55 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
|
|
56 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
|
|
57 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &userdata);
|
|
58 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &CurlWriteCallback);
|
|
59 /* Use system certs... useful on Windows. */
|
|
60 curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA);
|
|
61 CURLcode res = curl_easy_perform(curl);
|
|
62 curl_slist_free_all(list);
|
|
63 curl_easy_cleanup(curl);
|
|
64 if (res != CURLE_OK) {
|
|
65 QMessageBox box(QMessageBox::Icon::Critical, "",
|
36
|
66 QString("curl_easy_perform(curl) failed!: ") + QString(curl_easy_strerror(res)));
|
9
|
67 box.exec();
|
|
68 return "";
|
|
69 }
|
|
70 return userdata;
|
|
71 }
|
|
72 return "";
|
|
73 }
|
|
74
|
15
|
75 void ParseListStatus(std::string status, Anime::Anime& anime) {
|
|
76 std::unordered_map<std::string, Anime::ListStatus> map = {
|
|
77 {"CURRENT", Anime::ListStatus::CURRENT },
|
|
78 {"PLANNING", Anime::ListStatus::PLANNING },
|
|
79 {"COMPLETED", Anime::ListStatus::COMPLETED},
|
|
80 {"DROPPED", Anime::ListStatus::DROPPED },
|
|
81 {"PAUSED", Anime::ListStatus::PAUSED }
|
|
82 };
|
9
|
83
|
15
|
84 if (status == "REPEATING") {
|
|
85 anime.SetUserIsRewatching(true);
|
|
86 anime.SetUserStatus(Anime::ListStatus::CURRENT);
|
|
87 return;
|
|
88 }
|
9
|
89
|
15
|
90 if (!map.contains(status)) {
|
|
91 anime.SetUserStatus(Anime::ListStatus::NOT_IN_LIST);
|
|
92 return;
|
|
93 }
|
9
|
94
|
15
|
95 anime.SetUserStatus(map[status]);
|
|
96 }
|
9
|
97
|
15
|
98 std::string ListStatusToString(const Anime::Anime& anime) {
|
|
99 std::unordered_map<Anime::ListStatus, std::string> map = {
|
|
100 {Anime::ListStatus::CURRENT, "CURRENT" },
|
|
101 {Anime::ListStatus::PLANNING, "PLANNING" },
|
|
102 {Anime::ListStatus::COMPLETED, "COMPLETED"},
|
|
103 {Anime::ListStatus::DROPPED, "DROPPED" },
|
|
104 {Anime::ListStatus::PAUSED, "PAUSED" }
|
|
105 };
|
9
|
106
|
15
|
107 if (anime.GetUserIsRewatching())
|
|
108 return "REWATCHING";
|
|
109
|
|
110 if (!map.contains(anime.GetUserStatus()))
|
|
111 return "CURRENT";
|
|
112 return map[anime.GetUserStatus()];
|
|
113 }
|
9
|
114
|
10
|
115 Date ParseDate(const nlohmann::json& json) {
|
|
116 Date date;
|
11
|
117 if (json.contains("/year"_json_pointer) && json.at("/year"_json_pointer).is_number())
|
9
|
118 date.SetYear(JSON::GetInt(json, "/year"_json_pointer));
|
|
119 else
|
|
120 date.VoidYear();
|
|
121
|
11
|
122 if (json.contains("/month"_json_pointer) && json.at("/month"_json_pointer).is_number())
|
9
|
123 date.SetMonth(JSON::GetInt(json, "/month"_json_pointer));
|
|
124 else
|
|
125 date.VoidMonth();
|
|
126
|
11
|
127 if (json.contains("/day"_json_pointer) && json.at("/day"_json_pointer).is_number())
|
9
|
128 date.SetDay(JSON::GetInt(json, "/day"_json_pointer));
|
|
129 else
|
|
130 date.VoidDay();
|
10
|
131 return date;
|
9
|
132 }
|
|
133
|
|
134 void ParseTitle(const nlohmann::json& json, Anime::Anime& anime) {
|
|
135 anime.SetNativeTitle(JSON::GetString(json, "/native"_json_pointer));
|
|
136 anime.SetEnglishTitle(JSON::GetString(json, "/english"_json_pointer));
|
|
137 anime.SetRomajiTitle(JSON::GetString(json, "/romaji"_json_pointer));
|
|
138 }
|
|
139
|
|
140 int ParseMediaJson(const nlohmann::json& json) {
|
|
141 int id = JSON::GetInt(json, "/id"_json_pointer);
|
|
142 if (!id)
|
|
143 return 0;
|
|
144 Anime::Anime& anime = Anime::db.items[id];
|
|
145 anime.SetId(id);
|
|
146
|
11
|
147 ParseTitle(json.at("/title"_json_pointer), anime);
|
9
|
148
|
|
149 anime.SetEpisodes(JSON::GetInt(json, "/episodes"_json_pointer));
|
15
|
150 anime.SetFormat(Translate::AniList::ToSeriesFormat(JSON::GetString(json, "/format"_json_pointer)));
|
9
|
151
|
15
|
152 anime.SetAiringStatus(Translate::AniList::ToSeriesStatus(JSON::GetString(json, "/status"_json_pointer)));
|
9
|
153
|
10
|
154 anime.SetAirDate(ParseDate(json["/startDate"_json_pointer]));
|
9
|
155
|
|
156 anime.SetAudienceScore(JSON::GetInt(json, "/averageScore"_json_pointer));
|
15
|
157 anime.SetSeason(Translate::AniList::ToSeriesSeason(JSON::GetString(json, "/season"_json_pointer)));
|
9
|
158 anime.SetDuration(JSON::GetInt(json, "/duration"_json_pointer));
|
10
|
159 anime.SetSynopsis(Strings::TextifySynopsis(JSON::GetString(json, "/description"_json_pointer)));
|
9
|
160
|
|
161 if (json.contains("/genres"_json_pointer) && json["/genres"_json_pointer].is_array())
|
|
162 anime.SetGenres(json["/genres"_json_pointer].get<std::vector<std::string>>());
|
|
163 if (json.contains("/synonyms"_json_pointer) && json["/synonyms"_json_pointer].is_array())
|
10
|
164 anime.SetTitleSynonyms(json["/synonyms"_json_pointer].get<std::vector<std::string>>());
|
|
165 return id;
|
9
|
166 }
|
|
167
|
10
|
168 int ParseListItem(const nlohmann::json& json) {
|
|
169 int id = ParseMediaJson(json["media"]);
|
|
170
|
|
171 Anime::Anime& anime = Anime::db.items[id];
|
|
172
|
|
173 anime.AddToUserList();
|
9
|
174
|
10
|
175 anime.SetUserScore(JSON::GetInt(json, "/score"_json_pointer));
|
|
176 anime.SetUserProgress(JSON::GetInt(json, "/progress"_json_pointer));
|
15
|
177 ParseListStatus(JSON::GetString(json, "/status"_json_pointer), anime);
|
10
|
178 anime.SetUserNotes(JSON::GetString(json, "/notes"_json_pointer));
|
9
|
179
|
10
|
180 anime.SetUserDateStarted(ParseDate(json["/startedAt"_json_pointer]));
|
|
181 anime.SetUserDateCompleted(ParseDate(json["/completedAt"_json_pointer]));
|
9
|
182
|
10
|
183 anime.SetUserTimeUpdated(JSON::GetInt(json, "/updatedAt"_json_pointer));
|
|
184
|
|
185 return id;
|
9
|
186 }
|
|
187
|
|
188 int ParseList(const nlohmann::json& json) {
|
|
189 for (const auto& entry : json["entries"].items()) {
|
|
190 ParseListItem(entry.value());
|
|
191 }
|
10
|
192 return 1;
|
9
|
193 }
|
|
194
|
10
|
195 int GetAnimeList() {
|
9
|
196 /* NOTE: these should be in the qrc file */
|
|
197 const std::string query = "query ($id: Int) {\n"
|
15
|
198 " MediaListCollection (userId: $id, type: ANIME) {\n"
|
|
199 " lists {\n"
|
|
200 " name\n"
|
|
201 " entries {\n"
|
|
202 " score\n"
|
|
203 " notes\n"
|
|
204 " status\n"
|
|
205 " progress\n"
|
|
206 " startedAt {\n"
|
|
207 " year\n"
|
|
208 " month\n"
|
|
209 " day\n"
|
|
210 " }\n"
|
|
211 " completedAt {\n"
|
|
212 " year\n"
|
|
213 " month\n"
|
|
214 " day\n"
|
|
215 " }\n"
|
|
216 " updatedAt\n"
|
|
217 " media {\n"
|
|
218 " id\n"
|
|
219 " title {\n"
|
|
220 " romaji\n"
|
|
221 " english\n"
|
|
222 " native\n"
|
|
223 " }\n"
|
|
224 " format\n"
|
|
225 " status\n"
|
|
226 " averageScore\n"
|
|
227 " season\n"
|
|
228 " startDate {\n"
|
|
229 " year\n"
|
|
230 " month\n"
|
|
231 " day\n"
|
|
232 " }\n"
|
|
233 " genres\n"
|
|
234 " episodes\n"
|
|
235 " duration\n"
|
|
236 " synonyms\n"
|
|
237 " description(asHtml: false)\n"
|
|
238 " }\n"
|
|
239 " }\n"
|
|
240 " }\n"
|
|
241 " }\n"
|
|
242 "}\n";
|
9
|
243 // clang-format off
|
|
244 nlohmann::json json = {
|
|
245 {"query", query},
|
|
246 {"variables", {
|
10
|
247 {"id", account.UserId()}
|
9
|
248 }}
|
|
249 };
|
|
250 // clang-format on
|
|
251 /* TODO: do a try catch here, catch any json errors and then call
|
|
252 Authorize() if needed */
|
|
253 auto res = nlohmann::json::parse(SendRequest(json.dump()));
|
|
254 /* TODO: make sure that we actually need the wstring converter and see
|
|
255 if we can just get wide strings back from nlohmann::json */
|
|
256 for (const auto& list : res["data"]["MediaListCollection"]["lists"].items()) {
|
10
|
257 ParseList(list.value());
|
9
|
258 }
|
|
259 return 1;
|
|
260 }
|
|
261
|
10
|
262 int UpdateAnimeEntry(const Anime::Anime& anime) {
|
9
|
263 /**
|
|
264 * possible values:
|
15
|
265 *
|
9
|
266 * int mediaId,
|
|
267 * MediaListStatus status,
|
|
268 * float score,
|
|
269 * int scoreRaw,
|
|
270 * int progress,
|
|
271 * int progressVolumes,
|
|
272 * int repeat,
|
|
273 * int priority,
|
|
274 * bool private,
|
|
275 * string notes,
|
|
276 * bool hiddenFromStatusLists,
|
|
277 * string[] customLists,
|
|
278 * float[] advancedScores,
|
|
279 * Date startedAt,
|
|
280 * Date completedAt
|
15
|
281 **/
|
|
282 const std::string query = "mutation ($media_id: Int, $progress: Int, $status: MediaListStatus, "
|
|
283 "$score: Int, $notes: String) {\n"
|
|
284 " SaveMediaListEntry (mediaId: $media_id, progress: $progress, "
|
|
285 "status: $status, scoreRaw: $score, notes: "
|
|
286 "$notes) {\n"
|
|
287 " id\n"
|
|
288 " }\n"
|
|
289 "}\n";
|
9
|
290 // clang-format off
|
|
291 nlohmann::json json = {
|
|
292 {"query", query},
|
|
293 {"variables", {
|
10
|
294 {"media_id", anime.GetId()},
|
|
295 {"progress", anime.GetUserProgress()},
|
15
|
296 {"status", ListStatusToString(anime)},
|
10
|
297 {"score", anime.GetUserScore()},
|
|
298 {"notes", anime.GetUserNotes()}
|
9
|
299 }}
|
|
300 };
|
|
301 // clang-format on
|
|
302 SendRequest(json.dump());
|
|
303 return 1;
|
|
304 }
|
|
305
|
|
306 int ParseUser(const nlohmann::json& json) {
|
|
307 account.SetUsername(JSON::GetString(json, "/name"_json_pointer));
|
|
308 account.SetUserId(JSON::GetInt(json, "/id"_json_pointer));
|
10
|
309 return account.UserId();
|
9
|
310 }
|
|
311
|
|
312 int AuthorizeUser() {
|
|
313 /* Prompt for PIN */
|
36
|
314 QDesktopServices::openUrl(
|
|
315 QUrl("https://anilist.co/api/v2/oauth/authorize?client_id=" CLIENT_ID "&response_type=token"));
|
9
|
316 bool ok;
|
|
317 QString token = QInputDialog::getText(
|
36
|
318 0, "Credentials needed!", "Please enter the code given to you after logging in to AniList:", QLineEdit::Normal,
|
|
319 "", &ok);
|
9
|
320 if (ok && !token.isEmpty())
|
|
321 account.SetAuthToken(token.toStdString());
|
15
|
322 else // fail
|
9
|
323 return 0;
|
|
324 const std::string query = "query {\n"
|
15
|
325 " Viewer {\n"
|
|
326 " id\n"
|
|
327 " name\n"
|
|
328 " mediaListOptions {\n"
|
|
329 " scoreFormat\n"
|
|
330 " }\n"
|
|
331 " }\n"
|
|
332 "}\n";
|
9
|
333 nlohmann::json json = {
|
15
|
334 {"query", query}
|
|
335 };
|
9
|
336 auto ret = nlohmann::json::parse(SendRequest(json.dump()));
|
10
|
337 ParseUser(json["Viewer"]);
|
9
|
338 return 1;
|
|
339 }
|
|
340
|
|
341 } // namespace Services::AniList
|