comparison src/anilist.cpp @ 6:1d82f6e04d7d

Update: add first parts to the settings dialog
author Paper <mrpapersonic@gmail.com>
date Wed, 16 Aug 2023 00:49:17 -0400
parents 5af270662505
children 07a9095eaeed
comparison
equal deleted inserted replaced
5:51ae25154b70 6:1d82f6e04d7d
63 {"variables", { 63 {"variables", {
64 {"name", name} 64 {"name", name}
65 }} 65 }}
66 }; 66 };
67 auto ret = nlohmann::json::parse(SendRequest(json.dump())); 67 auto ret = nlohmann::json::parse(SendRequest(json.dump()));
68 return ret["data"]["User"]["id"].get<int>(); 68 return JSON::GetInt(ret, "/data/User/id"_json_pointer);
69 #undef QUERY 69 #undef QUERY
70 } 70 }
71 71
72 /* Maps to convert string forms to our internal enums */ 72 /* Maps to convert string forms to our internal enums */
73 73
167 /* TODO: make sure that we actually need the wstring converter and see 167 /* TODO: make sure that we actually need the wstring converter and see
168 if we can just get wide strings back from nlohmann::json */ 168 if we can just get wide strings back from nlohmann::json */
169 for (const auto& list : res["data"]["MediaListCollection"]["lists"].items()) { 169 for (const auto& list : res["data"]["MediaListCollection"]["lists"].items()) {
170 /* why are the .key() values strings?? */ 170 /* why are the .key() values strings?? */
171 AnimeList anime_list; 171 AnimeList anime_list;
172 anime_list.name = StringUtils::Utf8ToWstr(JSON::GetString(list.value(), "name")); 172 anime_list.name = JSON::GetString(list.value(), "/name"_json_pointer);
173 for (const auto& entry : list.value()["entries"].items()) { 173 for (const auto& entry : list.value()["entries"].items()) {
174 Anime anime; 174 Anime anime;
175 anime.score = JSON::GetInt(entry.value(), "score"); 175 anime.score = JSON::GetInt(entry.value(), "/score"_json_pointer);
176 anime.progress = JSON::GetInt(entry.value(), "progress"); 176 anime.progress = JSON::GetInt(entry.value(), "/progress"_json_pointer);
177 anime.status = StringToAnimeWatchingMap[JSON::GetString(entry.value(), "status")]; 177 anime.status = StringToAnimeWatchingMap[JSON::GetString(entry.value(), "/status"_json_pointer)];
178 anime.notes = StringUtils::Utf8ToWstr(JSON::GetString(entry.value(), "notes")); 178 anime.notes = JSON::GetString(entry.value(), "/notes"_json_pointer);
179 179
180 anime.started.SetYear(JSON::GetInt(entry.value()["startedAt"], "year")); 180 anime.started.SetYear(JSON::GetInt(entry.value(), "/startedAt/year"_json_pointer));
181 anime.started.SetMonth(JSON::GetInt(entry.value()["startedAt"], "month")); 181 anime.started.SetMonth(JSON::GetInt(entry.value(), "/startedAt/month"_json_pointer));
182 anime.started.SetDay(JSON::GetInt(entry.value()["startedAt"], "day")); 182 anime.started.SetDay(JSON::GetInt(entry.value(), "/startedAt/day"_json_pointer));
183 183
184 anime.completed.SetYear(JSON::GetInt(entry.value()["completedAt"], "year")); 184 anime.completed.SetYear(JSON::GetInt(entry.value(), "/completedAt/year"_json_pointer));
185 anime.completed.SetMonth(JSON::GetInt(entry.value()["completedAt"], "month")); 185 anime.completed.SetMonth(JSON::GetInt(entry.value(), "/completedAt/month"_json_pointer));
186 anime.completed.SetDay(JSON::GetInt(entry.value()["completedAt"], "day")); 186 anime.completed.SetDay(JSON::GetInt(entry.value(), "/completedAt/day"_json_pointer));
187 187
188 anime.updated = JSON::GetInt(entry.value(), "updatedAt"); 188 anime.updated = JSON::GetInt(entry.value(), "/updatedAt"_json_pointer);
189 189
190 anime.title.native = StringUtils::Utf8ToWstr(JSON::GetString(entry.value()["media"]["title"], "native")); 190 anime.title.native = JSON::GetString(entry.value(), "/media/title/native"_json_pointer);
191 anime.title.english = StringUtils::Utf8ToWstr(JSON::GetString(entry.value()["media"]["title"], "english")); 191 anime.title.english = JSON::GetString(entry.value(), "/media/title/english"_json_pointer);
192 anime.title.romaji = StringUtils::Utf8ToWstr(JSON::GetString(entry.value()["media"]["title"], "romaji")); 192 anime.title.romaji = JSON::GetString(entry.value(), "/media/title/romaji"_json_pointer);
193 193 /* fallback to romaji if english is not available
194 anime.id = JSON::GetInt(entry.value()["media"], "id"); 194 note that this takes up more space in memory and is stinky */
195 anime.episodes = JSON::GetInt(entry.value()["media"], "episodes"); 195 if (anime.title.english.empty())
196 anime.type = StringToAnimeFormatMap[JSON::GetString(entry.value()["media"], "format")]; 196 anime.title.english = anime.title.romaji;
197 197
198 anime.airing = StringToAnimeAiringMap[JSON::GetString(entry.value()["media"], "status")]; 198 anime.id = JSON::GetInt(entry.value(), "/media/id"_json_pointer);
199 199 anime.episodes = JSON::GetInt(entry.value(), "/media/episodes"_json_pointer);
200 anime.air_date.SetYear(JSON::GetInt(entry.value()["media"]["startDate"], "year")); 200 anime.type = StringToAnimeFormatMap[JSON::GetString(entry.value()["media"], "/media/format"_json_pointer)];
201 anime.air_date.SetMonth(JSON::GetInt(entry.value()["media"]["startDate"], "month")); 201
202 anime.air_date.SetDay(JSON::GetInt(entry.value()["media"]["startDate"], "day")); 202 anime.airing = StringToAnimeAiringMap[JSON::GetString(entry.value()["media"], "/media/status"_json_pointer)];
203 203
204 anime.audience_score = JSON::GetInt(entry.value()["media"], "averageScore"); 204 anime.air_date.SetYear(JSON::GetInt(entry.value(), "/media/startDate/year"_json_pointer));
205 anime.season = StringToAnimeSeasonMap[JSON::GetString(entry.value()["media"], "season")]; 205 anime.air_date.SetMonth(JSON::GetInt(entry.value(), "/media/startDate/month"_json_pointer));
206 anime.duration = JSON::GetInt(entry.value()["media"], "duration"); 206 anime.air_date.SetDay(JSON::GetInt(entry.value(), "/media/startDate/day"_json_pointer));
207 anime.synopsis = StringUtils::TextifySynopsis(StringUtils::Utf8ToWstr(JSON::GetString(entry.value()["media"], "duration"))); 207
208 anime.audience_score = JSON::GetInt(entry.value(), "/media/averageScore"_json_pointer);
209 anime.season = StringToAnimeSeasonMap[JSON::GetString(entry.value(), "/media/season"_json_pointer)];
210 anime.duration = JSON::GetInt(entry.value(), "/media/duration"_json_pointer);
211 anime.synopsis = StringUtils::TextifySynopsis(JSON::GetString(entry.value(), "/media/description"_json_pointer));
208 212
209 if (entry.value()["media"]["genres"].is_array()) 213 if (entry.value()["media"]["genres"].is_array())
210 anime.genres = entry.value()["media"]["genres"].get<std::vector<std::string>>(); 214 anime.genres = entry.value()["media"]["genres"].get<std::vector<std::string>>();
211 anime_list.Add(anime); 215 anime_list.Add(anime);
212 } 216 }
215 return 1; 219 return 1;
216 #undef QUERY 220 #undef QUERY
217 } 221 }
218 222
219 int AniList::Authorize() { 223 int AniList::Authorize() {
220 if (session.config.anilist.auth_token.empty()) { 224 /* Prompt for PIN */
221 /* Prompt for PIN */ 225 QDesktopServices::openUrl(QUrl("https://anilist.co/api/v2/oauth/authorize?client_id=" CLIENT_ID "&response_type=token"));
222 QDesktopServices::openUrl(QUrl("https://anilist.co/api/v2/oauth/authorize?client_id=" CLIENT_ID "&response_type=token")); 226 bool ok;
223 bool ok; 227 QString token = QInputDialog::getText(0, "Credentials needed!", "Please enter the code given to you after logging in to AniList:", QLineEdit::Normal, "", &ok);
224 QString token = QInputDialog::getText(0, "Credentials needed!", "Please enter the code given to you after logging in to AniList:", QLineEdit::Normal, "", &ok); 228 if (ok && !token.isEmpty()) {
225 if (ok && !token.isEmpty()) { 229 session.config.anilist.auth_token = token.toStdString();
226 session.config.anilist.auth_token = token.toStdString(); 230 } else { // fail
227 } else { // fail 231 return 0;
228 return 0;
229 }
230 } 232 }
231 return 1; 233 return 1;
232 } 234 }