Mercurial > minori
comparison src/services/anilist.cc @ 319:d928ec7b6a0d
services/kitsu: implement GetAnimeList()
it finally works!
| author | Paper <paper@paper.us.eu.org> |
|---|---|
| date | Wed, 12 Jun 2024 17:52:26 -0400 |
| parents | b1f4d1867ab1 |
| children | 1b5c04268d6a |
comparison
equal
deleted
inserted
replaced
| 318:3b355fa948c7 | 319:d928ec7b6a0d |
|---|---|
| 148 if (json.contains(ptr) && json[ptr].is_string()) | 148 if (json.contains(ptr) && json[ptr].is_string()) |
| 149 anime.SetTitle(language, json[ptr]); | 149 anime.SetTitle(language, json[ptr]); |
| 150 } | 150 } |
| 151 | 151 |
| 152 static int ParseMediaJson(const nlohmann::json& json) { | 152 static int ParseMediaJson(const nlohmann::json& json) { |
| 153 if (!json.contains("/id"_json_pointer) || !json["/id"_json_pointer].is_number()) | 153 if (!json.contains("/id"_json_pointer) || !json["/id"_json_pointer].is_number()) { |
| 154 return 0; | 154 session.SetStatusBar("AniList: Failed to parse anime object!"); |
| 155 return 0; | |
| 156 } | |
| 155 | 157 |
| 156 std::string service_id = Strings::ToUtf8String(json["/id"_json_pointer].get<int>()); | 158 std::string service_id = Strings::ToUtf8String(json["/id"_json_pointer].get<int>()); |
| 157 | 159 |
| 158 int id = Anime::db.LookupServiceIdOrUnused(Anime::Service::AniList, service_id); | 160 int id = Anime::db.LookupServiceIdOrUnused(Anime::Service::AniList, service_id); |
| 161 if (!id) { | |
| 162 session.SetStatusBar("AniList: Failed to parse anime object!"); | |
| 163 return 0; | |
| 164 } | |
| 159 | 165 |
| 160 Anime::Anime& anime = Anime::db.items[id]; | 166 Anime::Anime& anime = Anime::db.items[id]; |
| 161 anime.SetId(id); | 167 anime.SetId(id); |
| 162 anime.SetServiceId(Anime::Service::AniList, service_id); | 168 anime.SetServiceId(Anime::Service::AniList, service_id); |
| 163 | 169 |
| 189 | 195 |
| 190 return id; | 196 return id; |
| 191 } | 197 } |
| 192 | 198 |
| 193 static int ParseListItem(const nlohmann::json& json) { | 199 static int ParseListItem(const nlohmann::json& json) { |
| 194 int id = ParseMediaJson(json); | 200 int id = ParseMediaJson(json["/media"_json_pointer]); |
| 195 if (!id) | 201 if (!id) |
| 196 return 0; | 202 return 0; |
| 197 | 203 |
| 198 Anime::Anime& anime = Anime::db.items[id]; | 204 Anime::Anime& anime = Anime::db.items[id]; |
| 199 | 205 |
| 210 anime.SetUserTimeUpdated(JSON::GetNumber(json, "/updatedAt"_json_pointer, 0)); | 216 anime.SetUserTimeUpdated(JSON::GetNumber(json, "/updatedAt"_json_pointer, 0)); |
| 211 | 217 |
| 212 return id; | 218 return id; |
| 213 } | 219 } |
| 214 | 220 |
| 215 static int ParseList(const nlohmann::json& json) { | 221 static bool ParseList(const nlohmann::json& json) { |
| 222 bool success = true; | |
| 223 | |
| 216 for (const auto& entry : json["entries"].items()) | 224 for (const auto& entry : json["entries"].items()) |
| 217 ParseListItem(entry.value()); | 225 if (!ParseListItem(entry.value())) |
| 218 | 226 success = false; |
| 219 return 1; | 227 |
| 228 return success; | |
| 220 } | 229 } |
| 221 | 230 |
| 222 int GetAnimeList() { | 231 int GetAnimeList() { |
| 223 if (!account.IsValid()) { | 232 if (!account.IsValid()) { |
| 224 session.SetStatusBar("AniList: Account isn't valid! (unauthorized?)"); | 233 session.SetStatusBar("AniList: Account isn't valid! (unauthorized?)"); |
| 269 nlohmann::json result; | 278 nlohmann::json result; |
| 270 const bool res = SendJSONRequest(json, result); | 279 const bool res = SendJSONRequest(json, result); |
| 271 if (!res) | 280 if (!res) |
| 272 return 0; | 281 return 0; |
| 273 | 282 |
| 283 bool success = true; | |
| 284 | |
| 285 Anime::db.RemoveAllUserData(); | |
| 286 | |
| 274 for (const auto& list : result["data"]["MediaListCollection"]["lists"].items()) | 287 for (const auto& list : result["data"]["MediaListCollection"]["lists"].items()) |
| 275 ParseList(list.value()); | 288 if (!ParseList(list.value())) |
| 276 | 289 success = false; |
| 277 session.SetStatusBar("AniList: Retrieved anime list successfully!"); | 290 |
| 291 if (success) | |
| 292 session.SetStatusBar("AniList: Retrieved anime list successfully!"); | |
| 278 | 293 |
| 279 return 1; | 294 return 1; |
| 280 } | 295 } |
| 281 | 296 |
| 282 /* return is a vector of anime ids */ | 297 /* return is a vector of anime ids */ |
| 447 session.SetStatusBar("AniList: Requesting user ID..."); | 462 session.SetStatusBar("AniList: Requesting user ID..."); |
| 448 | 463 |
| 449 constexpr std::string_view query = "query {\n" | 464 constexpr std::string_view query = "query {\n" |
| 450 " Viewer {\n" | 465 " Viewer {\n" |
| 451 " id\n" | 466 " id\n" |
| 452 " name\n" | |
| 453 " mediaListOptions {\n" | |
| 454 " scoreFormat\n" // this will be used... eventually | |
| 455 " }\n" | |
| 456 " }\n" | 467 " }\n" |
| 457 "}\n"; | 468 "}\n"; |
| 458 nlohmann::json json = { | 469 nlohmann::json json = { |
| 459 {"query", query} | 470 {"query", query} |
| 460 }; | 471 }; |
| 463 nlohmann::json result; | 474 nlohmann::json result; |
| 464 const bool ret = SendJSONRequest(json, result); | 475 const bool ret = SendJSONRequest(json, result); |
| 465 if (!ret) | 476 if (!ret) |
| 466 return 0; | 477 return 0; |
| 467 | 478 |
| 468 session.SetStatusBar("AniList: Successfully retrieved user data!"); | 479 if (ParseUser(result["data"]["Viewer"])) |
| 469 | 480 session.SetStatusBar("AniList: Successfully retrieved user data!"); |
| 470 ParseUser(ret["data"]["Viewer"]); | 481 else |
| 482 session.SetStatusBar("AniList: Failed to retrieve user ID!"); | |
| 483 | |
| 471 return true; | 484 return true; |
| 472 } | 485 } |
| 473 | 486 |
| 474 } // namespace AniList | 487 } // namespace AniList |
| 475 } // namespace Services | 488 } // namespace Services |
