Mercurial > minori
annotate src/services/anilist.cc @ 312:11adae933b40
locale: remove old unfinished and probably bad spanish translation
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Tue, 11 Jun 2024 13:19:32 -0400 |
parents | 91ac90a34003 |
children | 34347fd2a2de |
rev | line source |
---|---|
9 | 1 #include "services/anilist.h" |
2 #include "core/anime.h" | |
10 | 3 #include "core/anime_db.h" |
304
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
4 #include "core/date.h" |
9 | 5 #include "core/config.h" |
76 | 6 #include "core/http.h" |
9 | 7 #include "core/json.h" |
8 #include "core/session.h" | |
9 #include "core/strings.h" | |
15 | 10 #include "gui/translate/anilist.h" |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
185
diff
changeset
|
11 |
258 | 12 #include <QByteArray> |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
185
diff
changeset
|
13 #include <QDate> |
9 | 14 #include <QDesktopServices> |
15 #include <QInputDialog> | |
16 #include <QLineEdit> | |
17 #include <QMessageBox> | |
10 | 18 #include <QUrl> |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
185
diff
changeset
|
19 |
9 | 20 #include <chrono> |
21 #include <exception> | |
291 | 22 #include <string_view> |
175 | 23 |
24 #include <iostream> | |
25 | |
64 | 26 using namespace nlohmann::literals::json_literals; |
11 | 27 |
63 | 28 namespace Services { |
29 namespace AniList { | |
9 | 30 |
291 | 31 static constexpr std::string_view CLIENT_ID = "13706"; |
304
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
32 #define MEDIA_FIELDS \ |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
33 "coverImage {\n" \ |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
34 " large\n" \ |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
35 "}\n" \ |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
36 "id\n" \ |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
37 "title {\n" \ |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
38 " romaji\n" \ |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
39 " english\n" \ |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
40 " native\n" \ |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
41 "}\n" \ |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
42 "format\n" \ |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
43 "status\n" \ |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
44 "averageScore\n" \ |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
45 "season\n" \ |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
46 "startDate {\n" \ |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
47 " year\n" \ |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
48 " month\n" \ |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
49 " day\n" \ |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
50 "}\n" \ |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
51 "genres\n" \ |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
52 "episodes\n" \ |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
53 "duration\n" \ |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
54 "synonyms\n" \ |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
55 "description(asHtml: false)\n" |
185
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
184
diff
changeset
|
56 |
9 | 57 class Account { |
258 | 58 public: |
59 int UserId() const { return session.config.auth.anilist.user_id; } | |
60 void SetUserId(const int id) { session.config.auth.anilist.user_id = id; } | |
9 | 61 |
258 | 62 std::string AuthToken() const { return session.config.auth.anilist.auth_token; } |
286
53e3c015a973
anime: initial cross-service support
Paper <paper@paper.us.eu.org>
parents:
284
diff
changeset
|
63 void SetAuthToken(const std::string& auth_token) { session.config.auth.anilist.auth_token = auth_token; } |
9 | 64 |
258 | 65 bool Authenticated() const { return !AuthToken().empty(); } |
66 bool IsValid() const { return UserId() && Authenticated(); } | |
10 | 67 }; |
9 | 68 |
69 static Account account; | |
70 | |
291 | 71 static std::string SendRequest(const std::string& data) { |
76 | 72 std::vector<std::string> headers = {"Authorization: Bearer " + account.AuthToken(), "Accept: application/json", |
258 | 73 "Content-Type: application/json"}; |
291 | 74 return Strings::ToUtf8String(HTTP::Request("https://graphql.anilist.co", headers, data, HTTP::Type::Post)); |
9 | 75 } |
76 | |
291 | 77 static nlohmann::json SendJSONRequest(const nlohmann::json& data) { |
175 | 78 std::string request = SendRequest(data.dump()); |
79 if (request.empty()) { | |
80 std::cerr << "[AniList] JSON Request returned an empty result!" << std::endl; | |
81 return {}; | |
82 } | |
83 | |
84 auto ret = nlohmann::json::parse(request, nullptr, false); | |
85 if (ret.is_discarded()) { | |
86 std::cerr << "[AniList] Failed to parse request JSON!" << std::endl; | |
87 return {}; | |
88 } | |
89 | |
90 if (ret.contains("/errors"_json_pointer) && ret.at("/errors"_json_pointer).is_array()) { | |
91 for (const auto& error : ret.at("/errors"_json_pointer)) | |
258 | 92 std::cerr << "[AniList] Received an error in response: " |
93 << JSON::GetString<std::string>(error, "/message"_json_pointer, "") << std::endl; | |
175 | 94 |
95 return {}; | |
96 } | |
97 | |
98 return ret; | |
99 } | |
100 | |
291 | 101 static void ParseListStatus(std::string status, Anime::Anime& anime) { |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
185
diff
changeset
|
102 static const std::unordered_map<std::string, Anime::ListStatus> map = { |
279 | 103 {"CURRENT", Anime::ListStatus::Current }, |
104 {"PLANNING", Anime::ListStatus::Planning }, | |
105 {"COMPLETED", Anime::ListStatus::Completed}, | |
106 {"DROPPED", Anime::ListStatus::Dropped }, | |
107 {"PAUSED", Anime::ListStatus::Paused } | |
258 | 108 }; |
9 | 109 |
15 | 110 if (status == "REPEATING") { |
111 anime.SetUserIsRewatching(true); | |
279 | 112 anime.SetUserStatus(Anime::ListStatus::Current); |
15 | 113 return; |
114 } | |
9 | 115 |
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
116 if (map.find(status) == map.end()) { |
279 | 117 anime.SetUserStatus(Anime::ListStatus::NotInList); |
15 | 118 return; |
119 } | |
9 | 120 |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
185
diff
changeset
|
121 anime.SetUserStatus(map.at(status)); |
15 | 122 } |
9 | 123 |
291 | 124 static std::string ListStatusToString(const Anime::Anime& anime) { |
279 | 125 if (anime.GetUserIsRewatching() && anime.GetUserStatus() == Anime::ListStatus::Current) |
15 | 126 return "REWATCHING"; |
127 | |
70
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
128 switch (anime.GetUserStatus()) { |
279 | 129 case Anime::ListStatus::Planning: return "PLANNING"; |
130 case Anime::ListStatus::Completed: return "COMPLETED"; | |
131 case Anime::ListStatus::Dropped: return "DROPPED"; | |
132 case Anime::ListStatus::Paused: return "PAUSED"; | |
76 | 133 default: break; |
70
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
134 } |
64e5f427c6a2
services/anilist: remove unordered_map usage for enum classes
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
135 return "CURRENT"; |
15 | 136 } |
9 | 137 |
291 | 138 static void ParseTitle(const nlohmann::json& json, Anime::Anime& anime) { |
305
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
304
diff
changeset
|
139 static const std::unordered_map<Anime::TitleLanguage, nlohmann::json::json_pointer> map = { |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
304
diff
changeset
|
140 {Anime::TitleLanguage::Native, "/native"_json_pointer}, |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
304
diff
changeset
|
141 {Anime::TitleLanguage::English, "/english"_json_pointer}, |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
304
diff
changeset
|
142 {Anime::TitleLanguage::Romaji, "/romaji"_json_pointer}, |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
304
diff
changeset
|
143 }; |
284
e66ffc338d82
anime: refactor title structure to a map
Paper <paper@paper.us.eu.org>
parents:
279
diff
changeset
|
144 |
305
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
304
diff
changeset
|
145 for (const auto& [language, ptr] : map) |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
304
diff
changeset
|
146 if (json.contains(ptr) && json[ptr].is_string()) |
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
304
diff
changeset
|
147 anime.SetTitle(language, json[ptr]); |
9 | 148 } |
149 | |
291 | 150 static int ParseMediaJson(const nlohmann::json& json) { |
175 | 151 int id = JSON::GetNumber(json, "/id"_json_pointer); |
9 | 152 if (!id) |
153 return 0; | |
175 | 154 |
9 | 155 Anime::Anime& anime = Anime::db.items[id]; |
156 anime.SetId(id); | |
286
53e3c015a973
anime: initial cross-service support
Paper <paper@paper.us.eu.org>
parents:
284
diff
changeset
|
157 anime.SetServiceId(Anime::Service::AniList, Strings::ToUtf8String(id)); |
53e3c015a973
anime: initial cross-service support
Paper <paper@paper.us.eu.org>
parents:
284
diff
changeset
|
158 anime.SetServiceId(Anime::Service::MyAnimeList, Strings::ToUtf8String(JSON::GetNumber(json, "/id_mal"_json_pointer))); |
9 | 159 |
11 | 160 ParseTitle(json.at("/title"_json_pointer), anime); |
9 | 161 |
175 | 162 anime.SetEpisodes(JSON::GetNumber(json, "/episodes"_json_pointer, 0)); |
163 anime.SetFormat(Translate::AniList::ToSeriesFormat(JSON::GetString<std::string>(json, "/format"_json_pointer, ""))); | |
9 | 164 |
258 | 165 anime.SetAiringStatus( |
166 Translate::AniList::ToSeriesStatus(JSON::GetString<std::string>(json, "/status"_json_pointer, ""))); | |
9 | 167 |
175 | 168 anime.SetAirDate(Date(json["/startDate"_json_pointer])); |
9 | 169 |
175 | 170 anime.SetPosterUrl(JSON::GetString<std::string>(json, "/coverImage/large"_json_pointer, "")); |
66
6481c5aed3e1
posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
65
diff
changeset
|
171 |
175 | 172 anime.SetAudienceScore(JSON::GetNumber(json, "/averageScore"_json_pointer, 0)); |
279 | 173 // anime.SetSeason(Translate::AniList::ToSeriesSeason(JSON::GetString<std::string>(json, "/season"_json_pointer, ""))); |
175 | 174 anime.SetDuration(JSON::GetNumber(json, "/duration"_json_pointer, 0)); |
260
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
175 |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
176 std::string synopsis = JSON::GetString<std::string>(json, "/description"_json_pointer, ""); |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
177 Strings::TextifySynopsis(synopsis); |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
178 anime.SetSynopsis(synopsis); |
9 | 179 |
175 | 180 anime.SetGenres(JSON::GetArray<std::vector<std::string>>(json, "/genres"_json_pointer, {})); |
181 anime.SetTitleSynonyms(JSON::GetArray<std::vector<std::string>>(json, "/synonyms"_json_pointer, {})); | |
182 | |
10 | 183 return id; |
9 | 184 } |
185 | |
291 | 186 static int ParseListItem(const nlohmann::json& json) { |
10 | 187 int id = ParseMediaJson(json["media"]); |
188 | |
189 Anime::Anime& anime = Anime::db.items[id]; | |
190 | |
191 anime.AddToUserList(); | |
9 | 192 |
175 | 193 anime.SetUserScore(JSON::GetNumber(json, "/score"_json_pointer, 0)); |
194 anime.SetUserProgress(JSON::GetNumber(json, "/progress"_json_pointer, 0)); | |
195 ParseListStatus(JSON::GetString<std::string>(json, "/status"_json_pointer, ""), anime); | |
196 anime.SetUserNotes(JSON::GetString<std::string>(json, "/notes"_json_pointer, "")); | |
9 | 197 |
175 | 198 anime.SetUserDateStarted(Date(json["/startedAt"_json_pointer])); |
199 anime.SetUserDateCompleted(Date(json["/completedAt"_json_pointer])); | |
9 | 200 |
175 | 201 anime.SetUserTimeUpdated(JSON::GetNumber(json, "/updatedAt"_json_pointer, 0)); |
10 | 202 |
203 return id; | |
9 | 204 } |
205 | |
291 | 206 static int ParseList(const nlohmann::json& json) { |
9 | 207 for (const auto& entry : json["entries"].items()) { |
208 ParseListItem(entry.value()); | |
209 } | |
10 | 210 return 1; |
9 | 211 } |
212 | |
10 | 213 int GetAnimeList() { |
175 | 214 if (!account.IsValid()) { |
215 std::cerr << "AniList: Account isn't valid!" << std::endl; | |
216 return 0; | |
217 } | |
218 | |
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
219 /* NOTE: these really ought to be in the qrc file */ |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
220 constexpr std::string_view query = "query ($id: Int) {\n" |
258 | 221 " MediaListCollection (userId: $id, type: ANIME) {\n" |
222 " lists {\n" | |
223 " name\n" | |
224 " entries {\n" | |
225 " score\n" | |
226 " notes\n" | |
227 " status\n" | |
228 " progress\n" | |
229 " startedAt {\n" | |
230 " year\n" | |
231 " month\n" | |
232 " day\n" | |
233 " }\n" | |
234 " completedAt {\n" | |
235 " year\n" | |
236 " month\n" | |
237 " day\n" | |
238 " }\n" | |
239 " updatedAt\n" | |
240 " media {\n" | |
304
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
241 MEDIA_FIELDS |
258 | 242 " }\n" |
243 " }\n" | |
244 " }\n" | |
245 " }\n" | |
246 "}\n"; | |
9 | 247 // clang-format off |
248 nlohmann::json json = { | |
249 {"query", query}, | |
250 {"variables", { | |
10 | 251 {"id", account.UserId()} |
9 | 252 }} |
253 }; | |
254 // clang-format on | |
175 | 255 |
256 auto res = SendJSONRequest(json); | |
257 | |
304
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
258 for (const auto& list : res["data"]["MediaListCollection"]["lists"].items()) |
10 | 259 ParseList(list.value()); |
304
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
260 |
9 | 261 return 1; |
262 } | |
263 | |
250 | 264 /* return is a vector of anime ids */ |
265 std::vector<int> Search(const std::string& search) { | |
258 | 266 constexpr std::string_view query = "query ($search: String) {\n" |
267 " Page (page: 1, perPage: 50) {\n" | |
268 " media (search: $search, type: ANIME) {\n" | |
304
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
269 MEDIA_FIELDS |
258 | 270 " }\n" |
271 " }\n" | |
272 "}\n"; | |
250 | 273 |
274 // clang-format off | |
275 nlohmann::json json = { | |
276 {"query", query}, | |
277 {"variables", { | |
278 {"search", search} | |
279 }} | |
280 }; | |
281 // clang-format on | |
282 | |
283 auto res = SendJSONRequest(json); | |
284 | |
304
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
285 /* FIXME: error handling here */ |
250 | 286 std::vector<int> ret; |
287 ret.reserve(res["data"]["Page"]["media"].size()); | |
288 | |
289 for (const auto& media : res["data"]["Page"]["media"].items()) | |
290 ret.push_back(ParseMediaJson(media.value())); | |
291 | |
292 return ret; | |
293 } | |
294 | |
304
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
295 std::vector<int> GetSeason(Anime::SeriesSeason season, Date::Year year) { |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
296 constexpr std::string_view query = "query ($season: MediaSeason!, $season_year: Int!, $page: Int) {\n" |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
297 " Page(page: $page) {\n" |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
298 " media(season: $season, seasonYear: $season_year, type: ANIME, sort: START_DATE) {\n" |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
299 MEDIA_FIELDS |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
300 " }\n" |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
301 " pageInfo {\n" |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
302 " total\n" |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
303 " perPage\n" |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
304 " currentPage\n" |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
305 " lastPage\n" |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
306 " hasNextPage\n" |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
307 " }\n" |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
308 " }\n" |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
309 "}\n"; |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
310 std::vector<int> ret; |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
311 |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
312 int page = 0; |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
313 bool has_next_page = true; |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
314 while (has_next_page) { |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
315 nlohmann::json json = { |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
316 {"query", query}, |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
317 {"variables", { |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
318 {"season", Translate::AniList::ToString(season)}, |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
319 {"season_year", Strings::ToUtf8String(year)}, |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
320 {"page", page} |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
321 }} |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
322 }; |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
323 |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
324 auto res = SendJSONRequest(json); |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
325 ret.reserve(ret.capacity() + res["data"]["Page"]["media"].size()); |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
326 |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
327 for (const auto& media : res["data"]["Page"]["media"].items()) |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
328 ret.push_back(ParseMediaJson(media.value())); |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
329 |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
330 has_next_page = JSON::GetBoolean(res, "/data/Page/pageInfo/hasNextPage"_json_pointer, false); |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
331 if (has_next_page) |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
332 page++; |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
333 } |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
334 |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
335 return ret; |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
336 } |
2115488eb302
*: add very early season searcher
Paper <paper@paper.us.eu.org>
parents:
291
diff
changeset
|
337 |
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
338 int UpdateAnimeEntry(int id) { |
9 | 339 /** |
340 * possible values: | |
15 | 341 * |
9 | 342 * int mediaId, |
343 * MediaListStatus status, | |
344 * float score, | |
345 * int scoreRaw, | |
346 * int progress, | |
184
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
347 * int progressVolumes, // manga-specific. |
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
348 * int repeat, // rewatch |
258 | 349 * int priority, |
9 | 350 * bool private, |
351 * string notes, | |
352 * bool hiddenFromStatusLists, | |
353 * string[] customLists, | |
354 * float[] advancedScores, | |
355 * Date startedAt, | |
356 * Date completedAt | |
258 | 357 **/ |
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
48
diff
changeset
|
358 Anime::Anime& anime = Anime::db.items[id]; |
184
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
359 if (!anime.IsInUserList()) |
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
360 return 0; |
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
361 |
291 | 362 std::optional<std::string> service_id = anime.GetServiceId(Anime::Service::AniList); |
363 if (!service_id) | |
364 return 0; | |
365 | |
250 | 366 constexpr std::string_view query = |
258 | 367 "mutation ($media_id: Int, $progress: Int, $status: MediaListStatus, $score: Int, $notes: String, $start: " |
368 "FuzzyDateInput, $comp: FuzzyDateInput, $repeat: Int) {\n" | |
369 " SaveMediaListEntry (mediaId: $media_id, progress: $progress, status: $status, scoreRaw: $score, notes: " | |
370 "$notes, startedAt: $start, completedAt: $comp, repeat: $repeat) {\n" | |
371 " id\n" | |
372 " }\n" | |
373 "}\n"; | |
9 | 374 // clang-format off |
375 nlohmann::json json = { | |
376 {"query", query}, | |
377 {"variables", { | |
291 | 378 {"media_id", Strings::ToInt<int64_t>(service_id.value())}, |
10 | 379 {"progress", anime.GetUserProgress()}, |
15 | 380 {"status", ListStatusToString(anime)}, |
10 | 381 {"score", anime.GetUserScore()}, |
77 | 382 {"notes", anime.GetUserNotes()}, |
383 {"start", anime.GetUserDateStarted().GetAsAniListJson()}, | |
184
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
384 {"comp", anime.GetUserDateCompleted().GetAsAniListJson()}, |
09492158bcc5
anime: etc. comments and changes
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
385 {"repeat", anime.GetUserRewatchedTimes()} |
9 | 386 }} |
387 }; | |
388 // clang-format on | |
175 | 389 |
390 auto ret = SendJSONRequest(json); | |
391 | |
392 return JSON::GetNumber(ret, "/data/SaveMediaListEntry/id"_json_pointer, 0); | |
9 | 393 } |
394 | |
291 | 395 static int ParseUser(const nlohmann::json& json) { |
175 | 396 account.SetUserId(JSON::GetNumber(json, "/id"_json_pointer, 0)); |
10 | 397 return account.UserId(); |
9 | 398 } |
399 | |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
400 bool AuthorizeUser() { |
9 | 401 /* Prompt for PIN */ |
258 | 402 QDesktopServices::openUrl(QUrl(Strings::ToQString("https://anilist.co/api/v2/oauth/authorize?client_id=" + |
291 | 403 std::string(CLIENT_ID) + "&response_type=token"))); |
175 | 404 |
9 | 405 bool ok; |
406 QString token = QInputDialog::getText( | |
258 | 407 0, "Credentials needed!", "Please enter the code given to you after logging in to AniList:", QLineEdit::Normal, |
408 "", &ok); | |
175 | 409 |
410 if (!ok || token.isEmpty()) | |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
411 return false; |
175 | 412 |
413 account.SetAuthToken(Strings::ToUtf8String(token)); | |
414 | |
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
175
diff
changeset
|
415 constexpr std::string_view query = "query {\n" |
258 | 416 " Viewer {\n" |
417 " id\n" | |
418 " name\n" | |
419 " mediaListOptions {\n" | |
420 " scoreFormat\n" // this will be used... eventually | |
421 " }\n" | |
422 " }\n" | |
423 "}\n"; | |
9 | 424 nlohmann::json json = { |
258 | 425 {"query", query} |
426 }; | |
175 | 427 |
428 auto ret = SendJSONRequest(json); | |
429 | |
74 | 430 ParseUser(ret["data"]["Viewer"]); |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
431 return true; |
9 | 432 } |
433 | |
63 | 434 } // namespace AniList |
435 } // namespace Services |