annotate src/anilist.cpp @ 5:51ae25154b70

Fix OS X support code
author Paper <mrpapersonic@gmail.com>
date Sat, 12 Aug 2023 13:10:34 -0400
parents 5af270662505
children 1d82f6e04d7d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
1 #include "window.h"
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
2 #include "json.h"
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
3 #include <curl/curl.h>
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
4 #include <chrono>
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
5 #include <exception>
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
6 #include <format>
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
7 #include "anilist.h"
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
8 #include "anime.h"
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
9 #include "config.h"
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
10 #include "string_utils.h"
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
11 #define CLIENT_ID "13706"
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
12
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
13 size_t AniList::CurlWriteCallback(void *contents, size_t size, size_t nmemb, void *userdata) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
14 ((std::string*)userdata)->append((char*)contents, size * nmemb);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
15 return size * nmemb;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
16 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
17
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
18 std::string AniList::SendRequest(std::string data) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
19 struct curl_slist *list = NULL;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
20 std::string userdata;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
21 curl = curl_easy_init();
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
22 if (curl) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
23 list = curl_slist_append(list, "Accept: application/json");
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
24 list = curl_slist_append(list, "Content-Type: application/json");
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
25 std::string bearer = "Authorization: Bearer " + session.config.anilist.auth_token;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
26 list = curl_slist_append(list, bearer.c_str());
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
27 curl_easy_setopt(curl, CURLOPT_URL, "https://graphql.anilist.co");
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
28 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
29 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
30 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &userdata);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
31 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &CurlWriteCallback);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
32 /* FIXME: This sucks. When using HTTPS, we should ALWAYS make sure that our peer
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
33 is actually valid. I assume the best way to go about this would be to bundle a
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
34 certificate file, and if it's not found we should *prompt the user* and ask them
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
35 if it's okay to contact AniList WITHOUT verification. If so, we're golden, and this
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
36 flag will be set. If not, we should abort mission.
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
37
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
38 For this program, it's probably fine to just contact AniList without
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
39 HTTPS verification. However it should still be in the list of things to do... */
4
5af270662505 Set override functions as override
Paper <mrpapersonic@gmail.com>
parents: 3
diff changeset
40 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
41 res = curl_easy_perform(curl);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
42 curl_slist_free_all(list);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
43 if (res != CURLE_OK) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
44 QMessageBox box(QMessageBox::Icon::Critical, "", QString("curl_easy_perform(curl) failed!: ") + QString(curl_easy_strerror(res)));
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
45 box.exec();
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
46 curl_easy_cleanup(curl);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
47 return "";
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
48 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
49 curl_easy_cleanup(curl);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
50 return userdata;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
51 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
52 return "";
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
53 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
54
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
55 int AniList::GetUserId(std::string name) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
56 #define QUERY "query ($name: String) {\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
57 " User (name: $name) {\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
58 " id\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
59 " }\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
60 "}\n"
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
61 nlohmann::json json = {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
62 {"query", QUERY},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
63 {"variables", {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
64 {"name", name}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
65 }}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
66 };
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
67 auto ret = nlohmann::json::parse(SendRequest(json.dump()));
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
68 return ret["data"]["User"]["id"].get<int>();
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
69 #undef QUERY
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
70 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
71
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
72 /* Maps to convert string forms to our internal enums */
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
73
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
74 std::map<std::string, enum AnimeWatchingStatus> StringToAnimeWatchingMap = {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
75 {"CURRENT", CURRENT},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
76 {"PLANNING", PLANNING},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
77 {"COMPLETED", COMPLETED},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
78 {"DROPPED", DROPPED},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
79 {"PAUSED", PAUSED},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
80 {"REPEATING", REPEATING}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
81 };
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
82
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
83 std::map<std::string, enum AnimeAiringStatus> StringToAnimeAiringMap = {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
84 {"FINISHED", FINISHED},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
85 {"RELEASING", RELEASING},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
86 {"NOT_YET_RELEASED", NOT_YET_RELEASED},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
87 {"CANCELLED", CANCELLED},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
88 {"HIATUS", HIATUS}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
89 };
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
90
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
91 std::map<std::string, enum AnimeSeason> StringToAnimeSeasonMap = {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
92 {"WINTER", WINTER},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
93 {"SPRING", SPRING},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
94 {"SUMMER", SUMMER},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
95 {"FALL", FALL}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
96 };
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
97
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
98 std::map<std::string, enum AnimeFormat> StringToAnimeFormatMap = {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
99 {"TV", TV},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
100 {"TV_SHORT", TV_SHORT},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
101 {"MOVIE", MOVIE},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
102 {"SPECIAL", SPECIAL},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
103 {"OVA", OVA},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
104 {"ONA", ONA},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
105 {"MUSIC", MUSIC},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
106 {"MANGA", MANGA},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
107 {"NOVEL", NOVEL},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
108 {"ONE_SHOT", ONE_SHOT}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
109 };
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
110
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
111 int AniList::UpdateAnimeList(std::vector<AnimeList>* anime_lists, int id) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
112 /* NOTE: these should be in the qrc file */
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
113 #define QUERY "query ($id: Int) {\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
114 " MediaListCollection (userId: $id, type: ANIME) {\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
115 " lists {\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
116 " name\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
117 " entries {\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
118 " score\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
119 " notes\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
120 " progress\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
121 " startedAt {\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
122 " year\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
123 " month\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
124 " day\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
125 " }\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
126 " completedAt {\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
127 " year\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
128 " month\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
129 " day\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
130 " }\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
131 " updatedAt\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
132 " media {\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
133 " id\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
134 " title {\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
135 " romaji\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
136 " english\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
137 " native\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
138 " }\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
139 " format\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
140 " status\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
141 " averageScore\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
142 " season\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
143 " startDate {\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
144 " year\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
145 " month\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
146 " day\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
147 " }\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
148 " genres\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
149 " episodes\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
150 " duration\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
151 " synonyms\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
152 " description(asHtml: false)\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
153 " }\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
154 " }\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
155 " }\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
156 " }\n" \
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
157 "}\n"
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
158 nlohmann::json json = {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
159 {"query", QUERY},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
160 {"variables", {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
161 {"id", id}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
162 }}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
163 };
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
164 /* TODO: do a try catch here, catch any json errors and then call
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
165 Authorize() if needed */
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
166 auto res = nlohmann::json::parse(SendRequest(json.dump()));
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
167 /* TODO: make sure that we actually need the wstring converter and see
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
168 if we can just get wide strings back from nlohmann::json */
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
169 for (const auto& list : res["data"]["MediaListCollection"]["lists"].items()) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
170 /* why are the .key() values strings?? */
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
171 AnimeList anime_list;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
172 anime_list.name = StringUtils::Utf8ToWstr(JSON::GetString(list.value(), "name"));
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
173 for (const auto& entry : list.value()["entries"].items()) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
174 Anime anime;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
175 anime.score = JSON::GetInt(entry.value(), "score");
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
176 anime.progress = JSON::GetInt(entry.value(), "progress");
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
177 anime.status = StringToAnimeWatchingMap[JSON::GetString(entry.value(), "status")];
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
178 anime.notes = StringUtils::Utf8ToWstr(JSON::GetString(entry.value(), "notes"));
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
179
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
180 anime.started.SetYear(JSON::GetInt(entry.value()["startedAt"], "year"));
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
181 anime.started.SetMonth(JSON::GetInt(entry.value()["startedAt"], "month"));
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
182 anime.started.SetDay(JSON::GetInt(entry.value()["startedAt"], "day"));
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
183
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
184 anime.completed.SetYear(JSON::GetInt(entry.value()["completedAt"], "year"));
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
185 anime.completed.SetMonth(JSON::GetInt(entry.value()["completedAt"], "month"));
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
186 anime.completed.SetDay(JSON::GetInt(entry.value()["completedAt"], "day"));
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
187
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
188 anime.updated = JSON::GetInt(entry.value(), "updatedAt");
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
189
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
190 anime.title.native = StringUtils::Utf8ToWstr(JSON::GetString(entry.value()["media"]["title"], "native"));
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
191 anime.title.english = StringUtils::Utf8ToWstr(JSON::GetString(entry.value()["media"]["title"], "english"));
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
192 anime.title.romaji = StringUtils::Utf8ToWstr(JSON::GetString(entry.value()["media"]["title"], "romaji"));
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
193
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
194 anime.id = JSON::GetInt(entry.value()["media"], "id");
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
195 anime.episodes = JSON::GetInt(entry.value()["media"], "episodes");
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
196 anime.type = StringToAnimeFormatMap[JSON::GetString(entry.value()["media"], "format")];
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
197
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
198 anime.airing = StringToAnimeAiringMap[JSON::GetString(entry.value()["media"], "status")];
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
199
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
200 anime.air_date.SetYear(JSON::GetInt(entry.value()["media"]["startDate"], "year"));
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
201 anime.air_date.SetMonth(JSON::GetInt(entry.value()["media"]["startDate"], "month"));
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
202 anime.air_date.SetDay(JSON::GetInt(entry.value()["media"]["startDate"], "day"));
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
203
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
204 anime.audience_score = JSON::GetInt(entry.value()["media"], "averageScore");
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
205 anime.season = StringToAnimeSeasonMap[JSON::GetString(entry.value()["media"], "season")];
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
206 anime.duration = JSON::GetInt(entry.value()["media"], "duration");
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
207 anime.synopsis = StringUtils::TextifySynopsis(StringUtils::Utf8ToWstr(JSON::GetString(entry.value()["media"], "duration")));
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
208
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
209 if (entry.value()["media"]["genres"].is_array())
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
210 anime.genres = entry.value()["media"]["genres"].get<std::vector<std::string>>();
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
211 anime_list.Add(anime);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
212 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
213 anime_lists->push_back(anime_list);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
214 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
215 return 1;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
216 #undef QUERY
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
217 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
218
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
219 int AniList::Authorize() {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
220 if (session.config.anilist.auth_token.empty()) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
221 /* Prompt for PIN */
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
222 QDesktopServices::openUrl(QUrl("https://anilist.co/api/v2/oauth/authorize?client_id=" CLIENT_ID "&response_type=token"));
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
223 bool ok;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
224 QString token = QInputDialog::getText(0, "Credentials needed!", "Please enter the code given to you after logging in to AniList:", QLineEdit::Normal, "", &ok);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
225 if (ok && !token.isEmpty()) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
226 session.config.anilist.auth_token = token.toStdString();
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
227 } else { // fail
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
228 return 0;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
229 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
230 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
231 return 1;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
232 }