annotate src/core/anime_db.cc @ 178:bc8d2ccff09c

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