diff src/services/anilist.cpp @ 10:4b198a111713

Update things actually compile now btw qttest wants to fuck over the model but that might be my fault so /shrug
author Paper <mrpapersonic@gmail.com>
date Sat, 16 Sep 2023 02:06:01 -0400
parents 5c0397762b53
children fc1bf97c528b
line wrap: on
line diff
--- a/src/services/anilist.cpp	Sun Sep 10 03:59:16 2023 -0400
+++ b/src/services/anilist.cpp	Sat Sep 16 02:06:01 2023 -0400
@@ -1,5 +1,6 @@
 #include "services/anilist.h"
 #include "core/anime.h"
+#include "core/anime_db.h"
 #include "core/config.h"
 #include "core/json.h"
 #include "core/session.h"
@@ -8,6 +9,7 @@
 #include <QInputDialog>
 #include <QLineEdit>
 #include <QMessageBox>
+#include <QUrl>
 #include <chrono>
 #include <curl/curl.h>
 #include <exception>
@@ -18,17 +20,17 @@
 
 class Account {
 	public:
-		std::string Username() const { return session.anilist.username; }
-		void SetUsername(std::string const& username) { session.anilist.username = username; }
+		std::string Username() const { return session.config.anilist.username; }
+		void SetUsername(std::string const& username) { session.config.anilist.username = username; }
 
-		int UserId() const { return session.anilist.user_id; }
-		void SetUserId(const int id) { session.anilist.user_id = id; }
+		int UserId() const { return session.config.anilist.user_id; }
+		void SetUserId(const int id) { session.config.anilist.user_id = id; }
 
-		std::string AuthToken() const { return session.anilist.auth_token; }
-		void SetAuthToken(std::string const& auth_token) { session.anilist.auth_token = auth_token; }
+		std::string AuthToken() const { return session.config.anilist.auth_token; }
+		void SetAuthToken(std::string const& auth_token) { session.config.anilist.auth_token = auth_token; }
 
 		bool Authenticated() const { return !AuthToken().empty(); }
-}
+};
 
 static Account account;
 
@@ -68,55 +70,55 @@
 	return "";
 }
 
-/* Maps to convert string forms to our internal enums */
+/* TODO: Move to Translate */
 
-std::map<std::string, enum AnimeWatchingStatus> StringToAnimeWatchingMap = {
-	{"CURRENT",	CURRENT  },
-	  {"PLANNING",  PLANNING },
-	  {"COMPLETED", COMPLETED},
-	{"DROPPED",	DROPPED  },
-	  {"PAUSED",	 PAUSED   },
-	  {"REPEATING", REPEATING}
+std::map<std::string, Anime::ListStatus> AniListStringToAnimeWatchingMap = {
+	{"CURRENT",	Anime::ListStatus::CURRENT  },
+	  {"PLANNING",  Anime::ListStatus::PLANNING },
+	  {"COMPLETED", Anime::ListStatus::COMPLETED},
+	{"DROPPED",	Anime::ListStatus::DROPPED  },
+	  {"PAUSED",	 Anime::ListStatus::PAUSED   },
+	  {"REPEATING", Anime::ListStatus::CURRENT}
 };
 
-std::map<enum AnimeWatchingStatus, std::string> AnimeWatchingToStringMap = {
-	{CURRENT,	  "CURRENT"  },
-	  {PLANNING,	 "PLANNING" },
-	  {COMPLETED, "COMPLETED"},
-	{DROPPED,	  "DROPPED"  },
-	  {PAUSED,	   "PAUSED"   },
-	  {REPEATING, "REPEATING"}
+std::map<Anime::ListStatus, std::string> AniListAnimeWatchingToStringMap = {
+	{Anime::ListStatus::CURRENT,	  "CURRENT"  },
+	  {Anime::ListStatus::PLANNING,	 "PLANNING" },
+	  {Anime::ListStatus::COMPLETED, "COMPLETED"},
+	{Anime::ListStatus::DROPPED,	  "DROPPED"  },
+	  {Anime::ListStatus::PAUSED,	   "PAUSED"   }
 };
 
-std::map<std::string, enum AnimeAiringStatus> StringToAnimeAiringMap = {
-	{"FINISHED",		 FINISHED		 },
-	{"RELEASING",		  RELEASING	   },
-	{"NOT_YET_RELEASED", NOT_YET_RELEASED},
-	{"CANCELLED",		  CANCELLED	   },
-	{"HIATUS",		   HIATUS			 }
+std::map<std::string, Anime::SeriesStatus> AniListStringToAnimeAiringMap = {
+	{"FINISHED",		 Anime::SeriesStatus::FINISHED		 },
+	{"RELEASING",		  Anime::SeriesStatus::RELEASING	   },
+	{"NOT_YET_RELEASED", Anime::SeriesStatus::NOT_YET_RELEASED},
+	{"CANCELLED",		  Anime::SeriesStatus::CANCELLED	   },
+	{"HIATUS",		   Anime::SeriesStatus::HIATUS			 }
 };
 
-std::map<std::string, enum AnimeSeason> StringToAnimeSeasonMap = {
-	{"WINTER", WINTER},
-	{"SPRING", SPRING},
-	{"SUMMER", SUMMER},
-	{"FALL",	 FALL	 }
+std::map<std::string, Anime::SeriesSeason> AniListStringToAnimeSeasonMap = {
+	{"WINTER", Anime::SeriesSeason::WINTER},
+	{"SPRING", Anime::SeriesSeason::SPRING},
+	{"SUMMER", Anime::SeriesSeason::SUMMER},
+	{"FALL",	 Anime::SeriesSeason::FALL	 }
 };
 
-std::map<std::string, enum AnimeFormat> StringToAnimeFormatMap = {
-	{"TV",	   TV		 },
-	  {"TV_SHORT", TV_SHORT},
-	  {"MOVIE",	MOVIE	 },
-	{"SPECIAL",	SPECIAL },
-	  {"OVA",	  OVA	 },
-	{"ONA",		ONA	   },
-	  {"MUSIC",	MUSIC	 },
-	  {"MANGA",	MANGA	 },
-	{"NOVEL",	  NOVEL   },
-	  {"ONE_SHOT", ONE_SHOT}
+std::map<std::string, enum Anime::SeriesFormat> AniListStringToAnimeFormatMap = {
+	{"TV",	   Anime::SeriesFormat::TV		 },
+	  {"TV_SHORT", Anime::SeriesFormat::TV_SHORT},
+	  {"MOVIE",	Anime::SeriesFormat::MOVIE	 },
+	{"SPECIAL",	Anime::SeriesFormat::SPECIAL },
+	  {"OVA",	  Anime::SeriesFormat::OVA	 },
+	{"ONA",		Anime::SeriesFormat::ONA	   },
+	  {"MUSIC",	Anime::SeriesFormat::MUSIC	 },
+	  {"MANGA",	Anime::SeriesFormat::MANGA	 },
+	{"NOVEL",	  Anime::SeriesFormat::NOVEL   },
+	  {"ONE_SHOT", Anime::SeriesFormat::ONE_SHOT}
 };
 
-void ParseDate(const nlohmann::json& json, Date& date) {
+Date ParseDate(const nlohmann::json& json) {
+	Date date;
 	if (json.contains("/year"_json_pointer) && json["/year"_json_pointer].is_number())
 		date.SetYear(JSON::GetInt(json, "/year"_json_pointer));
 	else
@@ -131,6 +133,7 @@
 		date.SetDay(JSON::GetInt(json, "/day"_json_pointer));
 	else
 		date.VoidDay();
+	return date;
 }
 
 void ParseTitle(const nlohmann::json& json, Anime::Anime& anime) {
@@ -151,43 +154,50 @@
 	anime.SetEpisodes(JSON::GetInt(json, "/episodes"_json_pointer));
 	anime.SetFormat(AniListStringToAnimeFormatMap[JSON::GetString(json, "/format"_json_pointer)]);
 
-	anime.SetListStatus(AniListStringToAnimeAiringMap[JSON::GetString(json, "/status"_json_pointer)]);
+	anime.SetAiringStatus(AniListStringToAnimeAiringMap[JSON::GetString(json, "/status"_json_pointer)]);
 
-	ParseDate(json["/startDate"_json_pointer], anime.air_date);
+	anime.SetAirDate(ParseDate(json["/startDate"_json_pointer]));
 
 	anime.SetAudienceScore(JSON::GetInt(json, "/averageScore"_json_pointer));
 	anime.SetSeason(AniListStringToAnimeSeasonMap[JSON::GetString(json, "/season"_json_pointer)]);
 	anime.SetDuration(JSON::GetInt(json, "/duration"_json_pointer));
-	anime.SetSynopsis(StringUtils::TextifySynopsis(JSON::GetString(json, "/description"_json_pointer)));
+	anime.SetSynopsis(Strings::TextifySynopsis(JSON::GetString(json, "/description"_json_pointer)));
 
 	if (json.contains("/genres"_json_pointer) && json["/genres"_json_pointer].is_array())
 		anime.SetGenres(json["/genres"_json_pointer].get<std::vector<std::string>>());
 	if (json.contains("/synonyms"_json_pointer) && json["/synonyms"_json_pointer].is_array())
-		anime.SetSynonyms(json["/synonyms"_json_pointer].get<std::vector<std::string>>());
-	return 1;
+		anime.SetTitleSynonyms(json["/synonyms"_json_pointer].get<std::vector<std::string>>());
+	return id;
 }
 
-int ParseListItem(const nlohmann::json& json, Anime::Anime& anime) {
-	anime.SetScore(JSON::GetInt(entry.value(), "/score"_json_pointer));
-	anime.SetProgress(JSON::GetInt(entry.value(), "/progress"_json_pointer));
-	anime.SetStatus(AniListStringToAnimeWatchingMap[JSON::GetString(entry.value(), "/status"_json_pointer)]);
-	anime.SetNotes(JSON::GetString(entry.value(), "/notes"_json_pointer));
+int ParseListItem(const nlohmann::json& json) {
+	int id = ParseMediaJson(json["media"]);
+
+	Anime::Anime& anime = Anime::db.items[id];
+
+	anime.AddToUserList();
 
-	ParseDate(json["/startedAt"_json_pointer], anime.started);
-	ParseDate(json["/completedAt"_json_pointer], anime.completed);
+	anime.SetUserScore(JSON::GetInt(json, "/score"_json_pointer));
+	anime.SetUserProgress(JSON::GetInt(json, "/progress"_json_pointer));
+	anime.SetUserStatus(AniListStringToAnimeWatchingMap[JSON::GetString(json, "/status"_json_pointer)]);
+	anime.SetUserNotes(JSON::GetString(json, "/notes"_json_pointer));
 
-	anime.SetUpdated(JSON::GetInt(entry.value(), "/updatedAt"_json_pointer));
+	anime.SetUserDateStarted(ParseDate(json["/startedAt"_json_pointer]));
+	anime.SetUserDateCompleted(ParseDate(json["/completedAt"_json_pointer]));
 
-	return ParseMediaJson(json["media"], anime);
+	anime.SetUserTimeUpdated(JSON::GetInt(json, "/updatedAt"_json_pointer));
+
+	return id;
 }
 
 int ParseList(const nlohmann::json& json) {
 	for (const auto& entry : json["entries"].items()) {
 		ParseListItem(entry.value());
 	}
+	return 1;
 }
 
-int GetAnimeList(int id) {
+int GetAnimeList() {
 	/* NOTE: these should be in the qrc file */
 	const std::string query = "query ($id: Int) {\n"
 							  "  MediaListCollection (userId: $id, type: ANIME) {\n"
@@ -196,6 +206,7 @@
 							  "      entries {\n"
 							  "        score\n"
 							  "        notes\n"
+							  "        status\n"
 							  "        progress\n"
 							  "        startedAt {\n"
 							  "          year\n"
@@ -238,7 +249,7 @@
 	nlohmann::json json = {
 		{"query", query},
 		{"variables", {
-			{"id", id}
+			{"id", account.UserId()}
 		}}
 	};
 	// clang-format on
@@ -248,13 +259,12 @@
 	/* TODO: make sure that we actually need the wstring converter and see
 	   if we can just get wide strings back from nlohmann::json */
 	for (const auto& list : res["data"]["MediaListCollection"]["lists"].items()) {
-
-		ParseList(list.entry());
+		ParseList(list.value());
 	}
 	return 1;
 }
 
-int UpdateAnimeEntry(const Anime& anime) {
+int UpdateAnimeEntry(const Anime::Anime& anime) {
 	/**
 	 * possible values:
 	 * 
@@ -285,11 +295,11 @@
 	nlohmann::json json = {
 		{"query", query},
 		{"variables", {
-			{"media_id", anime.id},
-			{"progress", anime.progress},
-			{"status",   AnimeWatchingToStringMap[anime.status]},
-			{"score",    anime.score},
-			{"notes",    anime.notes}
+			{"media_id", anime.GetId()},
+			{"progress", anime.GetUserProgress()},
+			{"status",   AniListAnimeWatchingToStringMap[anime.GetUserStatus()]},
+			{"score",    anime.GetUserScore()},
+			{"notes",    anime.GetUserNotes()}
 		}}
 	};
 	// clang-format on
@@ -300,7 +310,7 @@
 int ParseUser(const nlohmann::json& json) {
 	account.SetUsername(JSON::GetString(json, "/name"_json_pointer));
 	account.SetUserId(JSON::GetInt(json, "/id"_json_pointer));
-	account.SetAuthenticated(true);
+	return account.UserId();
 }
 
 int AuthorizeUser() {
@@ -314,7 +324,6 @@
 	if (ok && !token.isEmpty())
 		account.SetAuthToken(token.toStdString());
 	else { // fail
-		account.SetAuthenticated(false);
 		return 0;
 	}
 	const std::string query = "query {\n"
@@ -330,7 +339,7 @@
 		{"query", query}
 	};
 	auto ret = nlohmann::json::parse(SendRequest(json.dump()));
-	ParseUser(json["Viewer"]) account.SetAuthenticated(true);
+	ParseUser(json["Viewer"]);
 	return 1;
 }