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