Mercurial > minori
comparison src/services/kitsu.cc @ 317:b1f4d1867ab1
services: VERY initial Kitsu support
it only supports user authentication for now, but it's definitely
a start.
| author | Paper <paper@paper.us.eu.org> |
|---|---|
| date | Wed, 12 Jun 2024 04:07:10 -0400 |
| parents | |
| children | d928ec7b6a0d |
comparison
equal
deleted
inserted
replaced
| 316:180714442770 | 317:b1f4d1867ab1 |
|---|---|
| 1 #include "services/anilist.h" | |
| 2 #include "core/anime.h" | |
| 3 #include "core/anime_db.h" | |
| 4 #include "core/date.h" | |
| 5 #include "core/config.h" | |
| 6 #include "core/http.h" | |
| 7 #include "core/json.h" | |
| 8 #include "core/session.h" | |
| 9 #include "core/strings.h" | |
| 10 #include "core/time.h" | |
| 11 #include "gui/translate/anilist.h" | |
| 12 | |
| 13 #include <QByteArray> | |
| 14 #include <QDate> | |
| 15 #include <QDesktopServices> | |
| 16 #include <QInputDialog> | |
| 17 #include <QLineEdit> | |
| 18 #include <QMessageBox> | |
| 19 #include <QUrl> | |
| 20 | |
| 21 #include <chrono> | |
| 22 #include <exception> | |
| 23 #include <string_view> | |
| 24 | |
| 25 #include <iostream> | |
| 26 | |
| 27 using namespace nlohmann::literals::json_literals; | |
| 28 | |
| 29 static constexpr std::string_view CLIENT_ID = "dd031b32d2f56c990b1425efe6c42ad847e7fe3ab46bf1299f05ecd856bdb7dd"; | |
| 30 static constexpr std::string_view CLIENT_SECRET = "54d7307928f63414defd96399fc31ba847961ceaecef3a5fd93144e960c0e151"; | |
| 31 | |
| 32 static constexpr std::string_view BASE_API_PATH = "https://kitsu.io/api/edge"; | |
| 33 static constexpr std::string_view OAUTH_PATH = "https://kitsu.io/api/oauth/token"; | |
| 34 | |
| 35 namespace Services { | |
| 36 namespace Kitsu { | |
| 37 | |
| 38 /* This nifty little function basically handles authentication AND reauthentication. */ | |
| 39 static bool SendAuthRequest(const nlohmann::json& data) { | |
| 40 static const std::vector<std::string> headers = { | |
| 41 {"Content-Type: application/json"} | |
| 42 }; | |
| 43 | |
| 44 const std::string ret = Strings::ToUtf8String(HTTP::Request(std::string(OAUTH_PATH), headers, data.dump(), HTTP::Type::Post)); | |
| 45 if (ret.empty()) { | |
| 46 session.SetStatusBar("Kitsu: Request returned empty data!"); | |
| 47 return false; | |
| 48 } | |
| 49 | |
| 50 nlohmann::json result; | |
| 51 try { | |
| 52 result = nlohmann::json::parse(ret, nullptr, false); | |
| 53 } catch (const std::exception& ex) { | |
| 54 session.SetStatusBar(std::string("Kitsu: Failed to parse authorization data with error \"") + ex.what() + "\"!"); | |
| 55 return false; | |
| 56 } | |
| 57 | |
| 58 if (result.contains("/error"_json_pointer)) { | |
| 59 std::string status = "Kitsu: Failed with error \""; | |
| 60 status += result["/error"_json_pointer].get<std::string>(); | |
| 61 | |
| 62 if (result.contains("/error_description"_json_pointer)) { | |
| 63 status += "\" and description \""; | |
| 64 status += result["/error_description"_json_pointer].get<std::string>(); | |
| 65 } | |
| 66 | |
| 67 status += "\"!"; | |
| 68 | |
| 69 session.SetStatusBar(status); | |
| 70 return false; | |
| 71 } | |
| 72 | |
| 73 const std::vector<nlohmann::json::json_pointer> required = { | |
| 74 "/access_token"_json_pointer, | |
| 75 "/created_at"_json_pointer, | |
| 76 "/expires_in"_json_pointer, | |
| 77 "/refresh_token"_json_pointer, | |
| 78 "/scope"_json_pointer, | |
| 79 "/token_type"_json_pointer | |
| 80 }; | |
| 81 | |
| 82 for (const auto& ptr : required) { | |
| 83 if (!result.contains(ptr)) { | |
| 84 session.SetStatusBar("Kitsu: Authorization request returned bad data!"); | |
| 85 return false; | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 session.config.auth.kitsu.access_token = result["/access_token"_json_pointer].get<std::string>(); | |
| 90 session.config.auth.kitsu.access_token_expiration | |
| 91 = result["/created_at"_json_pointer].get<Time::Timestamp>(); | |
| 92 + result["/expires_in"_json_pointer].get<Time::Timestamp>(); | |
| 93 session.config.auth.kitsu.refresh_token = result["/refresh_token"_json_pointer].get<std::string>(); | |
| 94 | |
| 95 /* the next two are not that important */ | |
| 96 | |
| 97 return true; | |
| 98 } | |
| 99 | |
| 100 static bool RefreshAccessToken(std::string& access_token, const std::string& refresh_token) { | |
| 101 const nlohmann::json request = { | |
| 102 {"grant_type", "refresh_token"}, | |
| 103 {"refresh_token", refresh_token} | |
| 104 }; | |
| 105 | |
| 106 if (!SendAuthRequest(request)) | |
| 107 return false; | |
| 108 | |
| 109 return true; | |
| 110 } | |
| 111 | |
| 112 /* ----------------------------------------------------------------------------- */ | |
| 113 | |
| 114 static std::optional<std::string> AccountAccessToken() { | |
| 115 auto& auth = session.config.auth.kitsu; | |
| 116 | |
| 117 if (Time::GetSystemTime() >= session.config.auth.kitsu.access_token_expiration) | |
| 118 if (!RefreshAccessToken(auth.access_token, auth.refresh_token)) | |
| 119 return std::nullopt; | |
| 120 | |
| 121 return auth.access_token; | |
| 122 } | |
| 123 | |
| 124 /* ----------------------------------------------------------------------------- */ | |
| 125 | |
| 126 static std::optional<std::string> SendRequest(const std::string& path, const std::map<std::string, std::string>& params) { | |
| 127 std::optional<std::string> token = AccountAccessToken(); | |
| 128 if (!token) | |
| 129 return std::nullopt; | |
| 130 | |
| 131 const std::vector<std::string> headers = { | |
| 132 "Accept: application/vnd.api+json", | |
| 133 "Authorization: Bearer " + token.value(), | |
| 134 "Content-Type: application/vnd.api+json" | |
| 135 }; | |
| 136 | |
| 137 const std::string url = HTTP::EncodeParamsList(std::string(BASE_API_PATH) + path, params); | |
| 138 | |
| 139 return Strings::ToUtf8String(HTTP::Request(url, headers, "", HTTP::Type::Get)); | |
| 140 } | |
| 141 | |
| 142 static void ParseTitleJson(Anime::Anime& anime, const nlohmann::json& json) { | |
| 143 static const std::map<std::string, Anime::TitleLanguage> lookup = { | |
| 144 {"en", Anime::TitleLanguage::English}, | |
| 145 {"en_jp", Anime::TitleLanguage::Romaji}, | |
| 146 {"ja_jp", Anime::TitleLanguage::Native} | |
| 147 }; | |
| 148 | |
| 149 for (const auto& [string, title] : lookup) | |
| 150 if (json.contains(string)) | |
| 151 anime.SetTitle(title, json[string].get<std::string>()); | |
| 152 } | |
| 153 | |
| 154 static void ParseSubtype(Anime::Anime& anime, const std::string& str) { | |
| 155 static const std::map<std::string, Anime::SeriesFormat> lookup = { | |
| 156 {"ONA", Anime::SeriesFormat::Ona}, | |
| 157 {"OVA", Anime::SeriesFormat::Ova}, | |
| 158 {"TV", Anime::SeriesFormat::Tv}, | |
| 159 {"movie", Anime::SeriesFormat::Movie}, | |
| 160 {"music", Anime::SeriesFormat::Music}, | |
| 161 {"special", Anime::SeriesFormat::Special} | |
| 162 }; | |
| 163 | |
| 164 if (lookup.find(str) == lookup.end()) | |
| 165 return; | |
| 166 | |
| 167 anime.SetFormat(lookup.at(str)); | |
| 168 } | |
| 169 | |
| 170 static const std::string FAILED_TO_PARSE = "Kitsu: Failed to parse anime object!"; | |
| 171 | |
| 172 static int ParseAnimeJson(const nlohmann::json& json) { | |
| 173 const std::string service_id = json["/id"_json_pointer].get<std::string>(); | |
| 174 if (service_id.empty()) { | |
| 175 session.SetStatusBar(FAILED_TO_PARSE); | |
| 176 return 0; | |
| 177 } | |
| 178 | |
| 179 if (!json.contains("/attributes"_json_pointer)) { | |
| 180 session.SetStatusBar(FAILED_TO_PARSE); | |
| 181 return 0; | |
| 182 } | |
| 183 | |
| 184 const auto& attributes = json["/attributes"_json_pointer]; | |
| 185 | |
| 186 int id = Anime::db.LookupServiceIdOrUnused(Anime::Service::Kitsu, service_id); | |
| 187 if (!id) { | |
| 188 session.SetStatusBar(FAILED_TO_PARSE); | |
| 189 return 0; | |
| 190 } | |
| 191 | |
| 192 Anime::Anime& anime = Anime::db.items[id]; | |
| 193 | |
| 194 anime.SetId(id); | |
| 195 anime.SetServiceId(Anime::Service::Kitsu, service_id); | |
| 196 anime.SetSynopsis(attributes["/synopsis"_json_pointer].get<std::string>()); | |
| 197 ParseTitleJson(anime, attributes["/titles"_json_pointer]); | |
| 198 | |
| 199 // FIXME: parse abbreviatedTitles for synonyms?? | |
| 200 | |
| 201 anime.SetAudienceScore(JSON::GetNumber<double>(attributes, "/averageRating"_json_pointer)); | |
| 202 | |
| 203 if (attributes.contains("/startDate"_json_pointer)) | |
| 204 anime.SetAirDate(attributes["/startDate"_json_pointer].get<std::string>()); | |
| 205 | |
| 206 // TODO: endDate | |
| 207 | |
| 208 if (attributes.contains("/subtype"_json_pointer)) | |
| 209 ParseSubtype(anime, attributes["/subtype"_json_pointer].get<std::string>()); | |
| 210 | |
| 211 anime.SetPosterUrl(attributes["/posterImage/original"_json_pointer].get<std::string>()); | |
| 212 anime.SetEpisodes(attributes["/episodeCount"_json_pointer].get<int>()); | |
| 213 anime.SetDuration(attributes["/episodeLength"_json_pointer].get<int>()); | |
| 214 | |
| 215 return id; | |
| 216 } | |
| 217 | |
| 218 static int ParseLibraryJson(const nlohmann::json& json) { | |
| 219 if (!json.contains("/relationships/anime/data"_json_pointer) | |
| 220 || !json.contains("/attributes"_json_pointer) | |
| 221 || !json.contains("/id"_json_pointer)) { | |
| 222 session.SetStatusBar("Kitsu: Failed to parse library object!"); | |
| 223 return 0; | |
| 224 } | |
| 225 | |
| 226 int id = ParseAnimeJson(json["/relationships/anime/data"_json_pointer]); | |
| 227 if (!id) | |
| 228 return 0; | |
| 229 | |
| 230 const auto& attributes = json["/attributes"_json_pointer]; | |
| 231 | |
| 232 const std::string library_id = json["/id"_json_pointer].get<std::string>(); | |
| 233 | |
| 234 Anime::Anime& anime = Anime::db.items[id]; | |
| 235 | |
| 236 anime.AddToUserList(); | |
| 237 | |
| 238 anime.SetUserId(library_id); | |
| 239 anime.SetUserDateStarted(Date(attributes["/startedAt"_json_pointer].get<std::string>())); | |
| 240 anime.SetUserDateCompleted(Date(attributes["/finishedAt"_json_pointer].get<std::string>())); | |
| 241 anime.SetUserNotes(attributes["/notes"_json_pointer].get<std::string>()); | |
| 242 anime.SetUserProgress(attributes["/progress"_json_pointer].get<int>()); | |
| 243 anime.SetUserScore(attributes["/ratingTwenty"_json_pointer].get<int>() * 5); | |
| 244 anime.SetUserIsPrivate(attributes["/private"_json_pointer].get<bool>()); | |
| 245 anime.SetUserRewatchedTimes(attributes["/reconsumeCount"_json_pointer].get<int>()); | |
| 246 anime.SetUserIsRewatching(attributes["/reconsuming"_json_pointer].get<bool>()); /* "reconsuming". really? */ | |
| 247 // anime.SetUserStatus(); | |
| 248 // anime.SetUserLastUpdated(); | |
| 249 | |
| 250 return id; | |
| 251 } | |
| 252 | |
| 253 int GetAnimeList() { | |
| 254 return 0; | |
| 255 } | |
| 256 | |
| 257 /* unimplemented for now */ | |
| 258 std::vector<int> Search(const std::string& search) { | |
| 259 return {}; | |
| 260 } | |
| 261 | |
| 262 std::vector<int> GetSeason(Anime::SeriesSeason season, Date::Year year) { | |
| 263 return {}; | |
| 264 } | |
| 265 | |
| 266 int UpdateAnimeEntry(int id) { | |
| 267 return 0; | |
| 268 } | |
| 269 | |
| 270 bool AuthorizeUser(const std::string& email, const std::string& password) { | |
| 271 const nlohmann::json body = { | |
| 272 {"grant_type", "password"}, | |
| 273 {"username", email}, | |
| 274 {"password", HTTP::UrlEncode(password)} | |
| 275 }; | |
| 276 | |
| 277 if (!SendAuthRequest(body)) | |
| 278 return false; | |
| 279 | |
| 280 static const std::map<std::string, std::string> params = { | |
| 281 {"filter[self]", "true"} | |
| 282 }; | |
| 283 | |
| 284 std::optional<std::string> response = SendRequest("/users", params); | |
| 285 if (!response) | |
| 286 return false; // whuh? | |
| 287 | |
| 288 nlohmann::json json; | |
| 289 try { | |
| 290 json = nlohmann::json::parse(response.value()); | |
| 291 } catch (const std::exception& ex) { | |
| 292 session.SetStatusBar(std::string("Kitsu: Failed to parse user data with error \"") + ex.what() + "\"!"); | |
| 293 return false; | |
| 294 } | |
| 295 | |
| 296 if (!json.contains("/data/0/id"_json_pointer)) { | |
| 297 session.SetStatusBar("Kitsu: Failed to retrieve user ID!"); | |
| 298 return false; | |
| 299 } | |
| 300 | |
| 301 session.SetStatusBar("Kitsu: Successfully retrieved user data!"); | |
| 302 session.config.auth.kitsu.user_id = json["/data/0/id"_json_pointer].get<std::string>(); | |
| 303 | |
| 304 return true; | |
| 305 } | |
| 306 | |
| 307 } // namespace Kitsu | |
| 308 } // namespace Services |
