diff src/services/anilist.cc @ 258:862d0d8619f6

*: HUUUGE changes animia has been renamed to animone, so instead of thinking of a health condition, you think of a beautiful flower :) I've also edited some of the code for animone, but I have no idea if it even works or not because I don't have a mac or windows machine lying around. whoops! ... anyway, all of the changes divergent from Anisthesia are now licensed under BSD. it's possible that I could even rewrite most of the code to where I don't even have to keep the MIT license, but that's thinking too far into the future I've been slacking off on implementing the anime seasons page, mostly out of laziness. I think I'd have to create another db file specifically for the seasons anyway, this code is being pushed *primarily* because the hard drive it's on is failing! yay :)
author Paper <paper@paper.us.eu.org>
date Mon, 01 Apr 2024 02:43:44 -0400
parents c130f47f6f48
children dd211ff68b36
line wrap: on
line diff
--- a/src/services/anilist.cc	Sun Feb 18 16:02:14 2024 -0500
+++ b/src/services/anilist.cc	Mon Apr 01 02:43:44 2024 -0400
@@ -8,8 +8,8 @@
 #include "core/strings.h"
 #include "gui/translate/anilist.h"
 
+#include <QByteArray>
 #include <QDate>
-#include <QByteArray>
 #include <QDesktopServices>
 #include <QInputDialog>
 #include <QLineEdit>
@@ -29,22 +29,22 @@
 constexpr int CLIENT_ID = 13706;
 
 class Account {
-	public:
-		int UserId() const { return session.config.auth.anilist.user_id; }
-		void SetUserId(const int id) { session.config.auth.anilist.user_id = id; }
+public:
+	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(std::string const& auth_token) { session.config.auth.anilist.auth_token = auth_token; }
+	std::string AuthToken() const { return session.config.auth.anilist.auth_token; }
+	void SetAuthToken(std::string const& auth_token) { session.config.auth.anilist.auth_token = auth_token; }
 
-		bool Authenticated() const { return !AuthToken().empty(); }
-		bool IsValid() const { return UserId() && Authenticated(); }
+	bool Authenticated() const { return !AuthToken().empty(); }
+	bool IsValid() const { return UserId() && Authenticated(); }
 };
 
 static Account account;
 
 std::string SendRequest(std::string data) {
 	std::vector<std::string> headers = {"Authorization: Bearer " + account.AuthToken(), "Accept: application/json",
-										"Content-Type: application/json"};
+	                                    "Content-Type: application/json"};
 	return Strings::ToUtf8String(HTTP::Post("https://graphql.anilist.co", data, headers));
 }
 
@@ -63,7 +63,8 @@
 
 	if (ret.contains("/errors"_json_pointer) && ret.at("/errors"_json_pointer).is_array()) {
 		for (const auto& error : ret.at("/errors"_json_pointer))
-			std::cerr << "[AniList] Received an error in response: " << JSON::GetString<std::string>(error, "/message"_json_pointer, "") << std::endl;
+			std::cerr << "[AniList] Received an error in response: "
+			          << JSON::GetString<std::string>(error, "/message"_json_pointer, "") << std::endl;
 
 		return {};
 	}
@@ -73,12 +74,12 @@
 
 void ParseListStatus(std::string status, Anime::Anime& anime) {
 	static const std::unordered_map<std::string, Anime::ListStatus> map = {
-		{"CURRENT",   Anime::ListStatus::CURRENT  },
-		{"PLANNING",  Anime::ListStatus::PLANNING },
-		{"COMPLETED", Anime::ListStatus::COMPLETED},
-		{"DROPPED",   Anime::ListStatus::DROPPED  },
-		{"PAUSED",    Anime::ListStatus::PAUSED   }
-	};
+	    {"CURRENT",   Anime::ListStatus::CURRENT  },
+	    {"PLANNING",  Anime::ListStatus::PLANNING },
+	    {"COMPLETED", Anime::ListStatus::COMPLETED},
+	    {"DROPPED",   Anime::ListStatus::DROPPED  },
+	    {"PAUSED",    Anime::ListStatus::PAUSED   }
+    };
 
 	if (status == "REPEATING") {
 		anime.SetUserIsRewatching(true);
@@ -127,7 +128,8 @@
 	anime.SetEpisodes(JSON::GetNumber(json, "/episodes"_json_pointer, 0));
 	anime.SetFormat(Translate::AniList::ToSeriesFormat(JSON::GetString<std::string>(json, "/format"_json_pointer, "")));
 
-	anime.SetAiringStatus(Translate::AniList::ToSeriesStatus(JSON::GetString<std::string>(json, "/status"_json_pointer, "")));
+	anime.SetAiringStatus(
+	    Translate::AniList::ToSeriesStatus(JSON::GetString<std::string>(json, "/status"_json_pointer, "")));
 
 	anime.SetAirDate(Date(json["/startDate"_json_pointer]));
 
@@ -179,54 +181,54 @@
 
 	/* 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"
-							  "          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"
-							  "        }\n"
-							  "      }\n"
-							  "    }\n"
-							  "  }\n"
-							  "}\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"
+	                                   "          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"
+	                                   "        }\n"
+	                                   "      }\n"
+	                                   "    }\n"
+	                                   "  }\n"
+	                                   "}\n";
 	// clang-format off
 	nlohmann::json json = {
 		{"query", query},
@@ -246,36 +248,35 @@
 
 /* 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"
-		"      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"
-		"    }\n"
-		"  }\n"
-		"}\n";
+	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"
+	                                   "      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"
+	                                   "    }\n"
+	                                   "  }\n"
+	                                   "}\n";
 
 	// clang-format off
 	nlohmann::json json = {
@@ -308,7 +309,7 @@
 	 * int progress,
 	 * int progressVolumes, // manga-specific.
 	 * int repeat, // rewatch
-	 * int priority,  
+	 * int priority,
 	 * bool private,
 	 * string notes,
 	 * bool hiddenFromStatusLists,
@@ -316,17 +317,19 @@
 	 * float[] advancedScores,
 	 * Date startedAt,
 	 * Date completedAt
-	**/
+	 **/
 	Anime::Anime& anime = Anime::db.items[id];
 	if (!anime.IsInUserList())
 		return 0;
 
 	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";
+	    "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";
 	// clang-format off
 	nlohmann::json json = {
 		{"query", query},
@@ -355,13 +358,13 @@
 
 bool AuthorizeUser() {
 	/* Prompt for PIN */
-	QDesktopServices::openUrl(
-		QUrl(Strings::ToQString("https://anilist.co/api/v2/oauth/authorize?client_id=" + Strings::ToUtf8String(CLIENT_ID) + "&response_type=token")));
+	QDesktopServices::openUrl(QUrl(Strings::ToQString("https://anilist.co/api/v2/oauth/authorize?client_id=" +
+	                                                  Strings::ToUtf8String(CLIENT_ID) + "&response_type=token")));
 
 	bool ok;
 	QString token = QInputDialog::getText(
-		0, "Credentials needed!", "Please enter the code given to you after logging in to AniList:", QLineEdit::Normal,
-		"", &ok);
+	    0, "Credentials needed!", "Please enter the code given to you after logging in to AniList:", QLineEdit::Normal,
+	    "", &ok);
 
 	if (!ok || token.isEmpty())
 		return false;
@@ -369,17 +372,17 @@
 	account.SetAuthToken(Strings::ToUtf8String(token));
 
 	constexpr std::string_view query = "query {\n"
-							  "  Viewer {\n"
-							  "    id\n"
-							  "    name\n"
-							  "    mediaListOptions {\n"
-							  "      scoreFormat\n" // this will be used... eventually
-							  "    }\n"
-							  "  }\n"
-							  "}\n";
+	                                   "  Viewer {\n"
+	                                   "    id\n"
+	                                   "    name\n"
+	                                   "    mediaListOptions {\n"
+	                                   "      scoreFormat\n" // this will be used... eventually
+	                                   "    }\n"
+	                                   "  }\n"
+	                                   "}\n";
 	nlohmann::json json = {
-		{"query", query}
-	};
+	    {"query", query}
+    };
 
 	auto ret = SendJSONRequest(json);