diff 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
line wrap: on
line diff
--- a/src/anilist.cpp	Sat Aug 12 13:10:34 2023 -0400
+++ b/src/anilist.cpp	Wed Aug 16 00:49:17 2023 -0400
@@ -65,7 +65,7 @@
 		}}
 	};
 	auto ret = nlohmann::json::parse(SendRequest(json.dump()));
-	return ret["data"]["User"]["id"].get<int>();
+	return JSON::GetInt(ret, "/data/User/id"_json_pointer);
 #undef QUERY
 }
 
@@ -169,42 +169,46 @@
 	for (const auto& list : res["data"]["MediaListCollection"]["lists"].items()) {
 		/* why are the .key() values strings?? */
 		AnimeList anime_list;
-		anime_list.name = StringUtils::Utf8ToWstr(JSON::GetString(list.value(), "name"));
+		anime_list.name = JSON::GetString(list.value(), "/name"_json_pointer);
 		for (const auto& entry : list.value()["entries"].items()) {
 			Anime anime;
-			anime.score = JSON::GetInt(entry.value(), "score");
-			anime.progress = JSON::GetInt(entry.value(), "progress");
-			anime.status = StringToAnimeWatchingMap[JSON::GetString(entry.value(), "status")];
-			anime.notes = StringUtils::Utf8ToWstr(JSON::GetString(entry.value(), "notes"));
+			anime.score = JSON::GetInt(entry.value(), "/score"_json_pointer);
+			anime.progress = JSON::GetInt(entry.value(), "/progress"_json_pointer);
+			anime.status = StringToAnimeWatchingMap[JSON::GetString(entry.value(), "/status"_json_pointer)];
+			anime.notes = JSON::GetString(entry.value(), "/notes"_json_pointer);
 
-			anime.started.SetYear(JSON::GetInt(entry.value()["startedAt"], "year"));
-			anime.started.SetMonth(JSON::GetInt(entry.value()["startedAt"], "month"));
-			anime.started.SetDay(JSON::GetInt(entry.value()["startedAt"], "day"));
+			anime.started.SetYear(JSON::GetInt(entry.value(), "/startedAt/year"_json_pointer));
+			anime.started.SetMonth(JSON::GetInt(entry.value(), "/startedAt/month"_json_pointer));
+			anime.started.SetDay(JSON::GetInt(entry.value(), "/startedAt/day"_json_pointer));
 
-			anime.completed.SetYear(JSON::GetInt(entry.value()["completedAt"], "year"));
-			anime.completed.SetMonth(JSON::GetInt(entry.value()["completedAt"], "month"));
-			anime.completed.SetDay(JSON::GetInt(entry.value()["completedAt"], "day"));
+			anime.completed.SetYear(JSON::GetInt(entry.value(), "/completedAt/year"_json_pointer));
+			anime.completed.SetMonth(JSON::GetInt(entry.value(), "/completedAt/month"_json_pointer));
+			anime.completed.SetDay(JSON::GetInt(entry.value(), "/completedAt/day"_json_pointer));
 
-			anime.updated = JSON::GetInt(entry.value(), "updatedAt");
+			anime.updated = JSON::GetInt(entry.value(), "/updatedAt"_json_pointer);
 
-			anime.title.native  = StringUtils::Utf8ToWstr(JSON::GetString(entry.value()["media"]["title"], "native"));
-			anime.title.english = StringUtils::Utf8ToWstr(JSON::GetString(entry.value()["media"]["title"], "english"));
-			anime.title.romaji  = StringUtils::Utf8ToWstr(JSON::GetString(entry.value()["media"]["title"], "romaji"));
-
-			anime.id = JSON::GetInt(entry.value()["media"], "id");
-			anime.episodes = JSON::GetInt(entry.value()["media"], "episodes");
-			anime.type = StringToAnimeFormatMap[JSON::GetString(entry.value()["media"], "format")];
+			anime.title.native  = JSON::GetString(entry.value(), "/media/title/native"_json_pointer);
+			anime.title.english = JSON::GetString(entry.value(), "/media/title/english"_json_pointer);
+			anime.title.romaji  = JSON::GetString(entry.value(), "/media/title/romaji"_json_pointer);
+			/* fallback to romaji if english is not available
+			   note that this takes up more space in memory and is stinky */
+			if (anime.title.english.empty())
+				anime.title.english = anime.title.romaji;
 
-			anime.airing = StringToAnimeAiringMap[JSON::GetString(entry.value()["media"], "status")];
+			anime.id = JSON::GetInt(entry.value(), "/media/id"_json_pointer);
+			anime.episodes = JSON::GetInt(entry.value(), "/media/episodes"_json_pointer);
+			anime.type = StringToAnimeFormatMap[JSON::GetString(entry.value()["media"], "/media/format"_json_pointer)];
+
+			anime.airing = StringToAnimeAiringMap[JSON::GetString(entry.value()["media"], "/media/status"_json_pointer)];
 
-			anime.air_date.SetYear(JSON::GetInt(entry.value()["media"]["startDate"], "year"));
-			anime.air_date.SetMonth(JSON::GetInt(entry.value()["media"]["startDate"], "month"));
-			anime.air_date.SetDay(JSON::GetInt(entry.value()["media"]["startDate"], "day"));
+			anime.air_date.SetYear(JSON::GetInt(entry.value(), "/media/startDate/year"_json_pointer));
+			anime.air_date.SetMonth(JSON::GetInt(entry.value(), "/media/startDate/month"_json_pointer));
+			anime.air_date.SetDay(JSON::GetInt(entry.value(), "/media/startDate/day"_json_pointer));
 
-			anime.audience_score = JSON::GetInt(entry.value()["media"], "averageScore");
-			anime.season = StringToAnimeSeasonMap[JSON::GetString(entry.value()["media"], "season")];
-			anime.duration = JSON::GetInt(entry.value()["media"], "duration");
-			anime.synopsis = StringUtils::TextifySynopsis(StringUtils::Utf8ToWstr(JSON::GetString(entry.value()["media"], "duration")));
+			anime.audience_score = JSON::GetInt(entry.value(), "/media/averageScore"_json_pointer);
+			anime.season = StringToAnimeSeasonMap[JSON::GetString(entry.value(), "/media/season"_json_pointer)];
+			anime.duration = JSON::GetInt(entry.value(), "/media/duration"_json_pointer);
+			anime.synopsis = StringUtils::TextifySynopsis(JSON::GetString(entry.value(), "/media/description"_json_pointer));
 
 			if (entry.value()["media"]["genres"].is_array())
 				anime.genres = entry.value()["media"]["genres"].get<std::vector<std::string>>();
@@ -217,16 +221,14 @@
 }
 
 int AniList::Authorize() {
-	if (session.config.anilist.auth_token.empty()) {
-		/* Prompt for PIN */
-		QDesktopServices::openUrl(QUrl("https://anilist.co/api/v2/oauth/authorize?client_id=" CLIENT_ID "&response_type=token"));
-		bool ok;
-		QString token = QInputDialog::getText(0, "Credentials needed!", "Please enter the code given to you after logging in to AniList:", QLineEdit::Normal, "", &ok);
-		if (ok && !token.isEmpty()) {
-			session.config.anilist.auth_token = token.toStdString();
-		} else { // fail
-			return 0;
-		}
+	/* Prompt for PIN */
+	QDesktopServices::openUrl(QUrl("https://anilist.co/api/v2/oauth/authorize?client_id=" CLIENT_ID "&response_type=token"));
+	bool ok;
+	QString token = QInputDialog::getText(0, "Credentials needed!", "Please enter the code given to you after logging in to AniList:", QLineEdit::Normal, "", &ok);
+	if (ok && !token.isEmpty()) {
+		session.config.anilist.auth_token = token.toStdString();
+	} else { // fail
+		return 0;
 	}
 	return 1;
 }