From adcbb9e464bb9033e406008ea9cd209486a292aa Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 13 Sep 2021 21:55:09 +0200 Subject: Fix mangadex search/chapters listing after mangadex api update, combine same author/artist into one --- plugins/Manga.hpp | 6 ++++++ src/plugins/Mangadex.cpp | 51 ++++++++++++++++++++++++++---------------------- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/plugins/Manga.hpp b/plugins/Manga.hpp index 020099e..74d8ed1 100644 --- a/plugins/Manga.hpp +++ b/plugins/Manga.hpp @@ -7,9 +7,15 @@ namespace QuickMedia { // Return false to stop iteration using PageCallback = std::function; + enum class CreatorType { + AUTHOR, + ARTIST + }; + struct Creator { std::string name; std::string url; + CreatorType creator_type; }; class MangaImagesPage : public Page { diff --git a/src/plugins/Mangadex.cpp b/src/plugins/Mangadex.cpp index 9209828..671e9c7 100644 --- a/src/plugins/Mangadex.cpp +++ b/src/plugins/Mangadex.cpp @@ -113,19 +113,15 @@ namespace QuickMedia { if(!json_root.isObject()) return PluginResult::OK; - const Json::Value &results_json = json_root["results"]; - if(!results_json.isArray()) - return PluginResult::OK; - - for(const Json::Value &result_item_json : results_json) { - if(!result_item_json.isObject()) - continue; + const Json::Value &result_json = json_root["result"]; + if(!result_json.isString() || strcmp(result_json.asCString(), "ok") != 0) + return PluginResult::ERR; - const Json::Value &result_json = result_item_json["result"]; - if(!result_json.isString() || strcmp(result_json.asCString(), "ok") != 0) - continue; + const Json::Value &data_list_json = json_root["data"]; + if(!data_list_json.isArray()) + return PluginResult::ERR; - const Json::Value &data_json = result_item_json["data"]; + for(const Json::Value &data_json : data_list_json) { if(!data_json.isObject()) continue; @@ -187,20 +183,16 @@ namespace QuickMedia { if(!json_root.isObject()) return PluginResult::OK; - const Json::Value &results_json = json_root["results"]; - if(!results_json.isArray()) - return PluginResult::OK; - - const char *prev_chapter = nullptr; - for(const Json::Value &result_item_json : results_json) { - if(!result_item_json.isObject()) - continue; + const Json::Value &result_json = json_root["result"]; + if(!result_json.isString() || strcmp(result_json.asCString(), "ok") != 0) + return PluginResult::ERR; - const Json::Value &result_json = result_item_json["result"]; - if(!result_json.isString() || strcmp(result_json.asCString(), "ok") != 0) - continue; + const Json::Value &data_list_json = json_root["data"]; + if(!data_list_json.isArray()) + return PluginResult::ERR; - const Json::Value &data_json = result_item_json["data"]; + const char *prev_chapter = nullptr; + for(const Json::Value &data_json : data_list_json) { if(!data_json.isObject()) continue; @@ -293,6 +285,7 @@ namespace QuickMedia { Creator creator; creator.name = name_json.asString(); creator.url = id_json.asString(); + creator.creator_type = strcmp(type_json.asCString(), "author") == 0 ? CreatorType::AUTHOR : CreatorType::ARTIST; creators.push_back(std::move(creator)); } } @@ -319,6 +312,17 @@ namespace QuickMedia { return PluginResult::OK; } + static void remove_duplicate_creators(std::vector &creators) { + std::sort(creators.begin(), creators.end(), [](const Creator &creator1, const Creator &creator2) { + return creator1.name < creator2.name; + }); + creators.erase( + std::unique(creators.begin(), creators.end(), [](const Creator &creator1, const Creator &creator2) { + return creator1.name == creator2.name; + }), + creators.end()); + } + // TODO: Do requests in parallel PluginResult MangadexSearchPage::submit(const std::string &title, const std::string &url, std::vector &result_tabs) { PluginResult result; @@ -337,6 +341,7 @@ namespace QuickMedia { if(result != PluginResult::OK) return result; + remove_duplicate_creators(creators); for(Creator &creator : creators) { result_tabs.push_back(Tab{create_body(), std::make_unique(program, this, std::move(creator)), create_search_bar("Search...", SEARCH_DELAY_FILTER)}); } -- cgit v1.2.3