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