From 3dce4e2b9200a1cd4eca50147203cb34a31e0ef3 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 5 Jun 2021 12:13:08 +0200 Subject: Add covers to mangadex, remove arrow left/right, require alt --- src/plugins/Mangadex.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 1 deletion(-) (limited to 'src/plugins/Mangadex.cpp') diff --git a/src/plugins/Mangadex.cpp b/src/plugins/Mangadex.cpp index 0143d7f..4a10455 100644 --- a/src/plugins/Mangadex.cpp +++ b/src/plugins/Mangadex.cpp @@ -58,8 +58,88 @@ namespace QuickMedia { return PluginResult::OK; } + static std::shared_ptr relationship_get_body_item(const Json::Value &json, BodyItems &body_items) { + for(const Json::Value &item_json : json) { + if(!item_json.isObject()) + continue; + + const Json::Value &type_json = item_json["type"]; + if(!type_json.isString() || strcmp(type_json.asCString(), "manga") != 0) + continue; + + const Json::Value &id_json = item_json["id"]; + if(!id_json.isString()) + continue; + + std::string id_str = id_json.asString(); + auto it = std::find_if(body_items.begin(), body_items.end(), [&id_str](const std::shared_ptr &body_item) { + return body_item->url == id_str; + }); + + if(it == body_items.end()) + continue; + + return *it; + } + return nullptr; + } + + PluginResult MangadexSearchPage::get_cover_urls(BodyItems &body_items) { + std::string url = "https://api.mangadex.org/cover?"; + for(size_t i = 0; i < body_items.size(); ++i) { + if(i > 0) + url += "&"; + url += "manga[]=" + body_items[i]->url; + } + + Json::Value json_root; + if(download_json(json_root, url, {}, true) != DownloadResult::OK) + return PluginResult::NET_ERR; + + if(!json_root.isObject()) + return PluginResult::OK; + + const Json::Value &results_json = json_root["results"]; + if(!results_json.isArray()) + return PluginResult::OK; + + std::shared_ptr body_item; + for(const Json::Value &result_item_json : results_json) { + if(!result_item_json.isObject()) + continue; + + const Json::Value &result_json = result_item_json["result"]; + if(!result_json.isString() || strcmp(result_json.asCString(), "ok") != 0) + continue; + + const Json::Value &relationships_json = result_item_json["relationships"]; + if(!relationships_json.isArray()) + continue; + + body_item = relationship_get_body_item(relationships_json, body_items); + if(!body_item) + continue; + + const Json::Value &data_json = result_item_json["data"]; + if(!data_json.isObject()) + continue; + + const Json::Value &attributes_json = data_json["attributes"]; + if(!attributes_json.isObject()) + continue; + + const Json::Value &filename_json = attributes_json["fileName"]; + if(!filename_json.isString()) + continue; + + body_item->thumbnail_url = "https://uploads.mangadex.org/covers/" + body_item->url + "/" + filename_json.asString() + ".256.jpg"; + } + + return PluginResult::OK; + } + SearchResult MangadexSearchPage::search(const std::string &str, int page, BodyItems &result_items) { - std::string url = "https://api.mangadex.org/manga?title=" + url_param_encode(str) + "&limit=100&offset=" + std::to_string(page * 100); + std::string url = "https://api.mangadex.org/manga?title=" + url_param_encode(str) + "&limit=20&offset=" + std::to_string(page * 20); Json::Value json_root; if(download_json(json_root, url, {}, true) != DownloadResult::OK) @@ -108,6 +188,9 @@ namespace QuickMedia { result_items.push_back(std::move(body_item)); } + // Intentionally ignore errors. This api shouldn't fail if we fail to get covers + get_cover_urls(result_items); + return SearchResult::OK; } -- cgit v1.2.3