Mercurial > minori
comparison src/core/anime_db.cc @ 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 |
comparison
equal
deleted
inserted
replaced
| 175:9b10175be389 | 176:121c2d5b321f |
|---|---|
| 6 | 6 |
| 7 #include "gui/translate/anime.h" | 7 #include "gui/translate/anime.h" |
| 8 #include "gui/translate/anilist.h" | 8 #include "gui/translate/anilist.h" |
| 9 | 9 |
| 10 #include <fstream> | 10 #include <fstream> |
| 11 | |
| 12 #include <iostream> | |
| 13 #include <exception> | |
| 11 | 14 |
| 12 namespace Anime { | 15 namespace Anime { |
| 13 | 16 |
| 14 int Database::GetTotalAnimeAmount() { | 17 int Database::GetTotalAnimeAmount() { |
| 15 int total = 0; | 18 int total = 0; |
| 269 anime.SetAudienceScore(JSON::GetNumber(json, "/audience_score"_json_pointer, 0)); | 272 anime.SetAudienceScore(JSON::GetNumber(json, "/audience_score"_json_pointer, 0)); |
| 270 anime.SetSynopsis(JSON::GetString<std::string>(json, "/synopsis"_json_pointer, "")); | 273 anime.SetSynopsis(JSON::GetString<std::string>(json, "/synopsis"_json_pointer, "")); |
| 271 anime.SetDuration(JSON::GetNumber(json, "/duration"_json_pointer, 0)); | 274 anime.SetDuration(JSON::GetNumber(json, "/duration"_json_pointer, 0)); |
| 272 anime.SetPosterUrl(JSON::GetString<std::string>(json, "/poster_url"_json_pointer, "")); | 275 anime.SetPosterUrl(JSON::GetString<std::string>(json, "/poster_url"_json_pointer, "")); |
| 273 | 276 |
| 274 if (json.contains("/list_data") && json.at("/list_data").is_array()) | 277 if (json.contains("/list_data"_json_pointer) && json.at("/list_data"_json_pointer).is_object()) |
| 275 ParseAnimeUserInfoJSON(json, anime); | 278 ParseAnimeUserInfoJSON(json.at("/list_data"_json_pointer), anime); |
| 276 | 279 |
| 277 return true; | 280 return true; |
| 278 } | 281 } |
| 279 | 282 |
| 280 bool Database::ParseDatabaseJSON(const nlohmann::json& json) { | 283 bool Database::ParseDatabaseJSON(const nlohmann::json& json) { |
| 291 std::ifstream db_file(db_path); | 294 std::ifstream db_file(db_path); |
| 292 if (!db_file) | 295 if (!db_file) |
| 293 return false; | 296 return false; |
| 294 | 297 |
| 295 /* When parsing, do NOT throw exceptions */ | 298 /* When parsing, do NOT throw exceptions */ |
| 296 nlohmann::json json = json.parse(db_file, nullptr, false); | 299 nlohmann::json json; |
| 297 if (json.is_discarded()) | 300 try { |
| 298 return false; /* Give up */ | 301 json = json.parse(db_file); |
| 302 } catch (std::exception const& ex) { | |
| 303 std::cerr << "[anime/db] Failed to parse JSON! " << ex.what() << std::endl; | |
| 304 return false; | |
| 305 } | |
| 299 | 306 |
| 300 if (!ParseDatabaseJSON(json)) /* How */ | 307 if (!ParseDatabaseJSON(json)) /* How */ |
| 301 return false; | 308 return false; |
| 302 | 309 |
| 303 return true; | 310 return true; |
