diff src/services/anilist.cc @ 304:2115488eb302

*: add very early season searcher
author Paper <paper@paper.us.eu.org>
date Mon, 13 May 2024 17:02:35 -0400
parents 9a88e1725fd2
children 91ac90a34003
line wrap: on
line diff
--- a/src/services/anilist.cc	Mon May 13 15:19:55 2024 -0400
+++ b/src/services/anilist.cc	Mon May 13 17:02:35 2024 -0400
@@ -1,6 +1,7 @@
 #include "services/anilist.h"
 #include "core/anime.h"
 #include "core/anime_db.h"
+#include "core/date.h"
 #include "core/config.h"
 #include "core/http.h"
 #include "core/json.h"
@@ -28,6 +29,30 @@
 namespace AniList {
 
 static constexpr std::string_view CLIENT_ID = "13706";
+#define MEDIA_FIELDS \
+	"coverImage {\n" \
+	"  large\n" \
+	"}\n" \
+	"id\n" \
+	"title {\n" \
+	"  romaji\n" \
+	"  english\n" \
+	"  native\n" \
+	"}\n" \
+	"format\n" \
+	"status\n" \
+	"averageScore\n" \
+	"season\n" \
+	"startDate {\n" \
+	"  year\n" \
+	"  month\n" \
+	"  day\n" \
+	"}\n" \
+	"genres\n" \
+	"episodes\n" \
+	"duration\n" \
+	"synonyms\n" \
+	"description(asHtml: false)\n"
 
 class Account {
 public:
@@ -215,29 +240,7 @@
 	                                   "        }\n"
 	                                   "        updatedAt\n"
 	                                   "        media {\n"
-	                                   "          coverImage {\n"
-	                                   "            large\n"
-	                                   "          }\n"
-	                                   "          id\n"
-	                                   "          title {\n"
-	                                   "            romaji\n"
-	                                   "            english\n"
-	                                   "            native\n"
-	                                   "          }\n"
-	                                   "          format\n"
-	                                   "          status\n"
-	                                   "          averageScore\n"
-	                                   "          season\n"
-	                                   "          startDate {\n"
-	                                   "            year\n"
-	                                   "            month\n"
-	                                   "            day\n"
-	                                   "          }\n"
-	                                   "          genres\n"
-	                                   "          episodes\n"
-	                                   "          duration\n"
-	                                   "          synonyms\n"
-	                                   "          description(asHtml: false)\n"
+	                                   MEDIA_FIELDS
 	                                   "        }\n"
 	                                   "      }\n"
 	                                   "    }\n"
@@ -254,9 +257,9 @@
 
 	auto res = SendJSONRequest(json);
 
-	for (const auto& list : res["data"]["MediaListCollection"]["lists"].items()) {
+	for (const auto& list : res["data"]["MediaListCollection"]["lists"].items())
 		ParseList(list.value());
-	}
+
 	return 1;
 }
 
@@ -265,30 +268,7 @@
 	constexpr std::string_view query = "query ($search: String) {\n"
 	                                   "  Page (page: 1, perPage: 50) {\n"
 	                                   "    media (search: $search, type: ANIME) {\n"
-	                                   "      coverImage {\n"
-	                                   "        large\n"
-	                                   "      }\n"
-	                                   "      id\n"
-	                                   "      id_mal\n"
-	                                   "      title {\n"
-	                                   "        romaji\n"
-	                                   "        english\n"
-	                                   "        native\n"
-	                                   "      }\n"
-	                                   "      format\n"
-	                                   "      status\n"
-	                                   "      averageScore\n"
-	                                   "      season\n"
-	                                   "      startDate {\n"
-	                                   "        year\n"
-	                                   "        month\n"
-	                                   "        day\n"
-	                                   "      }\n"
-	                                   "      genres\n"
-	                                   "      episodes\n"
-	                                   "      duration\n"
-	                                   "      synonyms\n"
-	                                   "      description(asHtml: false)\n"
+	                                   MEDIA_FIELDS
 	                                   "    }\n"
 	                                   "  }\n"
 	                                   "}\n";
@@ -304,6 +284,7 @@
 
 	auto res = SendJSONRequest(json);
 
+	/* FIXME: error handling here */
 	std::vector<int> ret;
 	ret.reserve(res["data"]["Page"]["media"].size());
 
@@ -313,6 +294,49 @@
 	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;
+
+	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}
+			}}
+		};
+
+		auto res = SendJSONRequest(json);
+		ret.reserve(ret.capacity() + res["data"]["Page"]["media"].size());
+
+		for (const auto& media : res["data"]["Page"]["media"].items())
+			ret.push_back(ParseMediaJson(media.value()));
+
+		has_next_page = JSON::GetBoolean(res, "/data/Page/pageInfo/hasNextPage"_json_pointer, false);
+		if (has_next_page)
+			page++;
+	}
+
+	return ret;
+}
+
 int UpdateAnimeEntry(int id) {
 	/**
 	 * possible values: