Mercurial > minori
annotate src/services/kitsu.cc @ 334:948955c3ba81
services: use fmt for setting the status bar
this should make localization easier
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Mon, 17 Jun 2024 20:35:31 -0400 |
parents | b5d6c27c308f |
children | 5098387a3a46 |
rev | line source |
---|---|
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
1 #include "services/anilist.h" |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
2 #include "core/anime.h" |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
3 #include "core/anime_db.h" |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
4 #include "core/date.h" |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
5 #include "core/config.h" |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
6 #include "core/http.h" |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
7 #include "core/json.h" |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
8 #include "core/session.h" |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
9 #include "core/strings.h" |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
10 #include "core/time.h" |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
11 #include "gui/translate/anilist.h" |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
12 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
13 #include <QByteArray> |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
14 #include <QDate> |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
15 #include <QDesktopServices> |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
16 #include <QInputDialog> |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
17 #include <QLineEdit> |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
18 #include <QMessageBox> |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
19 #include <QUrl> |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
20 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
21 #include <chrono> |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
22 #include <exception> |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
23 #include <string_view> |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
24 |
334
948955c3ba81
services: use fmt for setting the status bar
Paper <paper@paper.us.eu.org>
parents:
327
diff
changeset
|
25 #include <fmt/core.h> |
948955c3ba81
services: use fmt for setting the status bar
Paper <paper@paper.us.eu.org>
parents:
327
diff
changeset
|
26 |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
27 #include <iostream> |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
28 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
29 using namespace nlohmann::literals::json_literals; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
30 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
31 static constexpr std::string_view CLIENT_ID = "dd031b32d2f56c990b1425efe6c42ad847e7fe3ab46bf1299f05ecd856bdb7dd"; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
32 static constexpr std::string_view CLIENT_SECRET = "54d7307928f63414defd96399fc31ba847961ceaecef3a5fd93144e960c0e151"; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
33 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
34 static constexpr std::string_view BASE_API_PATH = "https://kitsu.io/api/edge"; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
35 static constexpr std::string_view OAUTH_PATH = "https://kitsu.io/api/oauth/token"; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
36 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
37 namespace Services { |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
38 namespace Kitsu { |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
39 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
40 /* This nifty little function basically handles authentication AND reauthentication. */ |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
41 static bool SendAuthRequest(const nlohmann::json& data) { |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
42 static const std::vector<std::string> headers = { |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
43 {"Content-Type: application/json"} |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
44 }; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
45 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
46 const std::string ret = Strings::ToUtf8String(HTTP::Request(std::string(OAUTH_PATH), headers, data.dump(), HTTP::Type::Post)); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
47 if (ret.empty()) { |
334
948955c3ba81
services: use fmt for setting the status bar
Paper <paper@paper.us.eu.org>
parents:
327
diff
changeset
|
48 session.SetStatusBar(Strings::Translate("Kitsu: Request returned empty data!")); |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
49 return false; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
50 } |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
51 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
52 nlohmann::json result; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
53 try { |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
54 result = nlohmann::json::parse(ret, nullptr, false); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
55 } catch (const std::exception& ex) { |
334
948955c3ba81
services: use fmt for setting the status bar
Paper <paper@paper.us.eu.org>
parents:
327
diff
changeset
|
56 session.SetStatusBar(fmt::format(Strings::Translate("Kitsu: Failed to parse authorization data with error \"{}\""), ex.what())); |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
57 return false; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
58 } |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
59 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
60 if (result.contains("/error"_json_pointer)) { |
334
948955c3ba81
services: use fmt for setting the status bar
Paper <paper@paper.us.eu.org>
parents:
327
diff
changeset
|
61 session.SetStatusBar(fmt::format(Strings::Translate("Kitsu: Failed with error \"{}\"!"), result["/error"_json_pointer].get<std::string>())); |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
62 return false; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
63 } |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
64 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
65 const std::vector<nlohmann::json::json_pointer> required = { |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
66 "/access_token"_json_pointer, |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
67 "/created_at"_json_pointer, |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
68 "/expires_in"_json_pointer, |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
69 "/refresh_token"_json_pointer, |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
70 "/scope"_json_pointer, |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
71 "/token_type"_json_pointer |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
72 }; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
73 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
74 for (const auto& ptr : required) { |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
75 if (!result.contains(ptr)) { |
334
948955c3ba81
services: use fmt for setting the status bar
Paper <paper@paper.us.eu.org>
parents:
327
diff
changeset
|
76 session.SetStatusBar(Strings::Translate("Kitsu: Authorization request returned bad data!")); |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
77 return false; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
78 } |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
79 } |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
80 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
81 session.config.auth.kitsu.access_token = result["/access_token"_json_pointer].get<std::string>(); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
82 session.config.auth.kitsu.access_token_expiration |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
83 = result["/created_at"_json_pointer].get<Time::Timestamp>(); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
84 + result["/expires_in"_json_pointer].get<Time::Timestamp>(); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
85 session.config.auth.kitsu.refresh_token = result["/refresh_token"_json_pointer].get<std::string>(); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
86 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
87 /* the next two are not that important */ |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
88 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
89 return true; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
90 } |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
91 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
92 static bool RefreshAccessToken(std::string& access_token, const std::string& refresh_token) { |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
93 const nlohmann::json request = { |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
94 {"grant_type", "refresh_token"}, |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
95 {"refresh_token", refresh_token} |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
96 }; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
97 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
98 if (!SendAuthRequest(request)) |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
99 return false; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
100 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
101 return true; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
102 } |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
103 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
104 /* ----------------------------------------------------------------------------- */ |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
105 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
106 static std::optional<std::string> AccountAccessToken() { |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
107 auto& auth = session.config.auth.kitsu; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
108 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
109 if (Time::GetSystemTime() >= session.config.auth.kitsu.access_token_expiration) |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
110 if (!RefreshAccessToken(auth.access_token, auth.refresh_token)) |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
111 return std::nullopt; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
112 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
113 return auth.access_token; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
114 } |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
115 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
116 /* ----------------------------------------------------------------------------- */ |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
117 |
320
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
118 static void AddAnimeFilters(std::map<std::string, std::string>& map) { |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
119 static const std::vector<std::string> fields = { |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
120 "abbreviatedTitles", |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
121 "averageRating", |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
122 "episodeCount", |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
123 "episodeLength", |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
124 "posterImage", |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
125 "startDate", |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
126 "status", |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
127 "subtype", |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
128 "titles", |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
129 "categories", |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
130 "synopsis", |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
131 "animeProductions", |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
132 }; |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
133 static const std::string imploded = Strings::Implode(fields, ","); |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
134 |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
135 map["fields[anime]"] = imploded; |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
136 map["fields[animeProductions]"] = "producer"; |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
137 map["fields[categories]"] = "title"; |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
138 map["fields[producers]"] = "name"; |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
139 } |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
140 |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
141 static void AddLibraryEntryFilters(std::map<std::string, std::string>& map) { |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
142 static const std::vector<std::string> fields = { |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
143 "anime", |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
144 "startedAt", |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
145 "finishedAt", |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
146 "notes", |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
147 "progress", |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
148 "ratingTwenty", |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
149 "reconsumeCount", |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
150 "reconsuming", |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
151 "status", |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
152 "updatedAt", |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
153 }; |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
154 static const std::string imploded = Strings::Implode(fields, ","); |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
155 |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
156 map["fields[libraryEntries]"] = imploded; |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
157 } |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
158 |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
159 /* ----------------------------------------------------------------------------- */ |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
160 |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
161 static std::optional<nlohmann::json> SendJSONAPIRequest(const std::string& path, const std::map<std::string, std::string>& params = {}) { |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
162 std::optional<std::string> token = AccountAccessToken(); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
163 if (!token) |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
164 return std::nullopt; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
165 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
166 const std::vector<std::string> headers = { |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
167 "Accept: application/vnd.api+json", |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
168 "Authorization: Bearer " + token.value(), |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
169 "Content-Type: application/vnd.api+json" |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
170 }; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
171 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
172 const std::string url = HTTP::EncodeParamsList(std::string(BASE_API_PATH) + path, params); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
173 |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
174 const std::string response = Strings::ToUtf8String(HTTP::Request(url, headers, "", HTTP::Type::Get)); |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
175 if (response.empty()) |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
176 return std::nullopt; |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
177 |
320
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
178 nlohmann::json json; |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
179 try { |
320
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
180 json = nlohmann::json::parse(response); |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
181 } catch (const std::exception& ex) { |
334
948955c3ba81
services: use fmt for setting the status bar
Paper <paper@paper.us.eu.org>
parents:
327
diff
changeset
|
182 session.SetStatusBar(fmt::format(Strings::Translate("Kitsu: Failed to parse response with error \"{}\""), ex.what())); |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
183 return std::nullopt; |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
184 } |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
185 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
186 if (json.contains("/errors"_json_pointer)) { |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
187 for (const auto& item : json["/errors"]) |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
188 std::cerr << "Kitsu: API returned error \"" << json["/errors/title"_json_pointer] << "\" with detail \"" << json["/errors/detail"] << std::endl; |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
189 |
334
948955c3ba81
services: use fmt for setting the status bar
Paper <paper@paper.us.eu.org>
parents:
327
diff
changeset
|
190 session.SetStatusBar(Strings::Translate("Kitsu: Request failed with errors!")); |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
191 return std::nullopt; |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
192 } |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
193 |
320
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
194 return json; |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
195 } |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
196 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
197 static void ParseTitleJson(Anime::Anime& anime, const nlohmann::json& json) { |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
198 static const std::map<std::string, Anime::TitleLanguage> lookup = { |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
199 {"en", Anime::TitleLanguage::English}, |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
200 {"en_jp", Anime::TitleLanguage::Romaji}, |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
201 {"ja_jp", Anime::TitleLanguage::Native} |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
202 }; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
203 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
204 for (const auto& [string, title] : lookup) |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
205 if (json.contains(string)) |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
206 anime.SetTitle(title, json[string].get<std::string>()); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
207 } |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
208 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
209 static void ParseSubtype(Anime::Anime& anime, const std::string& str) { |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
210 static const std::map<std::string, Anime::SeriesFormat> lookup = { |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
211 {"ONA", Anime::SeriesFormat::Ona}, |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
212 {"OVA", Anime::SeriesFormat::Ova}, |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
213 {"TV", Anime::SeriesFormat::Tv}, |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
214 {"movie", Anime::SeriesFormat::Movie}, |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
215 {"music", Anime::SeriesFormat::Music}, |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
216 {"special", Anime::SeriesFormat::Special} |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
217 }; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
218 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
219 if (lookup.find(str) == lookup.end()) |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
220 return; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
221 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
222 anime.SetFormat(lookup.at(str)); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
223 } |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
224 |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
225 static void ParseListStatus(Anime::Anime& anime, const std::string& str) { |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
226 static const std::map<std::string, Anime::ListStatus> lookup = { |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
227 {"completed", Anime::ListStatus::Completed}, |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
228 {"current", Anime::ListStatus::Current}, |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
229 {"dropped", Anime::ListStatus::Dropped}, |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
230 {"on_hold", Anime::ListStatus::Paused}, |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
231 {"planned", Anime::ListStatus::Planning} |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
232 }; |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
233 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
234 if (lookup.find(str) == lookup.end()) |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
235 return; |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
236 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
237 anime.SetUserStatus(lookup.at(str)); |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
238 } |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
239 |
320
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
240 static void ParseSeriesStatus(Anime::Anime& anime, const std::string& str) { |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
241 static const std::map<std::string, Anime::SeriesStatus> lookup = { |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
242 {"current", Anime::SeriesStatus::Releasing}, |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
243 {"finished", Anime::SeriesStatus::Finished}, |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
244 {"tba", Anime::SeriesStatus::Hiatus}, // is this right? |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
245 {"unreleased", Anime::SeriesStatus::Cancelled}, |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
246 {"upcoming", Anime::SeriesStatus::NotYetReleased}, |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
247 }; |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
248 |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
249 if (lookup.find(str) == lookup.end()) |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
250 return; |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
251 |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
252 anime.SetAiringStatus(lookup.at(str)); |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
253 } |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
254 |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
255 static int ParseAnimeJson(const nlohmann::json& json) { |
334
948955c3ba81
services: use fmt for setting the status bar
Paper <paper@paper.us.eu.org>
parents:
327
diff
changeset
|
256 const std::string FAILED_TO_PARSE = Strings::Translate("Kitsu: Failed to parse anime object! {}"); |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
257 |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
258 const std::string service_id = json["/id"_json_pointer].get<std::string>(); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
259 if (service_id.empty()) { |
334
948955c3ba81
services: use fmt for setting the status bar
Paper <paper@paper.us.eu.org>
parents:
327
diff
changeset
|
260 session.SetStatusBar(fmt::format(FAILED_TO_PARSE, "(/id)")); |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
261 return 0; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
262 } |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
263 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
264 if (!json.contains("/attributes"_json_pointer)) { |
334
948955c3ba81
services: use fmt for setting the status bar
Paper <paper@paper.us.eu.org>
parents:
327
diff
changeset
|
265 session.SetStatusBar(fmt::format(FAILED_TO_PARSE, "(/attributes)")); |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
266 return 0; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
267 } |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
268 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
269 const auto& attributes = json["/attributes"_json_pointer]; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
270 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
271 int id = Anime::db.LookupServiceIdOrUnused(Anime::Service::Kitsu, service_id); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
272 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
273 Anime::Anime& anime = Anime::db.items[id]; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
274 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
275 anime.SetId(id); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
276 anime.SetServiceId(Anime::Service::Kitsu, service_id); |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
277 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
278 if (attributes.contains("/synopsis"_json_pointer) && attributes["/synopsis"_json_pointer].is_string()) |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
279 anime.SetSynopsis(attributes["/synopsis"_json_pointer].get<std::string>()); |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
280 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
281 if (attributes.contains("/titles"_json_pointer) && attributes["/titles"_json_pointer].is_object()) |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
282 ParseTitleJson(anime, attributes["/titles"_json_pointer]); |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
283 |
320
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
284 if (attributes.contains("/abbreviatedTitles"_json_pointer) && attributes["/abbreviatedTitles"_json_pointer].is_array()) |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
285 for (const auto& title : attributes["/abbreviatedTitles"_json_pointer]) |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
286 anime.AddTitleSynonym(title.get<std::string>()); |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
287 |
320
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
288 if (attributes.contains("/averageRating"_json_pointer) && attributes["/averageRating"_json_pointer].is_string()) |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
289 anime.SetAudienceScore(Strings::ToInt<double>(attributes["/averageRating"_json_pointer].get<std::string>())); |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
290 |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
291 if (attributes.contains("/startDate"_json_pointer) && attributes["/startDate"_json_pointer].is_string()) |
324
5d3c9b31aa6e
anime: add completed date member
Paper <paper@paper.us.eu.org>
parents:
321
diff
changeset
|
292 anime.SetStartedDate(attributes["/startDate"_json_pointer].get<std::string>()); |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
293 |
325
78929794e7d8
pages/seasons: run seasons search in a separate thread
Paper <paper@paper.us.eu.org>
parents:
324
diff
changeset
|
294 anime.SetCompletedDate(attributes.contains("/endDate"_json_pointer) && attributes["/endDate"_json_pointer].is_string() |
78929794e7d8
pages/seasons: run seasons search in a separate thread
Paper <paper@paper.us.eu.org>
parents:
324
diff
changeset
|
295 ? attributes["/endDate"_json_pointer].get<std::string>() |
78929794e7d8
pages/seasons: run seasons search in a separate thread
Paper <paper@paper.us.eu.org>
parents:
324
diff
changeset
|
296 : anime.GetStartedDate()); |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
297 |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
298 if (attributes.contains("/subtype"_json_pointer) && attributes["/subtype"_json_pointer].is_string()) |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
299 ParseSubtype(anime, attributes["/subtype"_json_pointer].get<std::string>()); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
300 |
320
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
301 if (attributes.contains("/status"_json_pointer) && attributes["/status"_json_pointer].is_string()) |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
302 ParseSeriesStatus(anime, attributes["/status"_json_pointer].get<std::string>()); |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
303 |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
304 if (attributes.contains("/posterImage/original"_json_pointer) && attributes["/posterImage/original"_json_pointer].is_string()) |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
305 anime.SetPosterUrl(attributes["/posterImage/original"_json_pointer].get<std::string>()); |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
306 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
307 if (attributes.contains("/episodeCount"_json_pointer) && attributes["/episodeCount"_json_pointer].is_number()) |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
308 anime.SetEpisodes(attributes["/episodeCount"_json_pointer].get<int>()); |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
309 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
310 if (attributes.contains("/episodeLength"_json_pointer) && attributes["/episodeLength"_json_pointer].is_number()) |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
311 anime.SetDuration(attributes["/episodeLength"_json_pointer].get<int>()); |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
312 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
313 return id; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
314 } |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
315 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
316 static int ParseLibraryJson(const nlohmann::json& json) { |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
317 static const std::vector<nlohmann::json::json_pointer> required = { |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
318 "/id"_json_pointer, |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
319 "/relationships/anime/data/id"_json_pointer, |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
320 "/attributes"_json_pointer, |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
321 }; |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
322 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
323 for (const auto& ptr : required) { |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
324 if (!json.contains(ptr)) { |
334
948955c3ba81
services: use fmt for setting the status bar
Paper <paper@paper.us.eu.org>
parents:
327
diff
changeset
|
325 session.SetStatusBar(fmt::format(Strings::Translate("Kitsu: Failed to parse library object! (missing {})"), ptr.to_string())); |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
326 return 0; |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
327 } |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
328 } |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
329 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
330 std::string service_id = json["/relationships/anime/data/id"_json_pointer].get<std::string>(); |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
331 if (service_id.empty()) { |
334
948955c3ba81
services: use fmt for setting the status bar
Paper <paper@paper.us.eu.org>
parents:
327
diff
changeset
|
332 session.SetStatusBar(Strings::Translate("Kitsu: Failed to parse library object (missing service ID)!")); |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
333 return 0; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
334 } |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
335 |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
336 int id = Anime::db.LookupServiceIdOrUnused(Anime::Service::Kitsu, service_id); |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
337 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
338 const auto& attributes = json["/attributes"_json_pointer]; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
339 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
340 const std::string library_id = json["/id"_json_pointer].get<std::string>(); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
341 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
342 Anime::Anime& anime = Anime::db.items[id]; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
343 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
344 anime.AddToUserList(); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
345 |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
346 anime.SetId(id); |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
347 anime.SetServiceId(Anime::Service::Kitsu, service_id); |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
348 |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
349 anime.SetUserId(library_id); |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
350 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
351 if (attributes.contains("/startedAt"_json_pointer) && attributes["/startedAt"_json_pointer].is_string()) |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
352 anime.SetUserDateStarted(Date(Time::ParseISO8601Time(attributes["/startedAt"_json_pointer].get<std::string>()))); |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
353 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
354 if (attributes.contains("/finishedAt"_json_pointer) && attributes["/finishedAt"_json_pointer].is_string()) |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
355 anime.SetUserDateCompleted(Date(Time::ParseISO8601Time(attributes["/finishedAt"_json_pointer].get<std::string>()))); |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
356 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
357 if (attributes.contains("/notes"_json_pointer) && attributes["/notes"_json_pointer].is_string()) |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
358 anime.SetUserNotes(attributes["/notes"_json_pointer].get<std::string>()); |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
359 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
360 if (attributes.contains("/progress"_json_pointer) && attributes["/progress"_json_pointer].is_number()) |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
361 anime.SetUserProgress(attributes["/progress"_json_pointer].get<int>()); |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
362 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
363 if (attributes.contains("/ratingTwenty"_json_pointer) && attributes["/ratingTwenty"_json_pointer].is_number()) |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
364 anime.SetUserScore(attributes["/ratingTwenty"_json_pointer].get<int>() * 5); |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
365 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
366 if (attributes.contains("/private"_json_pointer) && attributes["/private"_json_pointer].is_boolean()) |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
367 anime.SetUserIsPrivate(attributes["/private"_json_pointer].get<bool>()); |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
368 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
369 if (attributes.contains("/reconsumeCount"_json_pointer) && attributes["/reconsumeCount"_json_pointer].is_number()) |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
370 anime.SetUserRewatchedTimes(attributes["/reconsumeCount"_json_pointer].get<int>()); |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
371 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
372 if (attributes.contains("/reconsuming"_json_pointer) && attributes["/reconsuming"_json_pointer].is_boolean()) |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
373 anime.SetUserIsRewatching(attributes["/reconsuming"_json_pointer].get<bool>()); /* lmfao "reconsuming" */ |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
374 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
375 if (attributes.contains("/status"_json_pointer) && attributes["/status"_json_pointer].is_string()) |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
376 ParseListStatus(anime, attributes["/status"_json_pointer].get<std::string>()); |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
377 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
378 if (attributes.contains("/progressedAt"_json_pointer) && attributes["/progressedAt"_json_pointer].is_string()) |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
379 anime.SetUserTimeUpdated(Time::ParseISO8601Time(attributes["/progressedAt"_json_pointer].get<std::string>())); |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
380 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
381 return id; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
382 } |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
383 |
320
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
384 static void ParseMetadataJson(Anime::Anime& anime, const nlohmann::json& json) { |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
385 std::vector<std::string> categories; |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
386 std::vector<std::string> producers; |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
387 |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
388 for (const auto& item : json) { |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
389 std::string variant; |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
390 { |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
391 static const nlohmann::json::json_pointer p = "/type"_json_pointer; |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
392 |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
393 if (!item.contains(p) || !item[p].is_string()) |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
394 continue; |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
395 |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
396 variant = item[p].get<std::string>(); |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
397 } |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
398 |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
399 /* now parse variants */ |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
400 if (variant == "categories") { |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
401 static const nlohmann::json::json_pointer p = "/attributes/title"_json_pointer; |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
402 |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
403 if (!item.contains(p) || !item[p].is_string()) |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
404 continue; |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
405 |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
406 categories.push_back(item[p].get<std::string>()); |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
407 } else if (variant == "producers") { |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
408 static const nlohmann::json::json_pointer p = "/attributes/name"_json_pointer; |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
409 |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
410 if (!item.contains(p) || !item[p].is_string()) |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
411 continue; |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
412 |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
413 producers.push_back(item[p].get<std::string>()); |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
414 } |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
415 } |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
416 |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
417 anime.SetGenres(categories); |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
418 anime.SetProducers(producers); |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
419 } |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
420 |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
421 static bool ParseAnyJson(const nlohmann::json& json) { |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
422 static const nlohmann::json::json_pointer required = "/type"_json_pointer; |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
423 if (!json.contains(required) && !json[required].is_string()) { |
334
948955c3ba81
services: use fmt for setting the status bar
Paper <paper@paper.us.eu.org>
parents:
327
diff
changeset
|
424 session.SetStatusBar(fmt::format(Strings::Translate("Kitsu: Failed to generic object! (missing {})"), required.to_string())); |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
425 return 0; |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
426 } |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
427 |
320
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
428 std::string variant = json["/type"_json_pointer].get<std::string>(); |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
429 |
320
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
430 if (variant == "anime") { |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
431 return !!ParseAnimeJson(json); |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
432 } else if (variant == "libraryEntries") { |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
433 return !!ParseLibraryJson(json); |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
434 } else if (variant == "categories" || variant == "producers") { |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
435 /* do nothing */ |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
436 } else { |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
437 std::cerr << "Kitsu: received unknown type " << variant << std::endl; |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
438 } |
320
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
439 |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
440 return true; |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
441 } |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
442 |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
443 int GetAnimeList() { |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
444 static constexpr int LIBRARY_MAX_SIZE = 500; |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
445 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
446 const auto& auth = session.config.auth.kitsu; |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
447 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
448 if (auth.user_id.empty()) { |
334
948955c3ba81
services: use fmt for setting the status bar
Paper <paper@paper.us.eu.org>
parents:
327
diff
changeset
|
449 session.SetStatusBar(Strings::Translate("Kitsu: User ID is unavailable!")); |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
450 return 0; |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
451 } |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
452 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
453 int page = 0; |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
454 bool have_next_page = true; |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
455 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
456 std::map<std::string, std::string> params = { |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
457 {"filter[user_id]", auth.user_id}, |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
458 {"filter[kind]", "anime"}, |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
459 {"include", "anime"}, |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
460 {"page[offset]", Strings::ToUtf8String(page)}, |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
461 {"page[limit]", Strings::ToUtf8String(LIBRARY_MAX_SIZE)} |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
462 }; |
320
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
463 AddAnimeFilters(params); |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
464 AddLibraryEntryFilters(params); |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
465 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
466 Anime::db.RemoveAllUserData(); |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
467 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
468 bool success = true; |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
469 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
470 while (have_next_page) { |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
471 std::optional<nlohmann::json> response = SendJSONAPIRequest("/library-entries", params); |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
472 if (!response) |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
473 return 0; |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
474 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
475 const nlohmann::json& root = response.value(); |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
476 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
477 if (root.contains("/next"_json_pointer) && root["/next"_json_pointer].is_number()) { |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
478 page += root["/next"_json_pointer].get<int>(); |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
479 if (page <= 0) |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
480 have_next_page = false; |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
481 } else have_next_page = false; |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
482 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
483 for (const auto& item : root["/data"_json_pointer]) |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
484 if (!ParseLibraryJson(item)) |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
485 success = false; |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
486 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
487 for (const auto& variant : root["/included"_json_pointer]) |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
488 if (!ParseAnyJson(variant)) |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
489 success = false; |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
490 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
491 params["page[offset]"] = Strings::ToUtf8String(page); |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
492 } |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
493 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
494 if (success) |
334
948955c3ba81
services: use fmt for setting the status bar
Paper <paper@paper.us.eu.org>
parents:
327
diff
changeset
|
495 session.SetStatusBar(Strings::Translate("Kitsu: Successfully received library data!")); |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
496 |
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
497 return 1; |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
498 } |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
499 |
320
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
500 bool RetrieveAnimeMetadata(int id) { |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
501 /* TODO: the genres should *probably* be a std::optional */ |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
502 Anime::Anime& anime = Anime::db.items[id]; |
321
8141f409d52c
services/anilist: fix getting producers
Paper <paper@paper.us.eu.org>
parents:
320
diff
changeset
|
503 if (anime.GetGenres().size() > 0 && anime.GetProducers().size()) |
320
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
504 return false; |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
505 |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
506 std::optional<std::string> service_id = anime.GetServiceId(Anime::Service::Kitsu); |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
507 if (!service_id) |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
508 return false; |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
509 |
334
948955c3ba81
services: use fmt for setting the status bar
Paper <paper@paper.us.eu.org>
parents:
327
diff
changeset
|
510 session.SetStatusBar(Strings::Translate("Kitsu: Retrieving anime metadata...")); |
320
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
511 |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
512 static const std::map<std::string, std::string> params = { |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
513 {"include", Strings::Implode({ |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
514 "categories", |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
515 "animeProductions", |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
516 "animeProductions.producer", |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
517 }, ",")} |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
518 }; |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
519 |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
520 std::optional<nlohmann::json> response = SendJSONAPIRequest("/anime/" + service_id.value(), params); |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
521 if (!response) |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
522 return false; |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
523 |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
524 const auto& json = response.value(); |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
525 |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
526 if (!json.contains("/included"_json_pointer) || !json["/included"_json_pointer].is_array()) { |
334
948955c3ba81
services: use fmt for setting the status bar
Paper <paper@paper.us.eu.org>
parents:
327
diff
changeset
|
527 session.SetStatusBar(Strings::Translate("Kitsu: Server returned bad data when trying to retrieve anime metadata!")); |
320
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
528 return false; |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
529 } |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
530 |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
531 ParseMetadataJson(anime, json["/included"_json_pointer]); |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
532 |
334
948955c3ba81
services: use fmt for setting the status bar
Paper <paper@paper.us.eu.org>
parents:
327
diff
changeset
|
533 session.SetStatusBar(Strings::Translate("Kitsu: Successfully retrieved anime metadata!")); |
320
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
534 |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
535 return true; |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
536 } |
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
319
diff
changeset
|
537 |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
538 /* unimplemented for now */ |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
539 std::vector<int> Search(const std::string& search) { |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
540 return {}; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
541 } |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
542 |
327
b5d6c27c308f
anime: refactor Anime::SeriesSeason to Season class
Paper <paper@paper.us.eu.org>
parents:
325
diff
changeset
|
543 bool GetSeason(Anime::Season season) { |
325
78929794e7d8
pages/seasons: run seasons search in a separate thread
Paper <paper@paper.us.eu.org>
parents:
324
diff
changeset
|
544 return false; |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
545 } |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
546 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
547 int UpdateAnimeEntry(int id) { |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
548 return 0; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
549 } |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
550 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
551 bool AuthorizeUser(const std::string& email, const std::string& password) { |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
552 const nlohmann::json body = { |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
553 {"grant_type", "password"}, |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
554 {"username", email}, |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
555 {"password", HTTP::UrlEncode(password)} |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
556 }; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
557 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
558 if (!SendAuthRequest(body)) |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
559 return false; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
560 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
561 static const std::map<std::string, std::string> params = { |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
562 {"filter[self]", "true"} |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
563 }; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
564 |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
565 std::optional<nlohmann::json> response = SendJSONAPIRequest("/users", params); |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
566 if (!response) |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
567 return false; // whuh? |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
568 |
319
d928ec7b6a0d
services/kitsu: implement GetAnimeList()
Paper <paper@paper.us.eu.org>
parents:
317
diff
changeset
|
569 const nlohmann::json& json = response.value(); |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
570 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
571 if (!json.contains("/data/0/id"_json_pointer)) { |
334
948955c3ba81
services: use fmt for setting the status bar
Paper <paper@paper.us.eu.org>
parents:
327
diff
changeset
|
572 session.SetStatusBar(Strings::Translate("Kitsu: Failed to retrieve user ID!")); |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
573 return false; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
574 } |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
575 |
334
948955c3ba81
services: use fmt for setting the status bar
Paper <paper@paper.us.eu.org>
parents:
327
diff
changeset
|
576 session.SetStatusBar(Strings::Translate("Kitsu: Successfully authorized user!")); |
317
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
577 session.config.auth.kitsu.user_id = json["/data/0/id"_json_pointer].get<std::string>(); |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
578 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
579 return true; |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
580 } |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
581 |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
582 } // namespace Kitsu |
b1f4d1867ab1
services: VERY initial Kitsu support
Paper <paper@paper.us.eu.org>
parents:
diff
changeset
|
583 } // namespace Services |