Mercurial > minori
annotate src/core/anime_db.cc @ 380:861368fd42ff
about: update email address
| author | Paper <paper@tflc.us> |
|---|---|
| date | Thu, 06 Nov 2025 01:12:39 -0500 |
| parents | 5eaafed6c10b |
| children | 5beae59cf042 |
| rev | line source |
|---|---|
| 10 | 1 #include "core/anime_db.h" |
| 2 #include "core/anime.h" | |
| 258 | 3 #include "core/filesystem.h" |
| 379 | 4 #include "core/http.h" |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
5 #include "core/json.h" |
|
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
6 #include "core/session.h" |
| 258 | 7 #include "core/strings.h" |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
8 |
| 258 | 9 #include "gui/translate/anilist.h" |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
10 #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
|
11 |
|
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
177
diff
changeset
|
12 #include <QDate> |
|
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
177
diff
changeset
|
13 |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
14 #include <fstream> |
| 10 | 15 |
| 369 | 16 #include <cstdlib> |
| 258 | 17 #include <exception> |
|
176
121c2d5b321f
anime/db: finalize anime db cache
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
18 #include <iostream> |
|
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
19 #include <random> |
|
176
121c2d5b321f
anime/db: finalize anime db cache
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
20 |
| 10 | 21 namespace Anime { |
| 22 | |
| 369 | 23 size_t Database::GetTotalAnimeAmount() const |
| 24 { | |
|
177
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
25 size_t total = 0; |
|
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
26 |
| 369 | 27 for (const auto &[id, anime] : items) |
|
177
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
28 if (anime.IsInUserList()) |
| 10 | 29 total++; |
|
177
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
30 |
| 10 | 31 return total; |
| 32 } | |
| 33 | |
| 369 | 34 size_t Database::GetListsAnimeAmount(ListStatus status) const |
| 35 { | |
| 279 | 36 if (status == ListStatus::NotInList) |
| 10 | 37 return 0; |
|
177
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
38 |
|
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
39 size_t total = 0; |
|
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
40 |
| 369 | 41 for (const auto &[id, anime] : items) |
|
177
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
42 if (anime.IsInUserList() && anime.GetUserStatus() == status) |
| 10 | 43 total++; |
|
177
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
44 |
| 10 | 45 return total; |
| 46 } | |
| 47 | |
| 369 | 48 size_t Database::GetTotalEpisodeAmount() const |
| 49 { | |
|
177
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
50 size_t total = 0; |
|
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
51 |
| 369 | 52 for (const auto &[id, anime] : items) |
|
177
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
53 if (anime.IsInUserList()) |
|
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
54 total += anime.GetUserRewatchedTimes() * anime.GetEpisodes() + anime.GetUserProgress(); |
|
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
55 |
| 10 | 56 return total; |
| 57 } | |
| 58 | |
| 59 /* Returns the total watched amount in minutes. */ | |
| 369 | 60 size_t Database::GetTotalWatchedAmount() const |
| 61 { | |
|
177
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
62 size_t total = 0; |
|
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
63 |
| 369 | 64 for (const auto &[id, anime] : items) |
|
177
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
65 if (anime.IsInUserList()) |
| 258 | 66 total += anime.GetDuration() * anime.GetUserProgress() + |
| 67 anime.GetEpisodes() * anime.GetDuration() * anime.GetUserRewatchedTimes(); | |
|
177
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
68 |
| 10 | 69 return total; |
| 70 } | |
| 71 | |
| 72 /* Returns the total planned amount in minutes. | |
| 73 Note that we should probably limit progress to the | |
| 74 amount of episodes, as AniList will let you | |
| 75 set episode counts up to 32768. But that should | |
| 76 rather be handled elsewhere. */ | |
| 369 | 77 size_t Database::GetTotalPlannedAmount() const |
| 78 { | |
|
177
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
79 size_t total = 0; |
|
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
80 |
| 369 | 81 for (const auto &[id, anime] : items) |
|
177
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
82 if (anime.IsInUserList()) |
|
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
83 total += anime.GetDuration() * (anime.GetEpisodes() - anime.GetUserProgress()); |
|
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
84 |
| 10 | 85 return total; |
| 86 } | |
| 87 | |
|
177
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
88 /* In Taiga this is called the mean, but "average" is |
| 83 | 89 what's primarily used in conversation, at least |
| 90 in the U.S. */ | |
| 369 | 91 double Database::GetAverageScore() const |
| 92 { | |
| 10 | 93 double avg = 0; |
|
177
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
94 size_t amt = 0; |
|
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
95 |
| 369 | 96 for (const auto &[id, anime] : items) { |
|
177
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
97 if (anime.IsInUserList() && anime.GetUserScore()) { |
|
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
98 avg += anime.GetUserScore(); |
| 10 | 99 amt++; |
| 100 } | |
| 101 } | |
| 102 return avg / amt; | |
| 103 } | |
| 104 | |
| 369 | 105 double Database::GetScoreDeviation() const |
| 106 { | |
| 10 | 107 double squares_sum = 0, avg = GetAverageScore(); |
|
177
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
108 size_t amt = 0; |
|
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
109 |
| 369 | 110 for (const auto &[id, anime] : items) { |
|
177
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
111 if (anime.IsInUserList() && anime.GetUserScore()) { |
|
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
112 squares_sum += std::pow(static_cast<double>(anime.GetUserScore()) - avg, 2); |
| 10 | 113 amt++; |
| 114 } | |
| 115 } | |
|
177
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
116 |
| 10 | 117 return (amt > 0) ? std::sqrt(squares_sum / amt) : 0; |
| 118 } | |
| 119 | |
| 369 | 120 int Database::LookupAnimeTitle(const std::string &title) const |
| 121 { | |
| 64 | 122 if (title.empty()) |
| 123 return 0; | |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
124 |
| 264 | 125 std::string title_n(title); |
| 126 Strings::NormalizeAnimeTitle(title_n); | |
| 127 | |
| 369 | 128 for (const auto &[id, anime] : items) { |
|
260
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
129 std::vector<std::string> synonyms(anime.GetTitleSynonyms()); |
|
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
130 synonyms.push_back(anime.GetUserPreferredTitle()); |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
131 |
| 369 | 132 for (auto &synonym : synonyms) { |
| 264 | 133 Strings::NormalizeAnimeTitle(synonym); |
| 134 if (synonym == title_n) | |
|
254
d14f8e0e40c3
[UNFINISHED] *: update anime button
Paper <paper@paper.us.eu.org>
parents:
236
diff
changeset
|
135 return id; |
|
260
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
136 } |
| 64 | 137 } |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
138 |
|
254
d14f8e0e40c3
[UNFINISHED] *: update anime button
Paper <paper@paper.us.eu.org>
parents:
236
diff
changeset
|
139 return 0; |
| 64 | 140 } |
| 141 | |
| 369 | 142 static bool GetListDataAsJSON(const Anime &anime, nlohmann::json &json) |
| 143 { | |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
144 if (!anime.IsInUserList()) |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
145 return false; |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
146 |
|
177
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
147 // clang-format off |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
148 json = { |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
149 {"status", Translate::ToString(anime.GetUserStatus())}, |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
150 {"progress", anime.GetUserProgress()}, |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
151 {"score", anime.GetUserScore()}, |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
152 {"started", anime.GetUserDateStarted().GetAsAniListJson()}, |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
153 {"completed", anime.GetUserDateCompleted().GetAsAniListJson()}, |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
154 {"private", anime.GetUserIsPrivate()}, |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
155 {"rewatched_times", anime.GetUserRewatchedTimes()}, |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
156 {"rewatching", anime.GetUserIsRewatching()}, |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
157 {"updated", anime.GetUserTimeUpdated()}, |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
158 {"notes", anime.GetUserNotes()} |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
159 }; |
|
177
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
160 // clang-format on |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
161 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
162 return true; |
|
173
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 |
| 369 | 165 static bool GetAnimeAsJSON(const Anime &anime, nlohmann::json &json) |
| 166 { | |
|
177
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
167 // clang-format off |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
168 json = { |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
169 {"id", anime.GetId()}, |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
170 {"synonyms", anime.GetTitleSynonyms()}, |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
171 {"episodes", anime.GetEpisodes()}, |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
172 {"airing_status", Translate::ToString(anime.GetAiringStatus())}, |
|
324
5d3c9b31aa6e
anime: add completed date member
Paper <paper@paper.us.eu.org>
parents:
320
diff
changeset
|
173 {"started_date", anime.GetStartedDate().GetAsAniListJson()}, |
|
5d3c9b31aa6e
anime: add completed date member
Paper <paper@paper.us.eu.org>
parents:
320
diff
changeset
|
174 {"completed_date", anime.GetCompletedDate().GetAsAniListJson()}, |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
175 {"genres", anime.GetGenres()}, |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
176 {"producers", anime.GetProducers()}, |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
177 {"format", Translate::ToString(anime.GetFormat())}, |
| 279 | 178 // {"season", Translate::ToString(anime.GetSeason())}, |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
179 {"audience_score", anime.GetAudienceScore()}, |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
180 {"synopsis", anime.GetSynopsis()}, |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
181 {"duration", anime.GetDuration()}, |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
182 {"poster_url", anime.GetPosterUrl()} |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
183 }; |
|
177
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
184 // clang-format on |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
185 |
|
284
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
186 /* now for dynamically-filled stuff */ |
| 369 | 187 for (const auto &lang : TitleLanguages) { |
|
284
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
188 std::optional<std::string> title = anime.GetTitle(lang); |
|
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
189 if (title.has_value()) |
|
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
190 json["title"][Strings::ToLower(Translate::ToString(lang))] = title.value(); |
|
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
191 } |
|
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
192 |
| 369 | 193 for (const auto &service : Services) { |
|
286
53e3c015a973
anime: initial cross-service support
Paper <paper@paper.us.eu.org>
parents:
284
diff
changeset
|
194 std::optional<std::string> id = anime.GetServiceId(service); |
|
53e3c015a973
anime: initial cross-service support
Paper <paper@paper.us.eu.org>
parents:
284
diff
changeset
|
195 if (id.has_value()) |
|
53e3c015a973
anime: initial cross-service support
Paper <paper@paper.us.eu.org>
parents:
284
diff
changeset
|
196 json["ids"][Strings::ToLower(Translate::ToString(service))] = id.value(); |
|
53e3c015a973
anime: initial cross-service support
Paper <paper@paper.us.eu.org>
parents:
284
diff
changeset
|
197 } |
|
53e3c015a973
anime: initial cross-service support
Paper <paper@paper.us.eu.org>
parents:
284
diff
changeset
|
198 |
|
173
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 |
| 369 | 206 bool Database::GetDatabaseAsJSON(nlohmann::json &json) const |
| 207 { | |
| 208 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
|
209 nlohmann::json anime_json = {}; |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
210 GetAnimeAsJSON(anime, anime_json); |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
211 json.push_back(anime_json); |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
212 } |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
213 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
214 return true; |
|
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 |
| 369 | 217 bool Database::SaveDatabaseToDisk() const |
| 218 { | |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
219 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
|
220 Filesystem::CreateDirectories(db_path); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
221 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
222 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
|
223 if (!db_file) |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
224 return false; |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
225 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
226 nlohmann::json json = {}; |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
227 if (!GetDatabaseAsJSON(json)) |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
228 return false; |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
229 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
230 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
|
231 return true; |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
232 } |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
233 |
| 369 | 234 static bool ParseAnimeUserInfoJSON(const nlohmann::json &json, Anime &anime) |
| 235 { | |
|
174
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))); |
| 197 | 243 anime.SetUserDateCompleted(Date(JSON::GetValue(json, "/completed"_json_pointer))); |
|
174
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 |
| 369 | 253 static bool ParseAnimeInfoJSON(const nlohmann::json &json, Database &database) |
| 254 { | |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
255 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
|
256 if (!id) |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
257 return false; |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
258 |
| 369 | 259 Anime &anime = database.items[id]; |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
260 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
261 anime.SetId(id); |
| 369 | 262 for (const auto &service : Services) { |
|
320
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
263 nlohmann::json::json_pointer p("/ids/" + Strings::ToLower(Translate::ToString(service))); |
|
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
264 |
|
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
265 if (json.contains(p) && json[p].is_string()) |
|
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
266 anime.SetServiceId(service, json[p].get<std::string>()); |
|
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
267 } |
|
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
268 |
| 369 | 269 for (const auto &lang : TitleLanguages) { |
|
284
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
270 nlohmann::json::json_pointer p("/title/" + Strings::ToLower(Translate::ToString(lang))); |
|
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
271 |
|
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
272 if (json.contains(p) && json[p].is_string()) |
|
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
273 anime.SetTitle(lang, json[p].get<std::string>()); |
|
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
274 } |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
275 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
|
276 anime.SetEpisodes(JSON::GetNumber(json, "/episodes"_json_pointer, 0)); |
| 258 | 277 anime.SetAiringStatus( |
| 278 Translate::ToSeriesStatus(JSON::GetString<std::string>(json, "/airing_status"_json_pointer, ""))); | |
|
324
5d3c9b31aa6e
anime: add completed date member
Paper <paper@paper.us.eu.org>
parents:
320
diff
changeset
|
279 anime.SetStartedDate(Date(JSON::GetValue(json, "/started_date"_json_pointer))); |
|
5d3c9b31aa6e
anime: add completed date member
Paper <paper@paper.us.eu.org>
parents:
320
diff
changeset
|
280 anime.SetCompletedDate(Date(JSON::GetValue(json, "/completed_date"_json_pointer))); |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
281 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
|
282 anime.SetProducers(JSON::GetArray<std::vector<std::string>>(json, "/producers"_json_pointer, {})); |
| 175 | 283 anime.SetFormat(Translate::ToSeriesFormat(JSON::GetString<std::string>(json, "/format"_json_pointer, ""))); |
| 279 | 284 // 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
|
285 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
|
286 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
|
287 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
|
288 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
|
289 |
|
176
121c2d5b321f
anime/db: finalize anime db cache
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
290 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
|
291 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
|
292 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
293 return true; |
|
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 |
| 369 | 296 bool Database::ParseDatabaseJSON(const nlohmann::json &json) |
| 297 { | |
| 298 for (const auto &anime_json : json) | |
|
177
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
Paper <mrpapersonic@gmail.com>
parents:
176
diff
changeset
|
299 ParseAnimeInfoJSON(anime_json, *this); |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
300 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
301 return true; |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
302 } |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
303 |
| 369 | 304 bool Database::LoadDatabaseFromDisk() |
| 305 { | |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
306 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
|
307 Filesystem::CreateDirectories(db_path); |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
308 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
309 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
|
310 if (!db_file) |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
311 return false; |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
312 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
313 /* When parsing, do NOT throw exceptions */ |
|
176
121c2d5b321f
anime/db: finalize anime db cache
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
314 nlohmann::json json; |
|
121c2d5b321f
anime/db: finalize anime db cache
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
315 try { |
|
121c2d5b321f
anime/db: finalize anime db cache
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
316 json = json.parse(db_file); |
| 369 | 317 } catch (std::exception const &ex) { |
|
176
121c2d5b321f
anime/db: finalize anime db cache
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
318 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
|
319 return false; |
|
121c2d5b321f
anime/db: finalize anime db cache
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
320 } |
|
174
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
321 |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
322 if (!ParseDatabaseJSON(json)) /* How */ |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
323 return false; |
|
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
Paper <mrpapersonic@gmail.com>
parents:
173
diff
changeset
|
324 |
|
173
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
325 return true; |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
326 } |
|
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
Paper <mrpapersonic@gmail.com>
parents:
83
diff
changeset
|
327 |
| 369 | 328 int Database::GetUnusedId() const |
| 329 { | |
|
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
330 std::uniform_int_distribution<int> distrib(1, INT_MAX); |
|
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
331 int res; |
|
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
332 |
|
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
333 do { |
|
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
334 res = distrib(session.gen); |
|
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
335 } while (items.count(res) && !res); |
|
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
336 |
|
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
337 return res; |
|
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
338 } |
|
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
339 |
| 369 | 340 int Database::LookupServiceId(Service service, const std::string &id_to_find) const |
| 341 { | |
| 342 for (const auto &[id, anime] : items) { | |
|
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
343 std::optional<std::string> service_id = anime.GetServiceId(service); |
|
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
344 if (!service_id) |
|
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
345 continue; |
|
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
346 |
|
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
347 if (service_id == id_to_find) |
|
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
348 return id; |
|
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
349 } |
|
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
350 |
|
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
351 return 0; |
|
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
352 } |
|
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
353 |
| 369 | 354 int Database::LookupServiceIdOrUnused(Service service, const std::string &id_to_find) const |
| 355 { | |
|
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
356 int id = LookupServiceId(service, id_to_find); |
|
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
357 if (id) |
|
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
358 return id; |
|
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
359 |
|
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
360 return GetUnusedId(); |
|
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
361 } |
|
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
286
diff
changeset
|
362 |
| 369 | 363 void Database::RemoveAllUserData() |
| 364 { | |
| 365 for (auto &[id, anime] : items) { | |
|
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
366 if (anime.IsInUserList()) |
|
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
367 anime.RemoveFromUserList(); |
|
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
368 } |
|
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
369 } |
|
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
370 |
| 369 | 371 std::vector<int> Database::GetAllAnimeForSeason(Season season) |
| 372 { | |
|
326
10096c5489e3
anime/db: refactor anime_season_db out
Paper <paper@paper.us.eu.org>
parents:
324
diff
changeset
|
373 std::vector<int> res; |
|
10096c5489e3
anime/db: refactor anime_season_db out
Paper <paper@paper.us.eu.org>
parents:
324
diff
changeset
|
374 |
| 369 | 375 for (const auto &[id, anime] : items) { |
|
327
b5d6c27c308f
anime: refactor Anime::SeriesSeason to Season class
Paper <paper@paper.us.eu.org>
parents:
326
diff
changeset
|
376 if (anime.GetSeason() == season) |
|
326
10096c5489e3
anime/db: refactor anime_season_db out
Paper <paper@paper.us.eu.org>
parents:
324
diff
changeset
|
377 res.push_back(id); |
|
10096c5489e3
anime/db: refactor anime_season_db out
Paper <paper@paper.us.eu.org>
parents:
324
diff
changeset
|
378 } |
|
10096c5489e3
anime/db: refactor anime_season_db out
Paper <paper@paper.us.eu.org>
parents:
324
diff
changeset
|
379 |
|
10096c5489e3
anime/db: refactor anime_season_db out
Paper <paper@paper.us.eu.org>
parents:
324
diff
changeset
|
380 return res; |
|
10096c5489e3
anime/db: refactor anime_season_db out
Paper <paper@paper.us.eu.org>
parents:
324
diff
changeset
|
381 } |
|
10096c5489e3
anime/db: refactor anime_season_db out
Paper <paper@paper.us.eu.org>
parents:
324
diff
changeset
|
382 |
| 378 | 383 QImage Database::GetAnimePoster(int id) |
| 384 { | |
| 385 /* Creates an anime poster and then saves it to disk */ | |
| 386 QImage res; | |
| 387 QString posterfile; | |
| 388 | |
| 389 std::filesystem::path posterpath = Filesystem::GetAnimePostersPath(); | |
| 390 | |
| 391 posterpath = posterpath / ((Strings::ToUtf8String(id) + ".png")); | |
| 392 | |
| 393 Filesystem::CreateDirectories(posterpath); | |
| 394 | |
| 395 /* Load from disk if available */ | |
| 396 posterfile = Strings::ToQString(posterpath.string()); | |
| 397 | |
| 398 if (res.load(posterfile)) | |
| 399 return res; | |
| 400 | |
| 401 /* TODO check if the anime even exists.. */ | |
| 402 | |
| 403 Anime &anime = db.items[id]; | |
| 404 | |
| 405 QByteArray arr = HTTP::Request(anime.GetPosterUrl()); | |
| 406 | |
| 407 /* If it's not valid, return dummy QImage */ | |
| 408 if (arr.isNull() || !res.loadFromData(arr)) | |
| 409 return QImage(); | |
| 410 | |
| 411 qDebug() << posterfile; | |
| 412 | |
| 413 /* Save the result to disk | |
| 414 * TODO might be more efficient to save with original format, | |
| 415 * and save filename as well */ | |
| 416 if (!res.save(posterfile, "png")) | |
| 417 qDebug() << "oops\n"; | |
| 418 | |
| 419 return res; | |
| 420 } | |
| 421 | |
| 11 | 422 Database db; |
| 423 | |
| 279 | 424 } // namespace Anime |
