diff src/services/anilist.cc @ 315:34347fd2a2de

session: allow printing status messages ...!
author Paper <paper@paper.us.eu.org>
date Tue, 11 Jun 2024 14:16:40 -0400
parents 91ac90a34003
children b1f4d1867ab1
line wrap: on
line diff
--- a/src/services/anilist.cc	Tue Jun 11 13:29:45 2024 -0400
+++ b/src/services/anilist.cc	Tue Jun 11 14:16:40 2024 -0400
@@ -74,28 +74,29 @@
 	return Strings::ToUtf8String(HTTP::Request("https://graphql.anilist.co", headers, data, HTTP::Type::Post));
 }
 
-static nlohmann::json SendJSONRequest(const nlohmann::json& data) {
+static bool SendJSONRequest(const nlohmann::json& data, nlohmann::json& out) {
 	std::string request = SendRequest(data.dump());
 	if (request.empty()) {
-		std::cerr << "[AniList] JSON Request returned an empty result!" << std::endl;
-		return {};
+		session.SetStatusBar("AniList: JSON request returned an empty result!");
+		return false;
 	}
 
-	auto ret = nlohmann::json::parse(request, nullptr, false);
-	if (ret.is_discarded()) {
-		std::cerr << "[AniList] Failed to parse request JSON!" << std::endl;
-		return {};
+	out = nlohmann::json::parse(request, nullptr, false);
+	if (out.is_discarded()) {
+		session.SetStatusBar("AniList: Failed to parse request JSON!");
+		return false;
 	}
 
-	if (ret.contains("/errors"_json_pointer) && ret.at("/errors"_json_pointer).is_array()) {
-		for (const auto& error : ret.at("/errors"_json_pointer))
+	if (out.contains("/errors"_json_pointer) && out.at("/errors"_json_pointer).is_array()) {
+		for (const auto& error : out.at("/errors"_json_pointer))
 			std::cerr << "[AniList] Received an error in response: "
 			          << JSON::GetString<std::string>(error, "/message"_json_pointer, "") << std::endl;
 
-		return {};
+		session.SetStatusBar("AniList: Received an error in response!");
+		return false;
 	}
 
-	return ret;
+	return true;
 }
 
 static void ParseListStatus(std::string status, Anime::Anime& anime) {
@@ -212,10 +213,12 @@
 
 int GetAnimeList() {
 	if (!account.IsValid()) {
-		std::cerr << "AniList: Account isn't valid!" << std::endl;
+		session.SetStatusBar("AniList: Account isn't valid!");
 		return 0;
 	}
 
+	session.SetStatusBar("AniList: Retrieving anime list...");
+
 	/* 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"
@@ -253,11 +256,18 @@
 	};
 	// clang-format on
 
-	auto res = SendJSONRequest(json);
+	session.SetStatusBar("AniList: Parsing anime list...");
 
-	for (const auto& list : res["data"]["MediaListCollection"]["lists"].items())
+	nlohmann::json result;
+	const bool res = SendJSONRequest(json, result);
+	if (!res)
+		return 0;
+
+	for (const auto& list : result["data"]["MediaListCollection"]["lists"].items())
 		ParseList(list.value());
 
+	session.SetStatusBar("AniList: Retrieved anime list successfully!");
+
 	return 1;
 }
 
@@ -280,13 +290,16 @@
 	};
 	// clang-format on
 
-	auto res = SendJSONRequest(json);
+	nlohmann::json result;
+	const bool res = SendJSONRequest(json, result);
+	if (!res)
+		return {};
 
 	/* FIXME: error handling here */
 	std::vector<int> ret;
-	ret.reserve(res["data"]["Page"]["media"].size());
+	ret.reserve(result["/data/Page/media"_json_pointer].size());
 
-	for (const auto& media : res["data"]["Page"]["media"].items())
+	for (const auto& media : result["/data/Page/media"_json_pointer].items())
 		ret.push_back(ParseMediaJson(media.value()));
 
 	return ret;
@@ -321,13 +334,17 @@
 			}}
 		};
 
-		auto res = SendJSONRequest(json);
-		ret.reserve(ret.capacity() + res["data"]["Page"]["media"].size());
+		nlohmann::json result;
+		const bool res = SendJSONRequest(json, result);
+		if (!res)
+			return {};
 
-		for (const auto& media : res["data"]["Page"]["media"].items())
+		ret.reserve(ret.capacity() + result["data"]["Page"]["media"].size());
+
+		for (const auto& media : result["data"]["Page"]["media"].items())
 			ret.push_back(ParseMediaJson(media.value()));
 
-		has_next_page = JSON::GetBoolean(res, "/data/Page/pageInfo/hasNextPage"_json_pointer, false);
+		has_next_page = JSON::GetBoolean(result, "/data/Page/pageInfo/hasNextPage"_json_pointer, false);
 		if (has_next_page)
 			page++;
 	}
@@ -363,6 +380,8 @@
 	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"
@@ -387,9 +406,14 @@
 	};
 	// clang-format on
 
-	auto ret = SendJSONRequest(json);
+	nlohmann::json result;
+	const bool ret = SendJSONRequest(json, result);
+	if (!ret)
+		return 0;
 
-	return JSON::GetNumber(ret, "/data/SaveMediaListEntry/id"_json_pointer, 0);
+	session.SetStatusBar("AniList: Anime entry updated successfully!");
+
+	return JSON::GetNumber(result, "/data/SaveMediaListEntry/id"_json_pointer, 0);
 }
 
 static int ParseUser(const nlohmann::json& json) {
@@ -412,6 +436,8 @@
 
 	account.SetAuthToken(Strings::ToUtf8String(token));
 
+	session.SetStatusBar("AniList: Requesting user ID...");
+
 	constexpr std::string_view query = "query {\n"
 	                                   "  Viewer {\n"
 	                                   "    id\n"
@@ -425,7 +451,13 @@
 	    {"query", query}
     };
 
-	auto ret = SendJSONRequest(json);
+    /* SendJSONRequest handles status errors */
+    nlohmann::json result;
+	const bool ret = SendJSONRequest(json, result);
+	if (!ret)
+		return 0;
+
+	session.SetStatusBar("AniList: Successfully retrieved user data!");
 
 	ParseUser(ret["data"]["Viewer"]);
 	return true;