Mercurial > minori
annotate src/core/anime_db.cc @ 317:b1f4d1867ab1
services: VERY initial Kitsu support
it only supports user authentication for now, but it's definitely
a start.
| author | Paper <paper@paper.us.eu.org> | 
|---|---|
| date | Wed, 12 Jun 2024 04:07:10 -0400 | 
| parents | 53e3c015a973 | 
| children | d928ec7b6a0d | 
| rev | line source | 
|---|---|
| 10 | 1 #include "core/anime_db.h" | 
| 2 #include "core/anime.h" | |
| 258 | 3 #include "core/filesystem.h" | 
| 173 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 4 #include "core/json.h" | 
| 258 | 5 #include "core/strings.h" | 
| 173 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 6 | 
| 258 | 7 #include "gui/translate/anilist.h" | 
| 174 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 8 #include "gui/translate/anime.h" | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 9 | 
| 187 
9613d72b097e
*: multiple performance improvements
 Paper <mrpapersonic@gmail.com> parents: 
177diff
changeset | 10 #include <QDate> | 
| 
9613d72b097e
*: multiple performance improvements
 Paper <mrpapersonic@gmail.com> parents: 
177diff
changeset | 11 | 
| 174 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 12 #include <fstream> | 
| 10 | 13 | 
| 258 | 14 #include <exception> | 
| 317 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 15 #include <cstdlib> | 
| 176 
121c2d5b321f
anime/db: finalize anime db cache
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 16 #include <iostream> | 
| 317 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 17 #include <random> | 
| 176 
121c2d5b321f
anime/db: finalize anime db cache
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 18 | 
| 10 | 19 namespace Anime { | 
| 20 | |
| 177 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 21 size_t Database::GetTotalAnimeAmount() { | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 22 size_t total = 0; | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 23 | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 24 for (const auto& [id, anime] : items) | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 25 if (anime.IsInUserList()) | 
| 10 | 26 total++; | 
| 177 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 27 | 
| 10 | 28 return total; | 
| 29 } | |
| 30 | |
| 177 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 31 size_t Database::GetListsAnimeAmount(ListStatus status) { | 
| 279 | 32 if (status == ListStatus::NotInList) | 
| 10 | 33 return 0; | 
| 177 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 34 | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 35 size_t total = 0; | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 36 | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 37 for (const auto& [id, anime] : items) | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 38 if (anime.IsInUserList() && anime.GetUserStatus() == status) | 
| 10 | 39 total++; | 
| 177 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 40 | 
| 10 | 41 return total; | 
| 42 } | |
| 43 | |
| 177 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 44 size_t Database::GetTotalEpisodeAmount() { | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 45 size_t total = 0; | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 46 | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 47 for (const auto& [id, anime] : items) | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 48 if (anime.IsInUserList()) | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 49 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: 
176diff
changeset | 50 | 
| 10 | 51 return total; | 
| 52 } | |
| 53 | |
| 54 /* Returns the total watched amount in minutes. */ | |
| 177 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 55 size_t Database::GetTotalWatchedAmount() { | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 56 size_t total = 0; | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 57 | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 58 for (const auto& [id, anime] : items) | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 59 if (anime.IsInUserList()) | 
| 258 | 60 total += anime.GetDuration() * anime.GetUserProgress() + | 
| 61 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: 
176diff
changeset | 62 | 
| 10 | 63 return total; | 
| 64 } | |
| 65 | |
| 66 /* Returns the total planned amount in minutes. | |
| 67 Note that we should probably limit progress to the | |
| 68 amount of episodes, as AniList will let you | |
| 69 set episode counts up to 32768. But that should | |
| 70 rather be handled elsewhere. */ | |
| 177 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 71 size_t Database::GetTotalPlannedAmount() { | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 72 size_t total = 0; | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 73 | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 74 for (const auto& [id, anime] : items) | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 75 if (anime.IsInUserList()) | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 76 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: 
176diff
changeset | 77 | 
| 10 | 78 return total; | 
| 79 } | |
| 80 | |
| 177 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 81 /* In Taiga this is called the mean, but "average" is | 
| 83 | 82 what's primarily used in conversation, at least | 
| 83 in the U.S. */ | |
| 10 | 84 double Database::GetAverageScore() { | 
| 85 double avg = 0; | |
| 177 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 86 size_t amt = 0; | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 87 | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 88 for (const auto& [id, anime] : items) { | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 89 if (anime.IsInUserList() && anime.GetUserScore()) { | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 90 avg += anime.GetUserScore(); | 
| 10 | 91 amt++; | 
| 92 } | |
| 93 } | |
| 94 return avg / amt; | |
| 95 } | |
| 96 | |
| 97 double Database::GetScoreDeviation() { | |
| 98 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: 
176diff
changeset | 99 size_t amt = 0; | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 100 | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 101 for (const auto& [id, anime] : items) { | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 102 if (anime.IsInUserList() && anime.GetUserScore()) { | 
| 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 103 squares_sum += std::pow(static_cast<double>(anime.GetUserScore()) - avg, 2); | 
| 10 | 104 amt++; | 
| 105 } | |
| 106 } | |
| 177 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 107 | 
| 10 | 108 return (amt > 0) ? std::sqrt(squares_sum / amt) : 0; | 
| 109 } | |
| 110 | |
| 258 | 111 /* | 
| 254 
d14f8e0e40c3
[UNFINISHED] *: update anime button
 Paper <paper@paper.us.eu.org> parents: 
236diff
changeset | 112 * TODO: separate this from the anime DB, | 
| 
d14f8e0e40c3
[UNFINISHED] *: update anime button
 Paper <paper@paper.us.eu.org> parents: 
236diff
changeset | 113 * provide *some* sort of normalization | 
| 258 | 114 */ | 
| 83 | 115 int Database::GetAnimeFromTitle(const std::string& title) { | 
| 64 | 116 if (title.empty()) | 
| 117 return 0; | |
| 173 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 118 | 
| 264 | 119 std::string title_n(title); | 
| 120 Strings::NormalizeAnimeTitle(title_n); | |
| 121 | |
| 173 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 122 for (const auto& [id, anime] : items) { | 
| 260 
dd211ff68b36
pages/seasons: add initial functionality
 Paper <paper@paper.us.eu.org> parents: 
258diff
changeset | 123 std::vector<std::string> synonyms(anime.GetTitleSynonyms()); | 
| 
dd211ff68b36
pages/seasons: add initial functionality
 Paper <paper@paper.us.eu.org> parents: 
258diff
changeset | 124 synonyms.push_back(anime.GetUserPreferredTitle()); | 
| 173 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 125 | 
| 264 | 126 for (auto& synonym : synonyms) { | 
| 127 Strings::NormalizeAnimeTitle(synonym); | |
| 128 if (synonym == title_n) | |
| 254 
d14f8e0e40c3
[UNFINISHED] *: update anime button
 Paper <paper@paper.us.eu.org> parents: 
236diff
changeset | 129 return id; | 
| 260 
dd211ff68b36
pages/seasons: add initial functionality
 Paper <paper@paper.us.eu.org> parents: 
258diff
changeset | 130 } | 
| 64 | 131 } | 
| 173 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 132 | 
| 254 
d14f8e0e40c3
[UNFINISHED] *: update anime button
 Paper <paper@paper.us.eu.org> parents: 
236diff
changeset | 133 return 0; | 
| 64 | 134 } | 
| 135 | |
| 173 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 136 static bool GetListDataAsJSON(const Anime& anime, nlohmann::json& json) { | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 137 if (!anime.IsInUserList()) | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 138 return false; | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 139 | 
| 177 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 140 // clang-format off | 
| 173 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 141 json = { | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 142 {"status", Translate::ToString(anime.GetUserStatus())}, | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 143 {"progress", anime.GetUserProgress()}, | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 144 {"score", anime.GetUserScore()}, | 
| 174 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 145 {"started", anime.GetUserDateStarted().GetAsAniListJson()}, | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 146 {"completed", anime.GetUserDateCompleted().GetAsAniListJson()}, | 
| 173 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 147 {"private", anime.GetUserIsPrivate()}, | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 148 {"rewatched_times", anime.GetUserRewatchedTimes()}, | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 149 {"rewatching", anime.GetUserIsRewatching()}, | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 150 {"updated", anime.GetUserTimeUpdated()}, | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 151 {"notes", anime.GetUserNotes()} | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 152 }; | 
| 177 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 153 // clang-format on | 
| 174 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 154 | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 155 return true; | 
| 173 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 156 } | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 157 | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 158 static bool GetAnimeAsJSON(const Anime& anime, nlohmann::json& json) { | 
| 177 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 159 // clang-format off | 
| 173 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 160 json = { | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 161 {"id", anime.GetId()}, | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 162 {"synonyms", anime.GetTitleSynonyms()}, | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 163 {"episodes", anime.GetEpisodes()}, | 
| 174 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 164 {"airing_status", Translate::ToString(anime.GetAiringStatus())}, | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 165 {"air_date", anime.GetAirDate().GetAsAniListJson()}, | 
| 173 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 166 {"genres", anime.GetGenres()}, | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 167 {"producers", anime.GetProducers()}, | 
| 174 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 168 {"format", Translate::ToString(anime.GetFormat())}, | 
| 279 | 169 // {"season", Translate::ToString(anime.GetSeason())}, | 
| 173 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 170 {"audience_score", anime.GetAudienceScore()}, | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 171 {"synopsis", anime.GetSynopsis()}, | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 172 {"duration", anime.GetDuration()}, | 
| 174 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 173 {"poster_url", anime.GetPosterUrl()} | 
| 173 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 174 }; | 
| 177 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 175 // clang-format on | 
| 173 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 176 | 
| 284 
e66ffc338d82
anime: refactor title structure to a map
 Paper <paper@paper.us.eu.org> parents: 
279diff
changeset | 177 /* now for dynamically-filled stuff */ | 
| 
e66ffc338d82
anime: refactor title structure to a map
 Paper <paper@paper.us.eu.org> parents: 
279diff
changeset | 178 for (const auto& lang : TitleLanguages) { | 
| 
e66ffc338d82
anime: refactor title structure to a map
 Paper <paper@paper.us.eu.org> parents: 
279diff
changeset | 179 std::optional<std::string> title = anime.GetTitle(lang); | 
| 
e66ffc338d82
anime: refactor title structure to a map
 Paper <paper@paper.us.eu.org> parents: 
279diff
changeset | 180 if (title.has_value()) | 
| 
e66ffc338d82
anime: refactor title structure to a map
 Paper <paper@paper.us.eu.org> parents: 
279diff
changeset | 181 json["title"][Strings::ToLower(Translate::ToString(lang))] = title.value(); | 
| 
e66ffc338d82
anime: refactor title structure to a map
 Paper <paper@paper.us.eu.org> parents: 
279diff
changeset | 182 } | 
| 
e66ffc338d82
anime: refactor title structure to a map
 Paper <paper@paper.us.eu.org> parents: 
279diff
changeset | 183 | 
| 286 
53e3c015a973
anime: initial cross-service support
 Paper <paper@paper.us.eu.org> parents: 
284diff
changeset | 184 for (const auto& service : Services) { | 
| 
53e3c015a973
anime: initial cross-service support
 Paper <paper@paper.us.eu.org> parents: 
284diff
changeset | 185 std::optional<std::string> id = anime.GetServiceId(service); | 
| 
53e3c015a973
anime: initial cross-service support
 Paper <paper@paper.us.eu.org> parents: 
284diff
changeset | 186 if (id.has_value()) | 
| 
53e3c015a973
anime: initial cross-service support
 Paper <paper@paper.us.eu.org> parents: 
284diff
changeset | 187 json["ids"][Strings::ToLower(Translate::ToString(service))] = id.value(); | 
| 
53e3c015a973
anime: initial cross-service support
 Paper <paper@paper.us.eu.org> parents: 
284diff
changeset | 188 } | 
| 
53e3c015a973
anime: initial cross-service support
 Paper <paper@paper.us.eu.org> parents: 
284diff
changeset | 189 | 
| 173 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 190 nlohmann::json user; | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 191 if (GetListDataAsJSON(anime, user)) | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 192 json.push_back({"list_data", user}); | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 193 | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 194 return true; | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 195 } | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 196 | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 197 bool Database::GetDatabaseAsJSON(nlohmann::json& json) { | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 198 for (const auto& [id, anime] : items) { | 
| 174 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 199 nlohmann::json anime_json = {}; | 
| 173 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 200 GetAnimeAsJSON(anime, anime_json); | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 201 json.push_back(anime_json); | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 202 } | 
| 174 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 203 | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 204 return true; | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 205 } | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 206 | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 207 bool Database::SaveDatabaseToDisk() { | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 208 std::filesystem::path db_path = Filesystem::GetAnimeDBPath(); | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 209 Filesystem::CreateDirectories(db_path); | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 210 | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 211 std::ofstream db_file(db_path); | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 212 if (!db_file) | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 213 return false; | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 214 | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 215 nlohmann::json json = {}; | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 216 if (!GetDatabaseAsJSON(json)) | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 217 return false; | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 218 | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 219 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: 
173diff
changeset | 220 return true; | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 221 } | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 222 | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 223 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: 
173diff
changeset | 224 if (!anime.IsInUserList()) | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 225 anime.AddToUserList(); | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 226 | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 227 anime.SetUserStatus(Translate::ToListStatus(JSON::GetString<std::string>(json, "/status"_json_pointer, ""))); | 
| 175 | 228 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: 
173diff
changeset | 229 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: 
173diff
changeset | 230 anime.SetUserDateStarted(Date(JSON::GetValue(json, "/started"_json_pointer))); | 
| 197 | 231 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: 
173diff
changeset | 232 anime.SetUserIsPrivate(JSON::GetBoolean(json, "/private"_json_pointer, false)); | 
| 175 | 233 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: 
173diff
changeset | 234 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: 
173diff
changeset | 235 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: 
173diff
changeset | 236 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: 
173diff
changeset | 237 | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 238 return true; | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 239 } | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 240 | 
| 177 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 241 static bool ParseAnimeInfoJSON(const nlohmann::json& json, Database& database) { | 
| 174 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 242 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: 
173diff
changeset | 243 if (!id) | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 244 return false; | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 245 | 
| 177 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 246 Anime& anime = database.items[id]; | 
| 174 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 247 | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 248 anime.SetId(id); | 
| 284 
e66ffc338d82
anime: refactor title structure to a map
 Paper <paper@paper.us.eu.org> parents: 
279diff
changeset | 249 for (const auto& lang : TitleLanguages) { | 
| 
e66ffc338d82
anime: refactor title structure to a map
 Paper <paper@paper.us.eu.org> parents: 
279diff
changeset | 250 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: 
279diff
changeset | 251 | 
| 
e66ffc338d82
anime: refactor title structure to a map
 Paper <paper@paper.us.eu.org> parents: 
279diff
changeset | 252 if (json.contains(p) && json[p].is_string()) | 
| 
e66ffc338d82
anime: refactor title structure to a map
 Paper <paper@paper.us.eu.org> parents: 
279diff
changeset | 253 anime.SetTitle(lang, json[p].get<std::string>()); | 
| 
e66ffc338d82
anime: refactor title structure to a map
 Paper <paper@paper.us.eu.org> parents: 
279diff
changeset | 254 } | 
| 174 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 255 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: 
173diff
changeset | 256 anime.SetEpisodes(JSON::GetNumber(json, "/episodes"_json_pointer, 0)); | 
| 258 | 257 anime.SetAiringStatus( | 
| 258 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: 
173diff
changeset | 259 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: 
173diff
changeset | 260 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: 
173diff
changeset | 261 anime.SetProducers(JSON::GetArray<std::vector<std::string>>(json, "/producers"_json_pointer, {})); | 
| 175 | 262 anime.SetFormat(Translate::ToSeriesFormat(JSON::GetString<std::string>(json, "/format"_json_pointer, ""))); | 
| 279 | 263 // 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: 
173diff
changeset | 264 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: 
173diff
changeset | 265 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: 
173diff
changeset | 266 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: 
173diff
changeset | 267 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: 
173diff
changeset | 268 | 
| 176 
121c2d5b321f
anime/db: finalize anime db cache
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 269 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: 
175diff
changeset | 270 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: 
173diff
changeset | 271 | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 272 return true; | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 273 } | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 274 | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 275 bool Database::ParseDatabaseJSON(const nlohmann::json& json) { | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 276 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: 
176diff
changeset | 277 ParseAnimeInfoJSON(anime_json, *this); | 
| 174 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 278 | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 279 return true; | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 280 } | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 281 | 
| 177 
122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
 Paper <mrpapersonic@gmail.com> parents: 
176diff
changeset | 282 bool Database::LoadDatabaseFromDisk() { | 
| 174 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 283 std::filesystem::path db_path = Filesystem::GetAnimeDBPath(); | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 284 Filesystem::CreateDirectories(db_path); | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 285 | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 286 std::ifstream db_file(db_path); | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 287 if (!db_file) | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 288 return false; | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 289 | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 290 /* When parsing, do NOT throw exceptions */ | 
| 176 
121c2d5b321f
anime/db: finalize anime db cache
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 291 nlohmann::json json; | 
| 
121c2d5b321f
anime/db: finalize anime db cache
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 292 try { | 
| 
121c2d5b321f
anime/db: finalize anime db cache
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 293 json = json.parse(db_file); | 
| 
121c2d5b321f
anime/db: finalize anime db cache
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 294 } catch (std::exception const& ex) { | 
| 
121c2d5b321f
anime/db: finalize anime db cache
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 295 std::cerr << "[anime/db] Failed to parse JSON! " << ex.what() << std::endl; | 
| 
121c2d5b321f
anime/db: finalize anime db cache
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 296 return false; | 
| 
121c2d5b321f
anime/db: finalize anime db cache
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 297 } | 
| 174 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 298 | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 299 if (!ParseDatabaseJSON(json)) /* How */ | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 300 return false; | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
173diff
changeset | 301 | 
| 173 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 302 return true; | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 303 } | 
| 
de0a8d2f28b3
WILL NOT COMPILE: sample export ability for anime db
 Paper <mrpapersonic@gmail.com> parents: 
83diff
changeset | 304 | 
| 317 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 305 int Database::GetUnusedId() { | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 306 /* TODO: move these out of here */ | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 307 | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 308 std::random_device rd; | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 309 std::mt19937 gen(rd()); | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 310 std::uniform_int_distribution<int> distrib(1, INT_MAX); | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 311 int res; | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 312 | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 313 do { | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 314 res = distrib(gen); | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 315 } while (items.count(res)); | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 316 | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 317 return res; | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 318 } | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 319 | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 320 int Database::LookupServiceId(Service service, const std::string& id_to_find) { | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 321 for (const auto& [id, anime] : items) { | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 322 std::optional<std::string> service_id = anime.GetServiceId(service); | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 323 if (!service_id) | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 324 continue; | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 325 | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 326 if (service_id == id_to_find) | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 327 return id; | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 328 } | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 329 | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 330 return 0; | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 331 } | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 332 | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 333 int Database::LookupServiceIdOrUnused(Service service, const std::string& id_to_find) { | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 334 int id = LookupServiceId(service, id_to_find); | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 335 if (id) | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 336 return id; | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 337 | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 338 return GetUnusedId(); | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 339 } | 
| 
b1f4d1867ab1
services: VERY initial Kitsu support
 Paper <paper@paper.us.eu.org> parents: 
286diff
changeset | 340 | 
| 11 | 341 Database db; | 
| 342 | |
| 279 | 343 } // namespace Anime | 
