# HG changeset patch # User Paper # Date 1701197634 18000 # Node ID de0a8d2f28b3a4febba29a7f0707af1d842a6024 # Parent 45a0967485f1a4a3f8fda92cafdee78fa94310c5 WILL NOT COMPILE: sample export ability for anime db diff -r 45a0967485f1 -r de0a8d2f28b3 include/core/anime_db.h --- a/include/core/anime_db.h Tue Nov 28 13:22:35 2023 -0500 +++ b/include/core/anime_db.h Tue Nov 28 13:53:54 2023 -0500 @@ -2,6 +2,7 @@ #define __core__anime_db_h #include "core/anime.h" +#include "core/json.h" #include #include @@ -18,6 +19,8 @@ double GetScoreDeviation(); int GetListsAnimeAmount(ListStatus status); int GetAnimeFromTitle(const std::string& title); + + bool GetDatabaseAsJSON(nlohmann::json& json); }; extern Database db; diff -r 45a0967485f1 -r de0a8d2f28b3 src/core/anime_db.cc --- a/src/core/anime_db.cc Tue Nov 28 13:22:35 2023 -0500 +++ b/src/core/anime_db.cc Tue Nov 28 13:53:54 2023 -0500 @@ -1,6 +1,8 @@ #include "core/anime_db.h" #include "core/anime.h" #include "core/strings.h" +#include "core/json.h" + #include namespace Anime { @@ -93,7 +95,8 @@ static T get_lowest_in_map(const std::unordered_map& map) { if (map.size() <= 0) return 0; - T id; + + T id = 0; U ret = std::numeric_limits::max(); for (const auto& t : map) { if (t.second < ret) { @@ -106,7 +109,7 @@ /* This is really fugly but WHO CARES :P - This sort of ""advanced"" algorithm is only in effect because + This fairly basic algorithm is only in effect because there are some special cases, e.g. Another and Re:ZERO, where we get the wrong match, so we have to create Advanced Techniques to solve this @@ -115,31 +118,95 @@ 1. searches each anime item for a match to the preferred title AND all synonyms and marks those matches with `synonym.length() - (synonym.find(needle) + needle.length());` - which, on a title that exactly matches, will be 0 + which should never be less than zero and will be zero if, and only if + the titles match exactly. 2. returns the id of the match that is the lowest, which will most definitely match anything that exactly matches the title of the filename */ int Database::GetAnimeFromTitle(const std::string& title) { if (title.empty()) return 0; - std::unordered_map map; - for (const auto& a : items) { - long long ret = a.second.GetUserPreferredTitle().find(title); - if (ret != static_cast(std::string::npos)) { - map[a.second.GetId()] = a.second.GetUserPreferredTitle().length() - (ret + title.length()); + + std::unordered_map map; + + auto process_title = [&map](const Anime& anime, const std::string& title, const std::string& needle) -> bool { + size_t ret = title.find(needle); + if (ret == std::string::npos) + return false; + + map[anime.GetId()] = title.length() - (ret + needle.length()); + return true; + }; + + for (const auto& [id, anime] : items) { + if (process_title(anime, anime.GetUserPreferredTitle(), title)) continue; - } - for (const auto& synonym : a.second.GetTitleSynonyms()) { - ret = synonym.find(title); - if (ret != static_cast(std::string::npos)) { - map[a.second.GetId()] = synonym.length() - (ret + title.length()); + + for (const auto& synonym : anime.GetTitleSynonyms()) + if (process_title(anime, synonym, title)) continue; - } - } } + return get_lowest_in_map(map); } +static bool GetListDataAsJSON(const Anime& anime, nlohmann::json& json) { + if (!anime.IsInUserList()) + return false; + + json = { + {"status", Translate::ToString(anime.GetUserStatus())}, + {"progress", anime.GetUserProgress()}, + {"score", anime.GetUserScore()}, + //{"started", anime.GetUserDateStarted()}, + //{"completed", anime.GetUserDateCompleted()}, + {"private", anime.GetUserIsPrivate()}, + {"rewatched_times", anime.GetUserRewatchedTimes()}, + {"rewatching", anime.GetUserIsRewatching()}, + {"updated", anime.GetUserTimeUpdated()}, + {"notes", anime.GetUserNotes()} + }; +} + +static bool GetAnimeAsJSON(const Anime& anime, nlohmann::json& json) { + json = { + {"id", anime.GetId()}, + {"title", { + {"native", anime.GetNativeTitle()}, + {"romaji", anime.GetRomajiTitle()}, + {"english", anime.GetEnglishTitle()} + }}, + {"synonyms", anime.GetTitleSynonyms()}, + {"episodes", anime.GetEpisodes()}, + {"airing_status", anime.GetAiringStatus()}, + {"air_date", anime.GetAirDate()}, + {"genres", anime.GetGenres()}, + {"producers", anime.GetProducers()}, + {"format", anime.GetFormat()}, + {"season", anime.GetSeason()}, + {"audience_score", anime.GetAudienceScore()}, + {"synopsis", anime.GetSynopsis()}, + {"duration", anime.GetDuration()}, + {"poster_url", anime.GetPosterUrl()}, + {"service_url", anime.GetServiceUrl()} + }; + + nlohmann::json user; + if (GetListDataAsJSON(anime, user)) + json.push_back({"list_data", user}); + + return true; +} + +bool Database::GetDatabaseAsJSON(nlohmann::json& json) { + for (const auto& [id, anime] : items) { + nlohmann::json anime_json; + GetAnimeAsJSON(anime, anime_json); + json.push_back(anime_json); + } + return true; +} + Database db; } // namespace Anime \ No newline at end of file