diff src/services/anilist.cc @ 317:b1f4d1867ab1

services: VERY initial Kitsu support it only supports user authentication for now, but it's definitely a start.
author Paper <paper@paper.us.eu.org>
date Wed, 12 Jun 2024 04:07:10 -0400
parents 34347fd2a2de
children d928ec7b6a0d
line wrap: on
line diff
--- a/src/services/anilist.cc	Tue Jun 11 15:11:09 2024 -0400
+++ b/src/services/anilist.cc	Wed Jun 12 04:07:10 2024 -0400
@@ -23,6 +23,9 @@
 
 #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 {
@@ -54,19 +57,17 @@
 	"synonyms\n" \
 	"description(asHtml: false)\n"
 
-class Account {
-public:
+/* FIXME: why is this here */
+
+static struct {
 	int UserId() const { return session.config.auth.anilist.user_id; }
 	void SetUserId(const int id) { session.config.auth.anilist.user_id = id; }
 
 	std::string AuthToken() const { return session.config.auth.anilist.auth_token; }
 	void SetAuthToken(const std::string& auth_token) { session.config.auth.anilist.auth_token = auth_token; }
 
-	bool Authenticated() const { return !AuthToken().empty(); }
-	bool IsValid() const { return UserId() && Authenticated(); }
-};
-
-static Account account;
+	bool IsValid() const { return UserId() && !AuthToken().empty(); }
+} account;
 
 static std::string SendRequest(const std::string& data) {
 	std::vector<std::string> headers = {"Authorization: Bearer " + account.AuthToken(), "Accept: application/json",
@@ -89,7 +90,7 @@
 
 	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: "
+			std::cerr << "AniList: Received an error in response: "
 			          << JSON::GetString<std::string>(error, "/message"_json_pointer, "") << std::endl;
 
 		session.SetStatusBar("AniList: Received an error in response!");
@@ -149,14 +150,19 @@
 }
 
 static int ParseMediaJson(const nlohmann::json& json) {
-	int id = JSON::GetNumber(json, "/id"_json_pointer);
-	if (!id)
+	if (!json.contains("/id"_json_pointer) || !json["/id"_json_pointer].is_number())
 		return 0;
 
+	std::string service_id = Strings::ToUtf8String(json["/id"_json_pointer].get<int>());
+
+	int id = Anime::db.LookupServiceIdOrUnused(Anime::Service::AniList, service_id);
+
 	Anime::Anime& anime = Anime::db.items[id];
 	anime.SetId(id);
-	anime.SetServiceId(Anime::Service::AniList, Strings::ToUtf8String(id));
-	anime.SetServiceId(Anime::Service::MyAnimeList, Strings::ToUtf8String(JSON::GetNumber(json, "/id_mal"_json_pointer)));
+	anime.SetServiceId(Anime::Service::AniList, service_id);
+
+	if (json.contains("/id_mal"_json_pointer))
+		anime.SetServiceId(Anime::Service::MyAnimeList, json["/id_mal"_json_pointer].get<std::string>());
 
 	ParseTitle(json.at("/title"_json_pointer), anime);
 
@@ -185,7 +191,9 @@
 }
 
 static int ParseListItem(const nlohmann::json& json) {
-	int id = ParseMediaJson(json["media"]);
+	int id = ParseMediaJson(json);
+	if (!id)
+		return 0;
 
 	Anime::Anime& anime = Anime::db.items[id];
 
@@ -205,15 +213,15 @@
 }
 
 static int ParseList(const nlohmann::json& json) {
-	for (const auto& entry : json["entries"].items()) {
+	for (const auto& entry : json["entries"].items())
 		ParseListItem(entry.value());
-	}
+
 	return 1;
 }
 
 int GetAnimeList() {
 	if (!account.IsValid()) {
-		session.SetStatusBar("AniList: Account isn't valid!");
+		session.SetStatusBar("AniList: Account isn't valid! (unauthorized?)");
 		return 0;
 	}