comparison src/core/anime_db.cc @ 284:e66ffc338d82

anime: refactor title structure to a map
author Paper <paper@paper.us.eu.org>
date Wed, 08 May 2024 16:21:05 -0400
parents 657fda1b9cac
children 53e3c015a973
comparison
equal deleted inserted replaced
283:969a3e8c79c5 284:e66ffc338d82
155 155
156 static bool GetAnimeAsJSON(const Anime& anime, nlohmann::json& json) { 156 static bool GetAnimeAsJSON(const Anime& anime, nlohmann::json& json) {
157 // clang-format off 157 // clang-format off
158 json = { 158 json = {
159 {"id", anime.GetId()}, 159 {"id", anime.GetId()},
160 {"title", {
161 {"native", anime.GetNativeTitle()},
162 {"romaji", anime.GetRomajiTitle()},
163 {"english", anime.GetEnglishTitle()}
164 }},
165 {"synonyms", anime.GetTitleSynonyms()}, 160 {"synonyms", anime.GetTitleSynonyms()},
166 {"episodes", anime.GetEpisodes()}, 161 {"episodes", anime.GetEpisodes()},
167 {"airing_status", Translate::ToString(anime.GetAiringStatus())}, 162 {"airing_status", Translate::ToString(anime.GetAiringStatus())},
168 {"air_date", anime.GetAirDate().GetAsAniListJson()}, 163 {"air_date", anime.GetAirDate().GetAsAniListJson()},
169 {"genres", anime.GetGenres()}, 164 {"genres", anime.GetGenres()},
175 {"duration", anime.GetDuration()}, 170 {"duration", anime.GetDuration()},
176 {"poster_url", anime.GetPosterUrl()} 171 {"poster_url", anime.GetPosterUrl()}
177 }; 172 };
178 // clang-format on 173 // clang-format on
179 174
175 /* now for dynamically-filled stuff */
176 for (const auto& lang : TitleLanguages) {
177 std::optional<std::string> title = anime.GetTitle(lang);
178 if (title.has_value())
179 json["title"][Strings::ToLower(Translate::ToString(lang))] = title.value();
180 }
181
180 nlohmann::json user; 182 nlohmann::json user;
181 if (GetListDataAsJSON(anime, user)) 183 if (GetListDataAsJSON(anime, user))
182 json.push_back({"list_data", user}); 184 json.push_back({"list_data", user});
183 185
184 return true; 186 return true;
234 return false; 236 return false;
235 237
236 Anime& anime = database.items[id]; 238 Anime& anime = database.items[id];
237 239
238 anime.SetId(id); 240 anime.SetId(id);
239 anime.SetNativeTitle(JSON::GetString<std::string>(json, "/title/native"_json_pointer, "")); 241 for (const auto& lang : TitleLanguages) {
240 anime.SetRomajiTitle(JSON::GetString<std::string>(json, "/title/romaji"_json_pointer, "")); 242 nlohmann::json::json_pointer p("/title/" + Strings::ToLower(Translate::ToString(lang)));
241 anime.SetEnglishTitle(JSON::GetString<std::string>(json, "/title/english"_json_pointer, "")); 243
244 if (json.contains(p) && json[p].is_string())
245 anime.SetTitle(lang, json[p].get<std::string>());
246 }
242 anime.SetTitleSynonyms(JSON::GetArray<std::vector<std::string>>(json, "/synonyms"_json_pointer, {})); 247 anime.SetTitleSynonyms(JSON::GetArray<std::vector<std::string>>(json, "/synonyms"_json_pointer, {}));
243 anime.SetEpisodes(JSON::GetNumber(json, "/episodes"_json_pointer, 0)); 248 anime.SetEpisodes(JSON::GetNumber(json, "/episodes"_json_pointer, 0));
244 anime.SetAiringStatus( 249 anime.SetAiringStatus(
245 Translate::ToSeriesStatus(JSON::GetString<std::string>(json, "/airing_status"_json_pointer, ""))); 250 Translate::ToSeriesStatus(JSON::GetString<std::string>(json, "/airing_status"_json_pointer, "")));
246 anime.SetAirDate(Date(JSON::GetValue(json, "/air_date"_json_pointer))); 251 anime.SetAirDate(Date(JSON::GetValue(json, "/air_date"_json_pointer)));