Mercurial > minori
comparison src/services/anilist.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 | f81bed4e04ac |
comparison
equal
deleted
inserted
replaced
333:5980a960f3e1 | 334:948955c3ba81 |
---|---|
18 #include <QUrl> | 18 #include <QUrl> |
19 | 19 |
20 #include <chrono> | 20 #include <chrono> |
21 #include <exception> | 21 #include <exception> |
22 #include <string_view> | 22 #include <string_view> |
23 | |
24 #include <fmt/core.h> | |
23 | 25 |
24 #include <iostream> | 26 #include <iostream> |
25 | 27 |
26 namespace Services { | 28 namespace Services { |
27 namespace AniList { | 29 namespace AniList { |
73 return (auth.user_id && !auth.auth_token.empty()); | 75 return (auth.user_id && !auth.auth_token.empty()); |
74 } | 76 } |
75 | 77 |
76 static std::optional<nlohmann::json> SendJSONRequest(const nlohmann::json& data) { | 78 static std::optional<nlohmann::json> SendJSONRequest(const nlohmann::json& data) { |
77 if (!AccountIsValid()) { | 79 if (!AccountIsValid()) { |
78 session.SetStatusBar("AniList: Account isn't valid! (unauthorized?)"); | 80 session.SetStatusBar(Strings::Translate("AniList: Account isn't valid! (unauthorized?)")); |
79 return std::nullopt; | 81 return std::nullopt; |
80 } | 82 } |
81 | 83 |
82 const auto& auth = session.config.auth.anilist; | 84 const auto& auth = session.config.auth.anilist; |
83 | 85 |
87 "Content-Type: application/json", | 89 "Content-Type: application/json", |
88 }; | 90 }; |
89 | 91 |
90 const std::string response = Strings::ToUtf8String(HTTP::Request("https://graphql.anilist.co", headers, data.dump(), HTTP::Type::Post)); | 92 const std::string response = Strings::ToUtf8String(HTTP::Request("https://graphql.anilist.co", headers, data.dump(), HTTP::Type::Post)); |
91 if (response.empty()) { | 93 if (response.empty()) { |
92 session.SetStatusBar("AniList: JSON request returned an empty result!"); | 94 session.SetStatusBar(Strings::Translate("AniList: JSON request returned an empty result!")); |
93 return std::nullopt; | 95 return std::nullopt; |
94 } | 96 } |
95 | 97 |
96 nlohmann::json out; | 98 nlohmann::json out; |
97 | 99 |
98 try { | 100 try { |
99 out = nlohmann::json::parse(response); | 101 out = nlohmann::json::parse(response); |
100 } catch (const std::exception& ex) { | 102 } catch (const std::exception& ex) { |
101 session.SetStatusBar("AniList: Failed to parse request JSON with error!"); | 103 session.SetStatusBar(fmt::format(Strings::Translate("AniList: Failed to parse request JSON with error \"{}\"!"), ex.what())); |
102 return std::nullopt; | 104 return std::nullopt; |
103 } | 105 } |
104 | 106 |
105 if (out.contains("/errors"_json_pointer) && out.at("/errors"_json_pointer).is_array()) { | 107 if (out.contains("/errors"_json_pointer) && out.at("/errors"_json_pointer).is_array()) { |
106 for (const auto& error : out.at("/errors"_json_pointer)) | 108 for (const auto& error : out.at("/errors"_json_pointer)) |
107 std::cerr << "AniList: Received an error in response: " | 109 std::cerr << "AniList: Received an error in response: " |
108 << JSON::GetString<std::string>(error, "/message"_json_pointer, "") << std::endl; | 110 << JSON::GetString<std::string>(error, "/message"_json_pointer, "") << std::endl; |
109 | 111 |
110 session.SetStatusBar("AniList: Received an error in response!"); | 112 session.SetStatusBar(Strings::Translate("AniList: Received an error in response!")); |
111 return std::nullopt; | 113 return std::nullopt; |
112 } | 114 } |
113 | 115 |
114 return out; | 116 return out; |
115 } | 117 } |
163 anime.SetTitle(language, json[ptr]); | 165 anime.SetTitle(language, json[ptr]); |
164 } | 166 } |
165 | 167 |
166 static int ParseMediaJson(const nlohmann::json& json) { | 168 static int ParseMediaJson(const nlohmann::json& json) { |
167 if (!json.contains("/id"_json_pointer) || !json["/id"_json_pointer].is_number()) { | 169 if (!json.contains("/id"_json_pointer) || !json["/id"_json_pointer].is_number()) { |
168 session.SetStatusBar("AniList: Failed to parse anime object!"); | 170 session.SetStatusBar(Strings::Translate("AniList: Failed to parse anime object!")); |
169 return 0; | 171 return 0; |
170 } | 172 } |
171 | 173 |
172 std::string service_id = Strings::ToUtf8String(json["/id"_json_pointer].get<int>()); | 174 std::string service_id = Strings::ToUtf8String(json["/id"_json_pointer].get<int>()); |
173 | 175 |
174 int id = Anime::db.LookupServiceIdOrUnused(Anime::Service::AniList, service_id); | 176 int id = Anime::db.LookupServiceIdOrUnused(Anime::Service::AniList, service_id); |
175 if (!id) { | 177 if (!id) { |
176 session.SetStatusBar("AniList: Failed to parse anime object!"); | 178 session.SetStatusBar(Strings::Translate("AniList: Failed to parse anime object!")); |
177 return 0; | 179 return 0; |
178 } | 180 } |
179 | 181 |
180 Anime::Anime& anime = Anime::db.items[id]; | 182 Anime::Anime& anime = Anime::db.items[id]; |
181 anime.SetId(id); | 183 anime.SetId(id); |
297 {"id", auth.user_id} | 299 {"id", auth.user_id} |
298 }} | 300 }} |
299 }; | 301 }; |
300 // clang-format on | 302 // clang-format on |
301 | 303 |
302 session.SetStatusBar("AniList: Parsing anime list..."); | 304 session.SetStatusBar(Strings::Translate("AniList: Parsing anime list...")); |
303 | 305 |
304 const std::optional<nlohmann::json> response = SendJSONRequest(request); | 306 const std::optional<nlohmann::json> response = SendJSONRequest(request); |
305 if (!response) | 307 if (!response) |
306 return 0; | 308 return 0; |
307 | 309 |
314 for (const auto& list : json["data"]["MediaListCollection"]["lists"].items()) | 316 for (const auto& list : json["data"]["MediaListCollection"]["lists"].items()) |
315 if (!ParseList(list.value())) | 317 if (!ParseList(list.value())) |
316 success = false; | 318 success = false; |
317 | 319 |
318 if (success) | 320 if (success) |
319 session.SetStatusBar("AniList: Retrieved anime list successfully!"); | 321 session.SetStatusBar(Strings::Translate("AniList: Retrieved anime list successfully!")); |
320 | 322 |
321 return 1; | 323 return 1; |
322 } | 324 } |
323 | 325 |
324 /* return is a vector of anime ids */ | 326 /* return is a vector of anime ids */ |
441 if (!res) | 443 if (!res) |
442 return 0; | 444 return 0; |
443 | 445 |
444 const nlohmann::json& result = res.value(); | 446 const nlohmann::json& result = res.value(); |
445 | 447 |
446 session.SetStatusBar("AniList: Anime entry updated successfully!"); | 448 session.SetStatusBar(Strings::Translate("AniList: Anime entry updated successfully!")); |
447 | 449 |
448 return JSON::GetNumber(result, "/data/SaveMediaListEntry/id"_json_pointer, 0); | 450 return JSON::GetNumber(result, "/data/SaveMediaListEntry/id"_json_pointer, 0); |
449 } | 451 } |
450 | 452 |
451 static int ParseUser(const nlohmann::json& json) { | 453 static int ParseUser(const nlohmann::json& json) { |
469 if (!ok || token.isEmpty()) | 471 if (!ok || token.isEmpty()) |
470 return false; | 472 return false; |
471 | 473 |
472 auth.auth_token = Strings::ToUtf8String(token); | 474 auth.auth_token = Strings::ToUtf8String(token); |
473 | 475 |
474 session.SetStatusBar("AniList: Requesting user ID..."); | 476 session.SetStatusBar(Strings::Translate("AniList: Requesting user ID...")); |
475 | 477 |
476 static constexpr std::string_view query = | 478 static constexpr std::string_view query = |
477 "query {\n" | 479 "query {\n" |
478 " Viewer {\n" | 480 " Viewer {\n" |
479 " id\n" | 481 " id\n" |
490 return 0; | 492 return 0; |
491 | 493 |
492 const nlohmann::json& result = ret.value(); | 494 const nlohmann::json& result = ret.value(); |
493 | 495 |
494 if (ParseUser(result["data"]["Viewer"])) | 496 if (ParseUser(result["data"]["Viewer"])) |
495 session.SetStatusBar("AniList: Successfully retrieved user data!"); | 497 session.SetStatusBar(Strings::Translate("AniList: Successfully retrieved user data!")); |
496 else | 498 else |
497 session.SetStatusBar("AniList: Failed to retrieve user ID!"); | 499 session.SetStatusBar(Strings::Translate("AniList: Failed to retrieve user ID!")); |
498 | 500 |
499 return true; | 501 return true; |
500 } | 502 } |
501 | 503 |
502 } // namespace AniList | 504 } // namespace AniList |