changeset 176:121c2d5b321f

anime/db: finalize anime db cache
author Paper <mrpapersonic@gmail.com>
date Fri, 01 Dec 2023 13:12:26 -0500
parents 9b10175be389
children 122fad646f81
files src/core/anime_db.cc src/core/filesystem.cc
diffstat 2 files changed, 18 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/anime_db.cc	Thu Nov 30 13:52:26 2023 -0500
+++ b/src/core/anime_db.cc	Fri Dec 01 13:12:26 2023 -0500
@@ -9,6 +9,9 @@
 
 #include <fstream>
 
+#include <iostream>
+#include <exception>
+
 namespace Anime {
 
 int Database::GetTotalAnimeAmount() {
@@ -271,8 +274,8 @@
 	anime.SetDuration(JSON::GetNumber(json, "/duration"_json_pointer, 0));
 	anime.SetPosterUrl(JSON::GetString<std::string>(json, "/poster_url"_json_pointer, ""));
 
-	if (json.contains("/list_data") && json.at("/list_data").is_array())
-		ParseAnimeUserInfoJSON(json, anime);
+	if (json.contains("/list_data"_json_pointer) && json.at("/list_data"_json_pointer).is_object())
+		ParseAnimeUserInfoJSON(json.at("/list_data"_json_pointer), anime);
 
 	return true;
 }
@@ -293,9 +296,13 @@
 		return false;
 
 	/* When parsing, do NOT throw exceptions */
-	nlohmann::json json = json.parse(db_file, nullptr, false);
-	if (json.is_discarded())
-		return false; /* Give up */
+	nlohmann::json json;
+	try {
+		json = json.parse(db_file);
+	} catch (std::exception const& ex) {
+		std::cerr << "[anime/db] Failed to parse JSON! " << ex.what() << std::endl;
+		return false;
+	}
 
 	if (!ParseDatabaseJSON(json)) /* How */
 		return false;
--- a/src/core/filesystem.cc	Thu Nov 30 13:52:26 2023 -0500
+++ b/src/core/filesystem.cc	Fri Dec 01 13:12:26 2023 -0500
@@ -39,21 +39,27 @@
 #ifdef WIN32
 	std::filesystem::path path;
 	wchar_t* buf;
+
 	if (SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_CREATE, NULL, &buf) == S_OK)
 		path = buf;
 	else
 		return std::filesystem::path();
+
 	CoTaskMemFree(buf);
+
 	return path / CONFIG_DIR;
 #elif defined(MACOSX)
 	return std::filesystem::path(osx::GetApplicationSupportDirectory()) / CONFIG_DIR;
 #else // just assume POSIX
 	std::filesystem::path path;
 	const char* home = getenv("HOME");
+
 #	ifdef __linux__
 	if (!home)
 		home = getpwuid(getuid())->pw_dir;
 #	endif // __linux__
+
+	/* only do this if the home directory was really found */
 	if (home)
 		return std::filesystem::path(home) / ".config" / CONFIG_DIR;
 	else