Mercurial > minori
diff 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 |
line wrap: on
line diff
--- a/src/services/anilist.cc Wed Jun 12 05:25:41 2024 -0400 +++ b/src/services/anilist.cc Wed Jun 12 17:52:26 2024 -0400 @@ -150,12 +150,18 @@ } static int ParseMediaJson(const nlohmann::json& json) { - if (!json.contains("/id"_json_pointer) || !json["/id"_json_pointer].is_number()) + if (!json.contains("/id"_json_pointer) || !json["/id"_json_pointer].is_number()) { + session.SetStatusBar("AniList: Failed to parse anime object!"); return 0; + } std::string service_id = Strings::ToUtf8String(json["/id"_json_pointer].get<int>()); int id = Anime::db.LookupServiceIdOrUnused(Anime::Service::AniList, service_id); + if (!id) { + session.SetStatusBar("AniList: Failed to parse anime object!"); + return 0; + } Anime::Anime& anime = Anime::db.items[id]; anime.SetId(id); @@ -191,7 +197,7 @@ } static int ParseListItem(const nlohmann::json& json) { - int id = ParseMediaJson(json); + int id = ParseMediaJson(json["/media"_json_pointer]); if (!id) return 0; @@ -212,11 +218,14 @@ return id; } -static int ParseList(const nlohmann::json& json) { +static bool ParseList(const nlohmann::json& json) { + bool success = true; + for (const auto& entry : json["entries"].items()) - ParseListItem(entry.value()); + if (!ParseListItem(entry.value())) + success = false; - return 1; + return success; } int GetAnimeList() { @@ -271,10 +280,16 @@ if (!res) return 0; + bool success = true; + + Anime::db.RemoveAllUserData(); + for (const auto& list : result["data"]["MediaListCollection"]["lists"].items()) - ParseList(list.value()); + if (!ParseList(list.value())) + success = false; - session.SetStatusBar("AniList: Retrieved anime list successfully!"); + if (success) + session.SetStatusBar("AniList: Retrieved anime list successfully!"); return 1; } @@ -449,10 +464,6 @@ constexpr std::string_view query = "query {\n" " Viewer {\n" " id\n" - " name\n" - " mediaListOptions {\n" - " scoreFormat\n" // this will be used... eventually - " }\n" " }\n" "}\n"; nlohmann::json json = { @@ -465,9 +476,11 @@ if (!ret) return 0; - session.SetStatusBar("AniList: Successfully retrieved user data!"); - - ParseUser(ret["data"]["Viewer"]); + if (ParseUser(result["data"]["Viewer"])) + session.SetStatusBar("AniList: Successfully retrieved user data!"); + else + session.SetStatusBar("AniList: Failed to retrieve user ID!"); + return true; }