From ba4e62d55156f9b94b569b56b6382bbcf94b7d86 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 16 Apr 2021 09:37:53 +0200 Subject: Convert mangatown and manganelos into a generic manga plugin Revert for_each_page.. processing of manga instead of getting all pages. Mangatown requires you to navigate page by page, cant predict what a specific pages image url will be. --- src/QuickMedia.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 56 insertions(+), 15 deletions(-) (limited to 'src/QuickMedia.cpp') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 5bdc67e..26760ec 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -1,8 +1,7 @@ #include "../include/QuickMedia.hpp" #include "../plugins/Manganelo.hpp" -#include "../plugins/Manganelos.hpp" -#include "../plugins/Mangatown.hpp" #include "../plugins/Mangadex.hpp" +#include "../plugins/MangaGeneric.hpp" #include "../plugins/Youtube.hpp" #include "../plugins/Pornhub.hpp" #include "../plugins/Spankbang.hpp" @@ -675,9 +674,45 @@ namespace QuickMedia { show_room_side_panel = false; else show_room_side_panel = true; + main_thread_id = std::this_thread::get_id(); } + static void add_manganelos_handlers(MangaGenericSearchPage *manga_generic_search_page) { + manga_generic_search_page->search_handler("http://manganelos.com/search?q=", "&page=", 1) + .text_handler("//div[class='media-left cover-manga']//a", "title", "href", "/manga/") + .thumbnail_handler("//div[class='media-left cover-manga']//img[class='media-object']", "src", "/mangaimage/") + .list_chapters_handler("//section[id='examples']//div[class='chapter-list']//a", "text", "href", nullptr) + .list_page_images_handler("//p[id='arraydata']", "text", nullptr, [](std::vector &urls) { + if(urls.size() != 1) + return; + + std::string urls_combined = urls.front(); + urls.clear(); + + string_split(urls_combined, ',', [&urls](const char *str, size_t size) { + std::string url(str, size); + url = strip(url); + urls.push_back(std::move(url)); + return true; + }); + }) + .manga_id_handler("/manga/", "?"); + } + + static void add_mangatown_handlers(MangaGenericSearchPage *manga_generic_search_page) { + manga_generic_search_page->search_handler("https://www.mangatown.com/search?name=", "&page=", 1) + .text_handler("//p[class='title']/a", "title", "href", "/manga/") + .thumbnail_handler("//a[class='manga_cover']/img", "src", nullptr) + .list_chapters_handler("//ul[class='chapter_list']//a", "text", "href", "/manga/") + .list_chapters_uploaded_time_handler("//ul[class='chapter_list']//span[class='time']", "text", nullptr) + .list_page_images_pagination_handler( + "//div[class='page_select']//option", "value", "/manga/", [](int num_pages){ return std::max(0, (num_pages /= 2) - 1); }, + "//img[id='image']", "src", nullptr, + "//a[class='next_page']", "href", nullptr) + .manga_id_handler("/manga/", "/"); + } + void Program::load_plugin_by_name(std::vector &tabs, const char *start_dir) { if(!plugin_name || plugin_name[0] == '\0') return; @@ -727,7 +762,9 @@ namespace QuickMedia { tabs.push_back(Tab{std::move(history_body), std::move(history_page), std::move(search_bar)}); } else if(strcmp(plugin_name, "manganelos") == 0) { auto search_body = create_body(); - tabs.push_back(Tab{std::move(search_body), std::make_unique(this), create_search_bar("Search...", 400)}); + auto search_page = std::make_unique(this, plugin_name, nullptr); + add_manganelos_handlers(search_page.get()); + tabs.push_back(Tab{std::move(search_body), std::move(search_page), create_search_bar("Search...", 400)}); auto history_body = create_body(); auto search_bar = create_search_bar("Search...", SEARCH_DELAY_FILTER); @@ -735,7 +772,9 @@ namespace QuickMedia { tabs.push_back(Tab{std::move(history_body), std::move(history_page), std::move(search_bar)}); } else if(strcmp(plugin_name, "mangatown") == 0) { auto search_body = create_body(); - tabs.push_back(Tab{std::move(search_body), std::make_unique(this), create_search_bar("Search...", 400)}); + auto search_page = std::make_unique(this, plugin_name, "https://www.mangatown.com"); + add_mangatown_handlers(search_page.get()); + tabs.push_back(Tab{std::move(search_body), std::move(search_page), create_search_bar("Search...", 400)}); auto history_body = create_body(); auto search_bar = create_search_bar("Search...", SEARCH_DELAY_FILTER); @@ -2321,24 +2360,24 @@ namespace QuickMedia { Path content_cache_dir_ = content_cache_dir; image_download_future = AsyncTask>([images_page, content_cache_dir_, this](std::promise num_manga_pages_promise) { - std::vector page_image_urls; - if(images_page->get_page_image_urls(page_image_urls) != ImageResult::OK) { + int num_pages = 0; + if(images_page->get_number_of_images(num_pages) != ImageResult::OK) { num_manga_pages_promise.set_value(0); if(!image_download_cancel) show_notification("QuickMedia", "Failed to fetch page images", Urgency::CRITICAL); return; } else { - num_manga_pages_promise.set_value(page_image_urls.size()); - image_upscale_status.resize(page_image_urls.size(), 0); + num_manga_pages_promise.set_value(num_pages); + image_upscale_status.resize(num_pages, 0); } - if(page_image_urls.empty()) + if(num_pages == 0) return; // TODO: Download images in parallel int page = 1; - for(const std::string &url : page_image_urls) { + images_page->for_each_page_in_chapter([this, images_page, &page, content_cache_dir_](const std::string &url) { if(image_download_cancel) - return; + return false; int image_index = page - 1; @@ -2355,7 +2394,7 @@ namespace QuickMedia { } if(get_file_type(image_filepath) != FileType::FILE_NOT_FOUND && upscaled_ok) - continue; + return true; std::vector extra_args; const bool is_manganelo = (strcmp(images_page->get_service_name(), "manganelo") == 0); @@ -2376,7 +2415,7 @@ namespace QuickMedia { size_t file_size = 0; if(download_to_file(url, image_filepath_tmp.data, extra_args, true) != DownloadResult::OK || (is_manganelo && file_get_size(image_filepath_tmp, &file_size) == 0 && file_size < 255)) { if(!image_download_cancel) show_notification("QuickMedia", "Failed to download image: " + url, Urgency::CRITICAL); - continue; + return true; } bool rename_immediately = true; @@ -2412,10 +2451,12 @@ namespace QuickMedia { if(rename(image_filepath_tmp.data.c_str(), image_filepath.data.c_str()) != 0) { perror(image_filepath_tmp.data.c_str()); show_notification("QuickMedia", "Failed to save image to file: " + image_filepath.data, Urgency::CRITICAL); - continue; + return true; } } - } + + return true; + }); }, std::move(num_manga_pages_promise)); sf::Event event; -- cgit v1.2.3