diff options
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/Mangadex.cpp | 83 |
1 files changed, 48 insertions, 35 deletions
diff --git a/src/plugins/Mangadex.cpp b/src/plugins/Mangadex.cpp index 48ab23f..3f42297 100644 --- a/src/plugins/Mangadex.cpp +++ b/src/plugins/Mangadex.cpp @@ -58,11 +58,6 @@ namespace QuickMedia { return PluginResult::OK; } - class MangadexChapterImagesList : public BodyItemExtra { - public: - std::vector<std::string> image_urls; - }; - 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); @@ -124,7 +119,7 @@ namespace QuickMedia { return search_result_to_plugin_result(search(str, page, result_items)); } - static PluginResult get_chapters_for_manga(Page *page, const std::string &manga_id, int page_num, BodyItems &result_items) { + static PluginResult get_chapters_for_manga(Page *page, const std::string &manga_id, int page_num, BodyItems &result_items, ChapterImageUrls &chapter_image_urls) { std::string request_url = "https://api.mangadex.org/manga/" + manga_id + "/feed?order[chapter]=desc&limit=100&locales[]=en&offset=" + std::to_string(page_num * 100); Json::Value json_root; @@ -186,15 +181,16 @@ namespace QuickMedia { auto body_item = BodyItem::create(std::move(title)); body_item->url = id_json.asString(); - auto images_list = std::make_shared<MangadexChapterImagesList>(); + std::vector<std::string> image_urls; + image_urls.reserve(attributes_data_json.size()); for(const Json::Value &data_item_json : attributes_data_json) { if(!data_item_json.isString()) continue; std::string url = hash_json.asString() + "/" + data_item_json.asString(); - images_list->image_urls.push_back(std::move(url)); + image_urls.push_back(std::move(url)); } - body_item->extra = std::move(images_list); + chapter_image_urls[body_item->url] = std::move(image_urls); result_items.push_back(std::move(body_item)); } @@ -202,39 +198,20 @@ namespace QuickMedia { } PluginResult MangadexSearchPage::submit(const std::string &title, const std::string &url, std::vector<Tab> &result_tabs) { + chapter_image_urls.clear(); auto body = create_body(); - get_chapters_for_manga(this, url, 0, body->items); - result_tabs.push_back(Tab{std::move(body), std::make_unique<MangadexChaptersPage>(program, title, url), create_search_bar("Search...", SEARCH_DELAY_FILTER)}); + get_chapters_for_manga(this, url, 0, body->items, chapter_image_urls); + result_tabs.push_back(Tab{std::move(body), std::make_unique<MangadexChaptersPage>(program, this, title, url), create_search_bar("Search...", SEARCH_DELAY_FILTER)}); return PluginResult::OK; } PluginResult MangadexChaptersPage::submit(const std::string &title, const std::string &url, std::vector<Tab> &result_tabs) { - Json::Value json_root; - if(download_json(json_root, "https://api.mangadex.org/at-home/server/" + url, {}, true) != DownloadResult::OK) - return PluginResult::NET_ERR; - - if(!json_root.isObject()) - return PluginResult::ERR; - - const Json::Value &base_url_json = json_root["baseUrl"]; - if(!base_url_json.isString()) - return PluginResult::ERR; - - std::string base_url = base_url_json.asString(); - if(!base_url.empty() && base_url.back() != '/') - base_url += '/'; - - auto image_urls = static_cast<MangadexChapterImagesList*>(submit_body_item->extra.get())->image_urls; - for(auto &image_url : image_urls) { - image_url = base_url + "data/" + image_url; - } - - result_tabs.push_back(Tab{nullptr, std::make_unique<MangadexImagesPage>(program, content_title, title, content_url, image_urls), nullptr}); + result_tabs.push_back(Tab{nullptr, std::make_unique<MangadexImagesPage>(program, search_page, content_title, url, title), nullptr}); return PluginResult::OK; } PluginResult MangadexChaptersPage::get_page(const std::string&, int page, BodyItems &result_items) { - return get_chapters_for_manga(this, content_url, page, result_items); + return get_chapters_for_manga(this, content_url, page, result_items, search_page->chapter_image_urls); } bool MangadexChaptersPage::extract_id_from_url(const std::string &url, std::string &manga_id) const { @@ -256,15 +233,51 @@ namespace QuickMedia { } ImageResult MangadexImagesPage::get_number_of_images(int &num_images) { - num_images = image_urls.size(); + num_images = 0; + ImageResult image_result = get_image_urls_for_chapter(url); + if(image_result != ImageResult::OK) return image_result; + num_images = chapter_image_urls.size(); return ImageResult::OK; } ImageResult MangadexImagesPage::for_each_page_in_chapter(PageCallback callback) { - for(const std::string &url : image_urls) { + ImageResult image_result = get_image_urls_for_chapter(url); + if(image_result != ImageResult::OK) return image_result; + for(const std::string &url : chapter_image_urls) { if(!callback(url)) break; } return ImageResult::OK; } + + ImageResult MangadexImagesPage::get_image_urls_for_chapter(const std::string &url) { + if(!chapter_image_urls.empty()) + return ImageResult::OK; + + Json::Value json_root; + if(download_json(json_root, "https://api.mangadex.org/at-home/server/" + url, {}, true) != DownloadResult::OK) + return ImageResult::NET_ERR; + + if(!json_root.isObject()) + return ImageResult::ERR; + + const Json::Value &base_url_json = json_root["baseUrl"]; + if(!base_url_json.isString()) + return ImageResult::ERR; + + std::string base_url = base_url_json.asString(); + if(!base_url.empty() && base_url.back() != '/') + base_url += '/'; + + auto it = search_page->chapter_image_urls.find(url); + if(it == search_page->chapter_image_urls.end()) + return ImageResult::ERR; + + chapter_image_urls.resize(it->second.size()); + for(size_t i = 0; i < it->second.size(); ++i) { + chapter_image_urls[i] = base_url + "data/" + it->second[i]; + } + + return ImageResult::OK; + } } |