diff src/services/anilist.cc @ 291:9a88e1725fd2

*: refactor lots of stuff I forgot to put this into different commits, oops! anyway, it doesn't really matter *that* much since this is an unfinished hobby project anyway. once it starts getting stable commit history will be more important, but for now it's not that big of a deal
author Paper <paper@paper.us.eu.org>
date Sun, 12 May 2024 16:31:07 -0400
parents 53e3c015a973
children 2115488eb302
line wrap: on
line diff
--- a/src/services/anilist.cc	Wed May 08 17:32:28 2024 -0400
+++ b/src/services/anilist.cc	Sun May 12 16:31:07 2024 -0400
@@ -18,6 +18,7 @@
 
 #include <chrono>
 #include <exception>
+#include <string_view>
 
 #include <iostream>
 
@@ -26,7 +27,7 @@
 namespace Services {
 namespace AniList {
 
-static constexpr int CLIENT_ID = 13706;
+static constexpr std::string_view CLIENT_ID = "13706";
 
 class Account {
 public:
@@ -42,13 +43,13 @@
 
 static Account account;
 
-std::string SendRequest(std::string data) {
+static std::string SendRequest(const std::string& data) {
 	std::vector<std::string> headers = {"Authorization: Bearer " + account.AuthToken(), "Accept: application/json",
 	                                    "Content-Type: application/json"};
-	return Strings::ToUtf8String(HTTP::Post("https://graphql.anilist.co", data, headers));
+	return Strings::ToUtf8String(HTTP::Request("https://graphql.anilist.co", headers, data, HTTP::Type::Post));
 }
 
-nlohmann::json SendJSONRequest(nlohmann::json data) {
+static nlohmann::json SendJSONRequest(const nlohmann::json& data) {
 	std::string request = SendRequest(data.dump());
 	if (request.empty()) {
 		std::cerr << "[AniList] JSON Request returned an empty result!" << std::endl;
@@ -72,7 +73,7 @@
 	return ret;
 }
 
-void ParseListStatus(std::string status, Anime::Anime& anime) {
+static 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 },
@@ -95,7 +96,7 @@
 	anime.SetUserStatus(map.at(status));
 }
 
-std::string ListStatusToString(const Anime::Anime& anime) {
+static std::string ListStatusToString(const Anime::Anime& anime) {
 	if (anime.GetUserIsRewatching() && anime.GetUserStatus() == Anime::ListStatus::Current)
 		return "REWATCHING";
 
@@ -109,7 +110,7 @@
 	return "CURRENT";
 }
 
-void ParseTitle(const nlohmann::json& json, Anime::Anime& anime) {
+static void ParseTitle(const nlohmann::json& json, Anime::Anime& anime) {
 	nlohmann::json::json_pointer g = "/native"_json_pointer;
 	if (json.contains(g) && json[g].is_string())
 		anime.SetTitle(Anime::TitleLanguage::Native, json[g]);
@@ -123,7 +124,7 @@
 		anime.SetTitle(Anime::TitleLanguage::Romaji, json[g]);
 }
 
-int ParseMediaJson(const nlohmann::json& json) {
+static int ParseMediaJson(const nlohmann::json& json) {
 	int id = JSON::GetNumber(json, "/id"_json_pointer);
 	if (!id)
 		return 0;
@@ -159,7 +160,7 @@
 	return id;
 }
 
-int ParseListItem(const nlohmann::json& json) {
+static int ParseListItem(const nlohmann::json& json) {
 	int id = ParseMediaJson(json["media"]);
 
 	Anime::Anime& anime = Anime::db.items[id];
@@ -179,7 +180,7 @@
 	return id;
 }
 
-int ParseList(const nlohmann::json& json) {
+static int ParseList(const nlohmann::json& json) {
 	for (const auto& entry : json["entries"].items()) {
 		ParseListItem(entry.value());
 	}
@@ -336,6 +337,10 @@
 	if (!anime.IsInUserList())
 		return 0;
 
+	std::optional<std::string> service_id = anime.GetServiceId(Anime::Service::AniList);
+	if (!service_id)
+		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"
@@ -348,7 +353,7 @@
 	nlohmann::json json = {
 		{"query", query},
 		{"variables", {
-			{"media_id", anime.GetId()},
+			{"media_id", Strings::ToInt<int64_t>(service_id.value())},
 			{"progress", anime.GetUserProgress()},
 			{"status",   ListStatusToString(anime)},
 			{"score",    anime.GetUserScore()},
@@ -365,7 +370,7 @@
 	return JSON::GetNumber(ret, "/data/SaveMediaListEntry/id"_json_pointer, 0);
 }
 
-int ParseUser(const nlohmann::json& json) {
+static int ParseUser(const nlohmann::json& json) {
 	account.SetUserId(JSON::GetNumber(json, "/id"_json_pointer, 0));
 	return account.UserId();
 }
@@ -373,7 +378,7 @@
 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")));
+	                                                  std::string(CLIENT_ID) + "&response_type=token")));
 
 	bool ok;
 	QString token = QInputDialog::getText(