diff src/core/anime_db.cc @ 319:d928ec7b6a0d

services/kitsu: implement GetAnimeList() it finally works!
author Paper <paper@paper.us.eu.org>
date Wed, 12 Jun 2024 17:52:26 -0400
parents b1f4d1867ab1
children 1b5c04268d6a
line wrap: on
line diff
--- a/src/core/anime_db.cc	Wed Jun 12 05:25:41 2024 -0400
+++ b/src/core/anime_db.cc	Wed Jun 12 17:52:26 2024 -0400
@@ -2,6 +2,7 @@
 #include "core/anime.h"
 #include "core/filesystem.h"
 #include "core/json.h"
+#include "core/session.h"
 #include "core/strings.h"
 
 #include "gui/translate/anilist.h"
@@ -18,7 +19,7 @@
 
 namespace Anime {
 
-size_t Database::GetTotalAnimeAmount() {
+size_t Database::GetTotalAnimeAmount() const {
 	size_t total = 0;
 
 	for (const auto& [id, anime] : items)
@@ -28,7 +29,7 @@
 	return total;
 }
 
-size_t Database::GetListsAnimeAmount(ListStatus status) {
+size_t Database::GetListsAnimeAmount(ListStatus status) const {
 	if (status == ListStatus::NotInList)
 		return 0;
 
@@ -41,7 +42,7 @@
 	return total;
 }
 
-size_t Database::GetTotalEpisodeAmount() {
+size_t Database::GetTotalEpisodeAmount() const {
 	size_t total = 0;
 
 	for (const auto& [id, anime] : items)
@@ -52,7 +53,7 @@
 }
 
 /* Returns the total watched amount in minutes. */
-size_t Database::GetTotalWatchedAmount() {
+size_t Database::GetTotalWatchedAmount() const {
 	size_t total = 0;
 
 	for (const auto& [id, anime] : items)
@@ -68,7 +69,7 @@
    amount of episodes, as AniList will let you
    set episode counts up to 32768. But that should
    rather be handled elsewhere. */
-size_t Database::GetTotalPlannedAmount() {
+size_t Database::GetTotalPlannedAmount() const {
 	size_t total = 0;
 
 	for (const auto& [id, anime] : items)
@@ -81,7 +82,7 @@
 /* In Taiga this is called the mean, but "average" is
    what's primarily used in conversation, at least
    in the U.S. */
-double Database::GetAverageScore() {
+double Database::GetAverageScore() const {
 	double avg = 0;
 	size_t amt = 0;
 
@@ -94,7 +95,7 @@
 	return avg / amt;
 }
 
-double Database::GetScoreDeviation() {
+double Database::GetScoreDeviation() const {
 	double squares_sum = 0, avg = GetAverageScore();
 	size_t amt = 0;
 
@@ -108,11 +109,7 @@
 	return (amt > 0) ? std::sqrt(squares_sum / amt) : 0;
 }
 
-/*
- * TODO: separate this from the anime DB,
- * provide *some* sort of normalization
- */
-int Database::GetAnimeFromTitle(const std::string& title) {
+int Database::LookupAnimeTitle(const std::string& title) const {
 	if (title.empty())
 		return 0;
 
@@ -194,7 +191,7 @@
 	return true;
 }
 
-bool Database::GetDatabaseAsJSON(nlohmann::json& json) {
+bool Database::GetDatabaseAsJSON(nlohmann::json& json) const {
 	for (const auto& [id, anime] : items) {
 		nlohmann::json anime_json = {};
 		GetAnimeAsJSON(anime, anime_json);
@@ -204,7 +201,7 @@
 	return true;
 }
 
-bool Database::SaveDatabaseToDisk() {
+bool Database::SaveDatabaseToDisk() const {
 	std::filesystem::path db_path = Filesystem::GetAnimeDBPath();
 	Filesystem::CreateDirectories(db_path);
 
@@ -302,22 +299,18 @@
 	return true;
 }
 
-int Database::GetUnusedId() {
-	/* TODO: move these out of here */
-
-	std::random_device rd;
-	std::mt19937 gen(rd());
+int Database::GetUnusedId() const {
 	std::uniform_int_distribution<int> distrib(1, INT_MAX);
 	int res;
 
 	do {
-		res = distrib(gen);
-	} while (items.count(res));
+		res = distrib(session.gen);
+	} while (items.count(res) && !res);
 
 	return res;
 }
 
-int Database::LookupServiceId(Service service, const std::string& id_to_find) {
+int Database::LookupServiceId(Service service, const std::string& id_to_find) const {
 	for (const auto& [id, anime] : items) {
 		std::optional<std::string> service_id = anime.GetServiceId(service);
 		if (!service_id)
@@ -330,7 +323,7 @@
 	return 0;
 }
 
-int Database::LookupServiceIdOrUnused(Service service, const std::string& id_to_find) {
+int Database::LookupServiceIdOrUnused(Service service, const std::string& id_to_find) const {
 	int id = LookupServiceId(service, id_to_find);
 	if (id)
 		return id;
@@ -338,6 +331,13 @@
 	return GetUnusedId();
 }
 
+void Database::RemoveAllUserData() {
+	for (auto& [id, anime] : items) {
+		if (anime.IsInUserList())
+			anime.RemoveFromUserList();
+	}
+}
+
 Database db;
 
 } // namespace Anime