# HG changeset patch # User Paper # Date 1715199665 14400 # Node ID e66ffc338d824e0d21716ca8235db94d7adef01d # Parent 969a3e8c79c5a71677395a09f3b3a75bda6f1c12 anime: refactor title structure to a map diff -r 969a3e8c79c5 -r e66ffc338d82 include/core/anime.h --- a/include/core/anime.h Wed May 08 16:20:37 2024 -0400 +++ b/include/core/anime.h Wed May 08 16:21:05 2024 -0400 @@ -118,11 +118,7 @@ struct SeriesInformation { int id; - struct { - std::string romaji; - std::string english; - std::string native; - } title; + std::map titles; std::vector synonyms; int episodes = 0; SeriesStatus status = SeriesStatus::Unknown; @@ -164,9 +160,7 @@ /* Series data */ int GetId() const; - std::string GetRomajiTitle() const; - std::string GetEnglishTitle() const; - std::string GetNativeTitle() const; + std::optional GetTitle(TitleLanguage language) const; std::vector GetTitleSynonyms() const; int GetEpisodes() const; SeriesStatus GetAiringStatus() const; @@ -182,9 +176,7 @@ std::string GetServiceUrl() const; void SetId(int id); - void SetRomajiTitle(std::string const& title); - void SetEnglishTitle(std::string const& title); - void SetNativeTitle(std::string const& title); + void SetTitle(TitleLanguage language, const std::string& title); void SetTitleSynonyms(std::vector const& synonyms); void AddTitleSynonym(std::string const& synonym); void SetEpisodes(int episodes); diff -r 969a3e8c79c5 -r e66ffc338d82 src/core/anime.cc --- a/src/core/anime.cc Wed May 08 16:20:37 2024 -0400 +++ b/src/core/anime.cc Wed May 08 16:21:05 2024 -0400 @@ -170,19 +170,16 @@ return info_.id; } -std::string Anime::GetRomajiTitle() const { - return info_.title.romaji; -} +/* note: this should use std::optional */ +std::optional Anime::GetTitle(TitleLanguage language) const { + if (info_.titles.find(language) == info_.titles.end()) + return std::nullopt; -std::string Anime::GetEnglishTitle() const { - return info_.title.english; -} - -std::string Anime::GetNativeTitle() const { - return info_.title.native; + return info_.titles.at(language); } std::vector Anime::GetTitleSynonyms() const { + /* mainly for the GUI */ std::vector result; auto add_to_synonyms = [this](std::vector& vec, std::string key) { @@ -190,9 +187,9 @@ vec.push_back(key); }; - add_to_synonyms(result, info_.title.english); - add_to_synonyms(result, info_.title.romaji); - add_to_synonyms(result, info_.title.native); + for (const auto& lang : TitleLanguages) + if (info_.titles.find(lang) != info_.titles.end()) + add_to_synonyms(result, info_.titles.at(lang)); for (auto& synonym : info_.synonyms) add_to_synonyms(result, synonym); @@ -257,16 +254,8 @@ info_.id = id; } -void Anime::SetRomajiTitle(std::string const& title) { - info_.title.romaji = title; -} - -void Anime::SetEnglishTitle(std::string const& title) { - info_.title.english = title; -} - -void Anime::SetNativeTitle(std::string const& title) { - info_.title.native = title; +void Anime::SetTitle(TitleLanguage language, const std::string& title) { + info_.titles[language] = title; } void Anime::SetTitleSynonyms(std::vector const& synonyms) { @@ -318,12 +307,16 @@ } std::string Anime::GetUserPreferredTitle() const { - switch (session.config.anime_list.language) { - case TitleLanguage::Native: return (GetNativeTitle().empty()) ? GetRomajiTitle() : GetNativeTitle(); - case TitleLanguage::Romaji: return (GetEnglishTitle().empty()) ? GetRomajiTitle() : GetEnglishTitle(); - default: break; - } - return GetRomajiTitle(); + std::optional title = GetTitle(session.config.anime_list.language); + if (title.has_value()) + return title.value(); + + title = GetTitle(TitleLanguage::Romaji); + if (title.has_value()) + return title.value(); + + /* what? */ + return std::string(); } } // namespace Anime diff -r 969a3e8c79c5 -r e66ffc338d82 src/core/anime_db.cc --- a/src/core/anime_db.cc Wed May 08 16:20:37 2024 -0400 +++ b/src/core/anime_db.cc Wed May 08 16:21:05 2024 -0400 @@ -157,11 +157,6 @@ // clang-format off json = { {"id", anime.GetId()}, - {"title", { - {"native", anime.GetNativeTitle()}, - {"romaji", anime.GetRomajiTitle()}, - {"english", anime.GetEnglishTitle()} - }}, {"synonyms", anime.GetTitleSynonyms()}, {"episodes", anime.GetEpisodes()}, {"airing_status", Translate::ToString(anime.GetAiringStatus())}, @@ -177,6 +172,13 @@ }; // clang-format on + /* now for dynamically-filled stuff */ + for (const auto& lang : TitleLanguages) { + std::optional title = anime.GetTitle(lang); + if (title.has_value()) + json["title"][Strings::ToLower(Translate::ToString(lang))] = title.value(); + } + nlohmann::json user; if (GetListDataAsJSON(anime, user)) json.push_back({"list_data", user}); @@ -236,9 +238,12 @@ Anime& anime = database.items[id]; anime.SetId(id); - anime.SetNativeTitle(JSON::GetString(json, "/title/native"_json_pointer, "")); - anime.SetRomajiTitle(JSON::GetString(json, "/title/romaji"_json_pointer, "")); - anime.SetEnglishTitle(JSON::GetString(json, "/title/english"_json_pointer, "")); + for (const auto& lang : TitleLanguages) { + nlohmann::json::json_pointer p("/title/" + Strings::ToLower(Translate::ToString(lang))); + + if (json.contains(p) && json[p].is_string()) + anime.SetTitle(lang, json[p].get()); + } anime.SetTitleSynonyms(JSON::GetArray>(json, "/synonyms"_json_pointer, {})); anime.SetEpisodes(JSON::GetNumber(json, "/episodes"_json_pointer, 0)); anime.SetAiringStatus( diff -r 969a3e8c79c5 -r e66ffc338d82 src/core/anime_season.cc --- a/src/core/anime_season.cc Wed May 08 16:20:37 2024 -0400 +++ b/src/core/anime_season.cc Wed May 08 16:21:05 2024 -0400 @@ -7,6 +7,7 @@ SeriesSeason GetSeasonForMonth(Date::Month month) { switch (month) { case Date::Month::Jan: case Date::Month::Feb: case Date::Month::Mar: + default: return SeriesSeason::Winter; case Date::Month::Apr: case Date::Month::May: case Date::Month::Jun: return SeriesSeason::Spring; diff -r 969a3e8c79c5 -r e66ffc338d82 src/services/anilist.cc --- a/src/services/anilist.cc Wed May 08 16:20:37 2024 -0400 +++ b/src/services/anilist.cc Wed May 08 16:21:05 2024 -0400 @@ -110,9 +110,17 @@ } void ParseTitle(const nlohmann::json& json, Anime::Anime& anime) { - anime.SetNativeTitle(JSON::GetString(json, "/native"_json_pointer, "")); - anime.SetEnglishTitle(JSON::GetString(json, "/english"_json_pointer, "")); - anime.SetRomajiTitle(JSON::GetString(json, "/romaji"_json_pointer, "")); + nlohmann::json::json_pointer g = "/native"_json_pointer; + if (json.contains(g) && json[g].is_string()) + anime.SetTitle(Anime::TitleLanguage::Native, json[g]); + + g = "/english"_json_pointer; + if (json.contains(g) && json[g].is_string()) + anime.SetTitle(Anime::TitleLanguage::English, json[g]); + + g = "/romaji"_json_pointer; + if (json.contains(g) && json[g].is_string()) + anime.SetTitle(Anime::TitleLanguage::Romaji, json[g]); } int ParseMediaJson(const nlohmann::json& json) {