Mercurial > minori
annotate 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 |
| rev | line source |
|---|---|
| 10 | 1 #include "core/anime_db.h" |
| 2 #include "core/anime.h" | |
| 64 | 3 #include "core/strings.h" |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
4 #include "core/json.h" |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
5 #include "core/filesystem.h" |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
6 |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
7 #include "gui/translate/anime.h" |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
8 #include "gui/translate/anilist.h" |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
9 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
10 #include <fstream> |
| 10 | 11 |
|
176
121c2d5b321f
anime/db: finalize anime db cache
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
12 #include <iostream> |
|
121c2d5b321f
anime/db: finalize anime db cache
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
13 #include <exception> |
|
121c2d5b321f
anime/db: finalize anime db cache
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
14 |
| 10 | 15 namespace Anime { |
| 16 | |
| 17 int Database::GetTotalAnimeAmount() { | |
| 18 int total = 0; | |
| 11 | 19 for (const auto& a : items) { |
| 20 if (a.second.IsInUserList()) | |
| 10 | 21 total++; |
| 22 } | |
| 23 return total; | |
| 24 } | |
| 25 | |
| 26 int Database::GetListsAnimeAmount(ListStatus status) { | |
| 27 if (status == ListStatus::NOT_IN_LIST) | |
| 28 return 0; | |
| 29 int total = 0; | |
| 11 | 30 for (const auto& a : items) { |
| 31 if (a.second.IsInUserList() && a.second.GetUserStatus() == status) | |
| 10 | 32 total++; |
| 33 } | |
| 34 return total; | |
| 35 } | |
| 36 | |
| 37 int Database::GetTotalEpisodeAmount() { | |
| 38 int total = 0; | |
| 11 | 39 for (const auto& a : items) { |
| 40 if (a.second.IsInUserList()) { | |
| 41 total += a.second.GetUserRewatchedTimes() * a.second.GetEpisodes(); | |
| 42 total += a.second.GetUserProgress(); | |
| 10 | 43 } |
| 44 } | |
| 45 return total; | |
| 46 } | |
| 47 | |
| 48 /* Returns the total watched amount in minutes. */ | |
| 49 int Database::GetTotalWatchedAmount() { | |
| 50 int total = 0; | |
| 11 | 51 for (const auto& a : items) { |
| 52 if (a.second.IsInUserList()) { | |
| 53 total += a.second.GetDuration() * a.second.GetUserProgress(); | |
| 54 total += a.second.GetEpisodes() * a.second.GetDuration() * a.second.GetUserRewatchedTimes(); | |
| 10 | 55 } |
| 56 } | |
| 57 return total; | |
| 58 } | |
| 59 | |
| 60 /* Returns the total planned amount in minutes. | |
| 61 Note that we should probably limit progress to the | |
| 62 amount of episodes, as AniList will let you | |
| 63 set episode counts up to 32768. But that should | |
| 64 rather be handled elsewhere. */ | |
| 65 int Database::GetTotalPlannedAmount() { | |
| 66 int total = 0; | |
| 11 | 67 for (const auto& a : items) { |
| 68 if (a.second.IsInUserList()) | |
| 69 total += a.second.GetDuration() * (a.second.GetEpisodes() - a.second.GetUserProgress()); | |
| 10 | 70 } |
| 71 return total; | |
| 72 } | |
| 73 | |
| 83 | 74 /* In Taiga this is called a mean, but "average" is |
| 75 what's primarily used in conversation, at least | |
| 76 in the U.S. */ | |
| 10 | 77 double Database::GetAverageScore() { |
| 78 double avg = 0; | |
| 79 int amt = 0; | |
| 11 | 80 for (const auto& a : items) { |
| 81 if (a.second.IsInUserList() && a.second.GetUserScore()) { | |
| 82 avg += a.second.GetUserScore(); | |
| 10 | 83 amt++; |
| 84 } | |
| 85 } | |
| 86 return avg / amt; | |
| 87 } | |
| 88 | |
| 89 double Database::GetScoreDeviation() { | |
| 90 double squares_sum = 0, avg = GetAverageScore(); | |
| 91 int amt = 0; | |
| 11 | 92 for (const auto& a : items) { |
| 93 if (a.second.IsInUserList() && a.second.GetUserScore()) { | |
| 64 | 94 squares_sum += std::pow(static_cast<double>(a.second.GetUserScore()) - avg, 2); |
| 10 | 95 amt++; |
| 96 } | |
| 97 } | |
| 98 return (amt > 0) ? std::sqrt(squares_sum / amt) : 0; | |
| 99 } | |
| 100 | |
| 83 | 101 template <typename T, typename U> |
| 102 static T get_lowest_in_map(const std::unordered_map<T, U>& map) { | |
| 103 if (map.size() <= 0) | |
| 104 return 0; | |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
105 |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
106 T id = 0; |
| 83 | 107 U ret = std::numeric_limits<U>::max(); |
| 108 for (const auto& t : map) { | |
| 109 if (t.second < ret) { | |
| 110 ret = t.second; | |
| 111 id = t.first; | |
| 112 } | |
| 113 } | |
| 114 return id; | |
| 115 } | |
| 116 | |
| 117 /* This is really fugly but WHO CARES :P | |
| 118 | |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
119 This fairly basic algorithm is only in effect because |
| 83 | 120 there are some special cases, e.g. Another and Re:ZERO, where |
| 121 we get the wrong match, so we have to create Advanced Techniques | |
| 122 to solve this | |
| 123 | |
| 124 This algorithm: | |
| 125 1. searches each anime item for a match to the preferred title | |
| 126 AND all synonyms and marks those matches with | |
| 127 `synonym.length() - (synonym.find(needle) + needle.length());` | |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
128 which should never be less than zero and will be zero if, and only if |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
129 the titles match exactly. |
| 83 | 130 2. returns the id of the match that is the lowest, which will most |
| 131 definitely match anything that exactly matches the title of the | |
| 132 filename */ | |
| 133 int Database::GetAnimeFromTitle(const std::string& title) { | |
| 64 | 134 if (title.empty()) |
| 135 return 0; | |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
136 |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
137 std::unordered_map<int, size_t> map; |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
138 |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
139 auto process_title = [&map](const Anime& anime, const std::string& title, const std::string& needle) -> bool { |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
140 size_t ret = title.find(needle); |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
141 if (ret == std::string::npos) |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
142 return false; |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
143 |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
144 map[anime.GetId()] = title.length() - (ret + needle.length()); |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
145 return true; |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
146 }; |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
147 |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
148 for (const auto& [id, anime] : items) { |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
149 if (process_title(anime, anime.GetUserPreferredTitle(), title)) |
| 83 | 150 continue; |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
151 |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
152 for (const auto& synonym : anime.GetTitleSynonyms()) |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
153 if (process_title(anime, synonym, title)) |
| 83 | 154 continue; |
| 64 | 155 } |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
156 |
| 83 | 157 return get_lowest_in_map(map); |
| 64 | 158 } |
| 159 | |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
160 static bool GetListDataAsJSON(const Anime& anime, nlohmann::json& json) { |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
161 if (!anime.IsInUserList()) |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
162 return false; |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
163 |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
164 json = { |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
165 {"status", Translate::ToString(anime.GetUserStatus())}, |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
166 {"progress", anime.GetUserProgress()}, |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
167 {"score", anime.GetUserScore()}, |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
168 {"started", anime.GetUserDateStarted().GetAsAniListJson()}, |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
169 {"completed", anime.GetUserDateCompleted().GetAsAniListJson()}, |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
170 {"private", anime.GetUserIsPrivate()}, |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
171 {"rewatched_times", anime.GetUserRewatchedTimes()}, |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
172 {"rewatching", anime.GetUserIsRewatching()}, |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
173 {"updated", anime.GetUserTimeUpdated()}, |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
174 {"notes", anime.GetUserNotes()} |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
175 }; |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
176 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
177 return true; |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
178 } |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
179 |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
180 static bool GetAnimeAsJSON(const Anime& anime, nlohmann::json& json) { |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
181 json = { |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
182 {"id", anime.GetId()}, |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
183 {"title", { |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
184 {"native", anime.GetNativeTitle()}, |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
185 {"romaji", anime.GetRomajiTitle()}, |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
186 {"english", anime.GetEnglishTitle()} |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
187 }}, |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
188 {"synonyms", anime.GetTitleSynonyms()}, |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
189 {"episodes", anime.GetEpisodes()}, |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
190 {"airing_status", Translate::ToString(anime.GetAiringStatus())}, |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
191 {"air_date", anime.GetAirDate().GetAsAniListJson()}, |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
192 {"genres", anime.GetGenres()}, |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
193 {"producers", anime.GetProducers()}, |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
194 {"format", Translate::ToString(anime.GetFormat())}, |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
195 {"season", Translate::ToString(anime.GetSeason())}, |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
196 {"audience_score", anime.GetAudienceScore()}, |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
197 {"synopsis", anime.GetSynopsis()}, |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
198 {"duration", anime.GetDuration()}, |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
199 {"poster_url", anime.GetPosterUrl()} |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
200 }; |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
201 |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
202 nlohmann::json user; |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
203 if (GetListDataAsJSON(anime, user)) |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
204 json.push_back({"list_data", user}); |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
205 |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
206 return true; |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
207 } |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
208 |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
209 bool Database::GetDatabaseAsJSON(nlohmann::json& json) { |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
210 for (const auto& [id, anime] : items) { |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
211 nlohmann::json anime_json = {}; |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
212 GetAnimeAsJSON(anime, anime_json); |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
213 json.push_back(anime_json); |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
214 } |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
215 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
216 return true; |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
217 } |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
218 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
219 bool Database::SaveDatabaseToDisk() { |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
220 std::filesystem::path db_path = Filesystem::GetAnimeDBPath(); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
221 Filesystem::CreateDirectories(db_path); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
222 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
223 std::ofstream db_file(db_path); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
224 if (!db_file) |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
225 return false; |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
226 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
227 nlohmann::json json = {}; |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
228 if (!GetDatabaseAsJSON(json)) |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
229 return false; |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
230 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
231 db_file << std::setw(4) << json << std::endl; |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
232 return true; |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
233 } |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
234 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
235 static bool ParseAnimeUserInfoJSON(const nlohmann::json& json, Anime& anime) { |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
236 if (!anime.IsInUserList()) |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
237 anime.AddToUserList(); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
238 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
239 anime.SetUserStatus(Translate::ToListStatus(JSON::GetString<std::string>(json, "/status"_json_pointer, ""))); |
| 175 | 240 anime.SetUserProgress(JSON::GetNumber(json, "/progress"_json_pointer, 0)); |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
241 anime.SetUserScore(JSON::GetNumber(json, "/score"_json_pointer, 0)); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
242 anime.SetUserDateStarted(Date(JSON::GetValue(json, "/started"_json_pointer))); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
243 anime.SetUserDateStarted(Date(JSON::GetValue(json, "/completed"_json_pointer))); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
244 anime.SetUserIsPrivate(JSON::GetBoolean(json, "/private"_json_pointer, false)); |
| 175 | 245 anime.SetUserRewatchedTimes(JSON::GetNumber(json, "/rewatched_times"_json_pointer, 0)); |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
246 anime.SetUserIsRewatching(JSON::GetBoolean(json, "/rewatching"_json_pointer, false)); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
247 anime.SetUserTimeUpdated(JSON::GetNumber(json, "/updated"_json_pointer, 0)); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
248 anime.SetUserNotes(JSON::GetString<std::string>(json, "/notes"_json_pointer, "")); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
249 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
250 return true; |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
251 } |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
252 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
253 bool Database::ParseAnimeInfoJSON(const nlohmann::json& json) { |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
254 int id = JSON::GetNumber(json, "/id"_json_pointer, 0); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
255 if (!id) |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
256 return false; |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
257 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
258 Anime& anime = items[id]; |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
259 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
260 anime.SetId(id); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
261 anime.SetNativeTitle(JSON::GetString<std::string>(json, "/title/native"_json_pointer, "")); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
262 anime.SetRomajiTitle(JSON::GetString<std::string>(json, "/title/romaji"_json_pointer, "")); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
263 anime.SetEnglishTitle(JSON::GetString<std::string>(json, "/title/english"_json_pointer, "")); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
264 anime.SetTitleSynonyms(JSON::GetArray<std::vector<std::string>>(json, "/synonyms"_json_pointer, {})); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
265 anime.SetEpisodes(JSON::GetNumber(json, "/episodes"_json_pointer, 0)); |
| 175 | 266 anime.SetAiringStatus(Translate::ToSeriesStatus(JSON::GetString<std::string>(json, "/airing_status"_json_pointer, ""))); |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
267 anime.SetAirDate(Date(JSON::GetValue(json, "/air_date"_json_pointer))); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
268 anime.SetGenres(JSON::GetArray<std::vector<std::string>>(json, "/genres"_json_pointer, {})); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
269 anime.SetProducers(JSON::GetArray<std::vector<std::string>>(json, "/producers"_json_pointer, {})); |
| 175 | 270 anime.SetFormat(Translate::ToSeriesFormat(JSON::GetString<std::string>(json, "/format"_json_pointer, ""))); |
| 271 anime.SetSeason(Translate::ToSeriesSeason(JSON::GetString<std::string>(json, "/season"_json_pointer, ""))); | |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
272 anime.SetAudienceScore(JSON::GetNumber(json, "/audience_score"_json_pointer, 0)); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
273 anime.SetSynopsis(JSON::GetString<std::string>(json, "/synopsis"_json_pointer, "")); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
274 anime.SetDuration(JSON::GetNumber(json, "/duration"_json_pointer, 0)); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
275 anime.SetPosterUrl(JSON::GetString<std::string>(json, "/poster_url"_json_pointer, "")); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
276 |
|
176
121c2d5b321f
anime/db: finalize anime db cache
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
277 if (json.contains("/list_data"_json_pointer) && json.at("/list_data"_json_pointer).is_object()) |
|
121c2d5b321f
anime/db: finalize anime db cache
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
278 ParseAnimeUserInfoJSON(json.at("/list_data"_json_pointer), anime); |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
279 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
280 return true; |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
281 } |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
282 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
283 bool Database::ParseDatabaseJSON(const nlohmann::json& json) { |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
284 for (const auto& anime_json : json) |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
285 ParseAnimeInfoJSON(anime_json); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
286 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
287 return true; |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
288 } |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
289 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
290 bool Database::LoadDatabaseFromFile() { |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
291 std::filesystem::path db_path = Filesystem::GetAnimeDBPath(); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
292 Filesystem::CreateDirectories(db_path); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
293 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
294 std::ifstream db_file(db_path); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
295 if (!db_file) |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
296 return false; |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
297 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
298 /* When parsing, do NOT throw exceptions */ |
|
176
121c2d5b321f
anime/db: finalize anime db cache
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
299 nlohmann::json json; |
|
121c2d5b321f
anime/db: finalize anime db cache
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
300 try { |
|
121c2d5b321f
anime/db: finalize anime db cache
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
301 json = json.parse(db_file); |
|
121c2d5b321f
anime/db: finalize anime db cache
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
302 } catch (std::exception const& ex) { |
|
121c2d5b321f
anime/db: finalize anime db cache
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
303 std::cerr << "[anime/db] Failed to parse JSON! " << ex.what() << std::endl; |
|
121c2d5b321f
anime/db: finalize anime db cache
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
304 return false; |
|
121c2d5b321f
anime/db: finalize anime db cache
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
305 } |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
306 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
307 if (!ParseDatabaseJSON(json)) /* How */ |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
308 return false; |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
309 |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
310 return true; |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
311 } |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
312 |
| 11 | 313 Database db; |
| 314 | |
| 9 | 315 } // namespace Anime |
