diff src/services/anilist.cc @ 325:78929794e7d8

pages/seasons: run seasons search in a separate thread
author Paper <paper@paper.us.eu.org>
date Thu, 13 Jun 2024 00:36:41 -0400
parents 5d3c9b31aa6e
children b5d6c27c308f
line wrap: on
line diff
--- a/src/services/anilist.cc	Wed Jun 12 23:03:22 2024 -0400
+++ b/src/services/anilist.cc	Thu Jun 13 00:36:41 2024 -0400
@@ -23,15 +23,12 @@
 
 #include <iostream>
 
-/* This file really sucks because it was made when I was first
- * really "learning" C++ */
-
-using namespace nlohmann::literals::json_literals;
-
 namespace Services {
 namespace AniList {
 
 static constexpr std::string_view CLIENT_ID = "13706";
+
+/* This is used in multiple queries, so just put it here I guess. */
 #define MEDIA_FIELDS \
 	"coverImage {\n" \
 	"  large\n" \
@@ -198,10 +195,9 @@
 	if (json.contains("/startDate"_json_pointer) && json["/startDate"_json_pointer].is_object())
 		anime.SetStartedDate(Date(json["/startDate"_json_pointer]));
 
-	if (json.contains("/endDate"_json_pointer) && json["/endDate"_json_pointer].is_object())
-		anime.SetCompletedDate(Date(json["/endDate"_json_pointer]));
-	else
-		anime.SetCompletedDate(anime.GetStartedDate());
+	anime.SetCompletedDate(json.contains("/endDate"_json_pointer) && json["/endDate"_json_pointer].is_object()
+		? Date(json["/endDate"_json_pointer])
+		: anime.GetStartedDate());
 
 	anime.SetPosterUrl(JSON::GetString<std::string>(json, "/coverImage/large"_json_pointer, ""));
 
@@ -265,34 +261,35 @@
 int GetAnimeList() {
 	auto& auth = session.config.auth.anilist;
 
-	/* NOTE: these really ought to be in the qrc file */
-	constexpr std::string_view query = "query ($id: Int) {\n"
-									   "  MediaListCollection (userId: $id, type: ANIME) {\n"
-									   "    lists {\n"
-									   "      name\n"
-									   "      entries {\n"
-									   "        score\n"
-									   "        notes\n"
-									   "        status\n"
-									   "        progress\n"
-									   "        startedAt {\n"
-									   "          year\n"
-									   "          month\n"
-									   "          day\n"
-									   "        }\n"
-									   "        completedAt {\n"
-									   "          year\n"
-									   "          month\n"
-									   "          day\n"
-									   "        }\n"
-									   "        updatedAt\n"
-									   "        media {\n"
-									   MEDIA_FIELDS
-									   "        }\n"
-									   "      }\n"
-									   "    }\n"
-									   "  }\n"
-									   "}\n";
+	static constexpr std::string_view query =
+		"query ($id: Int) {\n"
+		"  MediaListCollection (userId: $id, type: ANIME) {\n"
+		"    lists {\n"
+		"      name\n"
+		"      entries {\n"
+		"        score\n"
+		"        notes\n"
+		"        status\n"
+		"        progress\n"
+		"        startedAt {\n"
+		"          year\n"
+		"          month\n"
+		"          day\n"
+		"        }\n"
+		"        completedAt {\n"
+		"          year\n"
+		"          month\n"
+		"          day\n"
+		"        }\n"
+		"        updatedAt\n"
+		"        media {\n"
+		MEDIA_FIELDS
+		"        }\n"
+		"      }\n"
+		"    }\n"
+		"  }\n"
+		"}\n";
+
 	// clang-format off
 	nlohmann::json request = {
 		{"query", query},
@@ -326,13 +323,14 @@
 
 /* return is a vector of anime ids */
 std::vector<int> Search(const std::string& search) {
-	constexpr std::string_view query = "query ($search: String) {\n"
-									   "  Page (page: 1, perPage: 50) {\n"
-									   "    media (search: $search, type: ANIME) {\n"
-									   MEDIA_FIELDS
-									   "    }\n"
-									   "  }\n"
-									   "}\n";
+	static constexpr std::string_view query =
+		"query ($search: String) {\n"
+		"  Page (page: 1, perPage: 50) {\n"
+		"    media (search: $search, type: ANIME) {\n"
+		MEDIA_FIELDS
+		"    }\n"
+		"  }\n"
+		"}\n";
 
 	// clang-format off
 	nlohmann::json json = {
@@ -359,74 +357,54 @@
 	return ret;
 }
 
-std::vector<int> GetSeason(Anime::SeriesSeason season, Date::Year year) {
-	constexpr std::string_view query = "query ($season: MediaSeason!, $season_year: Int!, $page: Int) {\n"
-									   "  Page(page: $page) {\n"
-									   "    media(season: $season, seasonYear: $season_year, type: ANIME, sort: START_DATE) {\n"
-									   MEDIA_FIELDS
-									   "    }\n"
-									   "    pageInfo {\n"
-									   "      total\n"
-									   "      perPage\n"
-									   "      currentPage\n"
-									   "      lastPage\n"
-									   "      hasNextPage\n"
-									   "    }\n"
-									   "  }\n"
-									   "}\n";
-	std::vector<int> ret;
+bool GetSeason(Anime::SeriesSeason season, Date::Year year) {
+	static constexpr std::string_view query =
+		"query ($season: MediaSeason!, $season_year: Int!, $page: Int) {\n"
+		"  Page(page: $page) {\n"
+		"    media(season: $season, seasonYear: $season_year, type: ANIME, sort: START_DATE) {\n"
+		MEDIA_FIELDS
+		"    }\n"
+		"    pageInfo {\n"
+		"      total\n"
+		"      perPage\n"
+		"      currentPage\n"
+		"      lastPage\n"
+		"      hasNextPage\n"
+		"    }\n"
+		"  }\n"
+		"}\n";
 
 	int page = 0;
 	bool has_next_page = true;
+
 	while (has_next_page) {
 		nlohmann::json json = {
 			{"query", query},
 			{"variables", {
 				{"season", Translate::AniList::ToString(season)},
 				{"season_year", Strings::ToUtf8String(year)},
-				{"page", page}
-			}}
+				{"page", page},
+			}},
 		};
 
 		const std::optional<nlohmann::json> res = SendJSONRequest(json);
 		if (!res)
-			return {};
+			return false;
 
 		const nlohmann::json& result = res.value();
 
-		ret.reserve(ret.capacity() + result["data"]["Page"]["media"].size());
-
-		for (const auto& media : result["data"]["Page"]["media"].items())
-			ret.push_back(ParseMediaJson(media.value()));
+		for (const auto& media : result["/data/Page/media"_json_pointer].items())
+			ParseMediaJson(media.value());
 
 		has_next_page = JSON::GetBoolean(result, "/data/Page/pageInfo/hasNextPage"_json_pointer, false);
 		if (has_next_page)
 			page++;
 	}
 
-	return ret;
+	return true;
 }
 
 int UpdateAnimeEntry(int id) {
-	/**
-	 * possible values:
-	 *
-	 * int mediaId,
-	 * MediaListStatus status,
-	 * float score,
-	 * int scoreRaw,
-	 * int progress,
-	 * int progressVolumes, // manga-specific.
-	 * int repeat, // rewatch
-	 * int priority,
-	 * bool private,
-	 * string notes,
-	 * bool hiddenFromStatusLists,
-	 * string[] customLists,
-	 * float[] advancedScores,
-	 * Date startedAt,
-	 * Date completedAt
-	 **/
 	Anime::Anime& anime = Anime::db.items[id];
 	if (!anime.IsInUserList())
 		return 0;
@@ -435,13 +413,11 @@
 	if (!service_id)
 		return 0;
 
-	session.SetStatusBar("AniList: Updating anime entry...");
-
-	constexpr std::string_view query =
-		"mutation ($media_id: Int, $progress: Int, $status: MediaListStatus, $score: Int, $notes: String, $start: "
-		"FuzzyDateInput, $comp: FuzzyDateInput, $repeat: Int) {\n"
-		"  SaveMediaListEntry (mediaId: $media_id, progress: $progress, status: $status, scoreRaw: $score, notes: "
-		"$notes, startedAt: $start, completedAt: $comp, repeat: $repeat) {\n"
+	static constexpr std::string_view query =
+		"mutation ($media_id: Int, $progress: Int, $status: MediaListStatus, $score: Int, $notes: String,"
+		"     $start: FuzzyDateInput, $comp: FuzzyDateInput, $repeat: Int) {\n"
+		"  SaveMediaListEntry (mediaId: $media_id, progress: $progress, status: $status, scoreRaw: $score,"
+		"        notes: $notes, startedAt: $start, completedAt: $comp, repeat: $repeat) {\n"
 		"    id\n"
 		"  }\n"
 		"}\n";
@@ -497,11 +473,13 @@
 
 	session.SetStatusBar("AniList: Requesting user ID...");
 
-	constexpr std::string_view query = "query {\n"
-									   "  Viewer {\n"
-									   "    id\n"
-									   "  }\n"
-									   "}\n";
+	static constexpr std::string_view query =
+		"query {\n"
+		"  Viewer {\n"
+		"    id\n"
+		"  }\n"
+		"}\n";
+
 	nlohmann::json json = {
 		{"query", query}
 	};