From e65d0b4effdbbcc9393c58075c010f59231fd45e Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 13 Feb 2022 19:19:31 +0100 Subject: Make manga images pages less error prone --- TODO | 4 +++- plugins/LocalManga.hpp | 4 +--- plugins/Manga.hpp | 2 +- plugins/MangaGeneric.hpp | 2 +- plugins/Mangadex.hpp | 4 +--- plugins/Manganelo.hpp | 4 +--- src/QuickMedia.cpp | 2 +- src/plugins/LocalManga.cpp | 30 ++++++++++-------------------- src/plugins/MangaGeneric.cpp | 20 ++------------------ src/plugins/Mangadex.cpp | 30 ++++++++++-------------------- src/plugins/Manganelo.cpp | 30 ++++++++++-------------------- 11 files changed, 41 insertions(+), 91 deletions(-) diff --git a/TODO b/TODO index 3fa13f9..1e94118 100644 --- a/TODO +++ b/TODO @@ -217,4 +217,6 @@ Allow using ~ and $HOME in config file. Save manga "I" mode to file and load it on startup. Show in bookmarks and history page if a manga has been read (local-manga). Add "finished reading" to online manga as well, for the manga sites that publish latest chapter in the search page. -Async load visible body item content. This is needed for local-manga if the manga is stored on NFS where recursively reading all manga directories is slow. We only want to read recursively for the manga that is visible on the screen. \ No newline at end of file +Async load visible body item content. This is needed for local-manga if the manga is stored on NFS where recursively reading all manga directories is slow. We only want to read recursively for the manga that is visible on the screen. +Allow using ~/.config/mpv/input.conf when using use_system_mpv_config by merging QuickMedia's input.conf and the users input.conf and creating a temporary file in /tmp and load that. But make sure configs dont clash. +Allow asynchronously loading body items. This is needed in manga combined plugin. \ No newline at end of file diff --git a/plugins/LocalManga.hpp b/plugins/LocalManga.hpp index 0ab3f62..d327629 100644 --- a/plugins/LocalManga.hpp +++ b/plugins/LocalManga.hpp @@ -53,12 +53,10 @@ namespace QuickMedia { public: LocalMangaImagesPage(Program *program, std::string manga_name, std::string chapter_name, std::string url, std::string thumbnail_url) : MangaImagesPage(program, std::move(manga_name), std::move(chapter_name), std::move(url), std::move(thumbnail_url)) {} - ImageResult get_number_of_images(int &num_images) override; + ImageResult update_image_urls(int &num_images) override; ImageResult for_each_page_in_chapter(PageCallback callback) override; const char* get_service_name() const override { return "local-manga"; } const char* get_website_url() const override { return "localhost"; } bool is_local() override { return true; } - private: - ImageResult get_image_urls_for_chapter(const std::string &url); }; } \ No newline at end of file diff --git a/plugins/Manga.hpp b/plugins/Manga.hpp index fe9f0b4..a05dd3a 100644 --- a/plugins/Manga.hpp +++ b/plugins/Manga.hpp @@ -26,7 +26,7 @@ namespace QuickMedia { const char* get_title() const override { return chapter_name.c_str(); } PageTypez get_type() const override { return PageTypez::MANGA_IMAGES; } - virtual ImageResult get_number_of_images(int &num_images) = 0; + virtual ImageResult update_image_urls(int &num_images) = 0; virtual ImageResult for_each_page_in_chapter(PageCallback callback) = 0; virtual void change_chapter(std::string new_chapter_name, std::string new_url) { diff --git a/plugins/MangaGeneric.hpp b/plugins/MangaGeneric.hpp index 56d7c74..5318e63 100644 --- a/plugins/MangaGeneric.hpp +++ b/plugins/MangaGeneric.hpp @@ -185,7 +185,7 @@ namespace QuickMedia { public: MangaGenericImagesPage(Program *program, std::string manga_name, std::string chapter_name, std::string url, const char *service_name, const std::string &website_url, const ListPageQuery *list_page_query, bool fail_on_http_error, std::string thumbnail_url) : MangaImagesPage(program, std::move(manga_name), std::move(chapter_name), std::move(url), std::move(thumbnail_url)), service_name(service_name), website_url(website_url), list_page_query(list_page_query), fail_on_http_error(fail_on_http_error) {} - ImageResult get_number_of_images(int &num_images) override; + virtual ImageResult update_image_urls(int &num_images) override; ImageResult for_each_page_in_chapter(PageCallback callback) override; const char* get_service_name() const override { return service_name; } const char* get_website_url() const override { return website_url.c_str(); } diff --git a/plugins/Mangadex.hpp b/plugins/Mangadex.hpp index 8735326..6153187 100644 --- a/plugins/Mangadex.hpp +++ b/plugins/Mangadex.hpp @@ -53,12 +53,10 @@ namespace QuickMedia { public: MangadexImagesPage(Program *program, MangadexSearchPage *search_page, std::string manga_name, std::string chapter_id, std::string chapter_name, std::string thumbnail_url) : MangaImagesPage(program, std::move(manga_name), std::move(chapter_name), std::move(chapter_id), std::move(thumbnail_url)), search_page(search_page) {} - ImageResult get_number_of_images(int &num_images) override; + ImageResult update_image_urls(int &num_images) override; ImageResult for_each_page_in_chapter(PageCallback callback) override; const char* get_service_name() const override { return "mangadex"; } const char* get_website_url() const override { return "https://mangadex.org/"; } - private: - ImageResult get_image_urls_for_chapter(const std::string &url); private: MangadexSearchPage *search_page; }; diff --git a/plugins/Manganelo.hpp b/plugins/Manganelo.hpp index d835a06..f44ad2d 100644 --- a/plugins/Manganelo.hpp +++ b/plugins/Manganelo.hpp @@ -37,11 +37,9 @@ namespace QuickMedia { public: ManganeloImagesPage(Program *program, std::string manga_name, std::string chapter_name, std::string url, std::string thumbnail_url) : MangaImagesPage(program, std::move(manga_name), std::move(chapter_name), std::move(url), std::move(thumbnail_url)) {} - ImageResult get_number_of_images(int &num_images) override; + ImageResult update_image_urls(int &num_images) override; ImageResult for_each_page_in_chapter(PageCallback callback) override; const char* get_service_name() const override { return "manganelo"; } const char* get_website_url() const override { return "https://manganelo.com/"; } - private: - ImageResult get_image_urls_for_chapter(const std::string &url); }; } \ No newline at end of file diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index edb1db1..3ef29b5 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -3438,7 +3438,7 @@ 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) { int num_pages = 0; - if(images_page->get_number_of_images(num_pages) != ImageResult::OK) { + if(images_page->update_image_urls(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; diff --git a/src/plugins/LocalManga.cpp b/src/plugins/LocalManga.cpp index ed0f1b2..34a2983 100644 --- a/src/plugins/LocalManga.cpp +++ b/src/plugins/LocalManga.cpp @@ -490,27 +490,8 @@ namespace QuickMedia { return true; } - ImageResult LocalMangaImagesPage::get_number_of_images(int &num_images) { + ImageResult LocalMangaImagesPage::update_image_urls(int &num_images) { 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 LocalMangaImagesPage::for_each_page_in_chapter(PageCallback callback) { - 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 LocalMangaImagesPage::get_image_urls_for_chapter(const std::string &url) { - if(!chapter_image_urls.empty()) - return ImageResult::OK; Path chapter_url = Path(get_config().local_manga_directory).join(manga_name).join(url); std::vector pages = get_images_in_manga(chapter_url); @@ -521,6 +502,15 @@ namespace QuickMedia { chapter_image_urls.push_back(local_manga_page.path.data); } + num_images = chapter_image_urls.size(); + return ImageResult::OK; + } + + ImageResult LocalMangaImagesPage::for_each_page_in_chapter(PageCallback callback) { + for(const std::string &url : chapter_image_urls) { + if(!callback(url)) + break; + } return ImageResult::OK; } } \ No newline at end of file diff --git a/src/plugins/MangaGeneric.cpp b/src/plugins/MangaGeneric.cpp index 609dab9..47fbf08 100644 --- a/src/plugins/MangaGeneric.cpp +++ b/src/plugins/MangaGeneric.cpp @@ -423,7 +423,7 @@ namespace QuickMedia { return true; } - ImageResult MangaGenericImagesPage::get_number_of_images(int &num_images) { + ImageResult MangaGenericImagesPage::update_image_urls(int &num_images) { num_images = 0; switch(list_page_query->type) { case ListPageQueryType::IMAGES: { @@ -442,11 +442,6 @@ namespace QuickMedia { return ImageResult::ERR; } - if(chapter_num_pages != -1) { - num_images = chapter_num_pages; - return ImageResult::OK; - } - current_image_url.clear(); next_page_url.clear(); @@ -535,8 +530,6 @@ namespace QuickMedia { ImageResult MangaGenericImagesPage::for_each_page_in_chapter(PageCallback callback) { switch(list_page_query->type) { case ListPageQueryType::IMAGES: { - ImageResult result = get_page_image_urls(); - if(result != ImageResult::OK) return result; for(const std::string &url : chapter_image_urls) { if(!callback(url)) break; @@ -552,14 +545,10 @@ namespace QuickMedia { return ImageResult::ERR; } - int num_images = 0; - ImageResult result = get_number_of_images(num_images); - if(result != ImageResult::OK) return result; - if(!callback(current_image_url)) return ImageResult::OK; - for(int i = 0; i < num_images; ++i) { + for(int i = 0; i < chapter_num_pages; ++i) { std::string full_url = url + next_page_url; current_image_url.clear(); next_page_url.clear(); @@ -609,8 +598,6 @@ namespace QuickMedia { return ImageResult::OK; } case ListPageQueryType::CUSTOM: { - ImageResult result = get_page_image_urls(); - if(result != ImageResult::OK) return result; for(const std::string &url : chapter_image_urls) { if(!callback(url)) break; @@ -622,9 +609,6 @@ namespace QuickMedia { } ImageResult MangaGenericImagesPage::get_page_image_urls() { - if(!chapter_image_urls.empty()) - return ImageResult::OK; - std::vector args; if(!website_url.empty()) args.push_back({ "-H", "referer: " + website_url }); diff --git a/src/plugins/Mangadex.cpp b/src/plugins/Mangadex.cpp index 3b6b2e0..6aa73b0 100644 --- a/src/plugins/Mangadex.cpp +++ b/src/plugins/Mangadex.cpp @@ -415,27 +415,8 @@ namespace QuickMedia { return true; } - ImageResult MangadexImagesPage::get_number_of_images(int &num_images) { + ImageResult MangadexImagesPage::update_image_urls(int &num_images) { 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) { - 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; // TODO: If this fails, call this again to retrieve another server. Also do the same for automedia @@ -476,6 +457,15 @@ namespace QuickMedia { chapter_image_urls.push_back(base_url + "data/" + hash_str + "/" + data_item_json.asString()); } + num_images = chapter_image_urls.size(); + return ImageResult::OK; + } + + ImageResult MangadexImagesPage::for_each_page_in_chapter(PageCallback callback) { + for(const std::string &url : chapter_image_urls) { + if(!callback(url)) + break; + } return ImageResult::OK; } } diff --git a/src/plugins/Manganelo.cpp b/src/plugins/Manganelo.cpp index a440743..6f0d86f 100644 --- a/src/plugins/Manganelo.cpp +++ b/src/plugins/Manganelo.cpp @@ -246,27 +246,8 @@ namespace QuickMedia { return PluginResult::OK; } - ImageResult ManganeloImagesPage::get_number_of_images(int &num_images) { + ImageResult ManganeloImagesPage::update_image_urls(int &num_images) { 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 ManganeloImagesPage::for_each_page_in_chapter(PageCallback callback) { - 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 ManganeloImagesPage::get_image_urls_for_chapter(const std::string &url) { - if(!chapter_image_urls.empty()) - return ImageResult::OK; std::string website_data; if(download_to_string(url, website_data, {CommandArg { "-H", "referer: https://manganelo.com/" }}, true) != DownloadResult::OK) @@ -294,6 +275,15 @@ namespace QuickMedia { chapter_image_urls.clear(); return ImageResult::ERR; } + num_images = chapter_image_urls.size(); + return ImageResult::OK; + } + + ImageResult ManganeloImagesPage::for_each_page_in_chapter(PageCallback callback) { + for(const std::string &url : chapter_image_urls) { + if(!callback(url)) + break; + } return ImageResult::OK; } } \ No newline at end of file -- cgit v1.2.3