diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-04-14 19:52:21 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-04-14 23:45:11 +0200 |
commit | ab36fbffef977b99cc03d0b77ac37bdc1b5705dc (patch) | |
tree | 17207a364eecee330a26ac117a1384ad0b4a1e97 /src/plugins | |
parent | 4db0876f45533d3b55ee79df2d2bc78b789b5a28 (diff) |
Rework manga plugins downloading.. preparing for parallel downloads
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/Mangadex.cpp | 48 | ||||
-rw-r--r-- | src/plugins/Manganelo.cpp | 41 | ||||
-rw-r--r-- | src/plugins/Manganelos.cpp | 41 | ||||
-rw-r--r-- | src/plugins/Mangatown.cpp | 104 |
4 files changed, 63 insertions, 171 deletions
diff --git a/src/plugins/Mangadex.cpp b/src/plugins/Mangadex.cpp index 59126cb..44c9762 100644 --- a/src/plugins/Mangadex.cpp +++ b/src/plugins/Mangadex.cpp @@ -219,29 +219,7 @@ namespace QuickMedia { return true; } - ImageResult MangadexImagesPage::get_number_of_images(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; - } - - bool MangadexImagesPage::save_mangadex_cookies(const std::string &url, const std::string &cookie_filepath) { - CommandArg cookie_arg = { "-c", cookie_filepath }; - std::string server_response; - if(download_to_string(url, server_response, {std::move(cookie_arg)}, true) != DownloadResult::OK) - return false; - - return true; - } - - ImageResult MangadexImagesPage::get_image_urls_for_chapter(const std::string &url) { - if(!chapter_image_urls.empty()) - return ImageResult::OK; - + ImageResult MangadexImagesPage::get_page_image_urls(std::vector<std::string> &urls) { std::string cookie_filepath; if(!get_cookie_filepath(cookie_filepath)) return ImageResult::ERR; @@ -283,28 +261,22 @@ namespace QuickMedia { continue; std::string image_url = server + chapter_hash_str + "/" + image_name.asCString(); - chapter_image_urls.push_back(std::move(image_url)); + urls.push_back(std::move(image_url)); } } - if(chapter_image_urls.empty()) + if(urls.empty()) return ImageResult::ERR; + return ImageResult::OK; } - ImageResult MangadexImagesPage::for_each_page_in_chapter(PageCallback callback) { - std::vector<std::string> image_urls; - ImageResult image_result = get_image_urls_for_chapter(url); - if(image_result != ImageResult::OK) - return image_result; - - image_urls = chapter_image_urls; - - for(const std::string &url : image_urls) { - if(!callback(url)) - break; - } + bool MangadexImagesPage::save_mangadex_cookies(const std::string &url, const std::string &cookie_filepath) { + CommandArg cookie_arg = { "-c", cookie_filepath }; + std::string server_response; + if(download_to_string(url, server_response, {std::move(cookie_arg)}, true) != DownloadResult::OK) + return false; - return ImageResult::OK; + return true; } } diff --git a/src/plugins/Manganelo.cpp b/src/plugins/Manganelo.cpp index 8ce9e01..b67acb2 100644 --- a/src/plugins/Manganelo.cpp +++ b/src/plugins/Manganelo.cpp @@ -226,36 +226,7 @@ namespace QuickMedia { return PluginResult::OK; } - ImageResult ManganeloImagesPage::get_number_of_images(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) { - std::vector<std::string> image_urls; - ImageResult image_result = get_image_urls_for_chapter(url); - if(image_result != ImageResult::OK) - return image_result; - - image_urls = chapter_image_urls; - - for(const std::string &url : 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; - + ImageResult ManganeloImagesPage::get_page_image_urls(std::vector<std::string> &urls) { std::string website_data; if(download_to_string(url, website_data, {}, true) != DownloadResult::OK) return ImageResult::NET_ERR; @@ -273,16 +244,16 @@ namespace QuickMedia { std::string image_url = strip(src); urls->push_back(std::move(image_url)); } - }, &chapter_image_urls); + }, &urls); cleanup: quickmedia_html_search_deinit(&html_search); - if(result != 0) { - chapter_image_urls.clear(); + if(result != 0) return ImageResult::ERR; - } - if(chapter_image_urls.empty()) + + if(urls.empty()) return ImageResult::ERR; + return ImageResult::OK; } }
\ No newline at end of file diff --git a/src/plugins/Manganelos.cpp b/src/plugins/Manganelos.cpp index 9ab0915..04d9ca2 100644 --- a/src/plugins/Manganelos.cpp +++ b/src/plugins/Manganelos.cpp @@ -117,36 +117,7 @@ namespace QuickMedia { return true; } - ImageResult ManganelosImagesPage::get_number_of_images(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 ManganelosImagesPage::for_each_page_in_chapter(PageCallback callback) { - std::vector<std::string> image_urls; - ImageResult image_result = get_image_urls_for_chapter(url); - if(image_result != ImageResult::OK) - return image_result; - - image_urls = chapter_image_urls; - - for(const std::string &url : image_urls) { - if(!callback(url)) - break; - } - - return ImageResult::OK; - } - - ImageResult ManganelosImagesPage::get_image_urls_for_chapter(const std::string &url) { - if(!chapter_image_urls.empty()) - return ImageResult::OK; - + ImageResult ManganelosImagesPage::get_page_image_urls(std::vector<std::string> &urls) { std::string website_data; if(download_to_string(url, website_data, {}, true) != DownloadResult::OK) return ImageResult::NET_ERR; @@ -168,16 +139,16 @@ namespace QuickMedia { return true; }); } - }, &chapter_image_urls); + }, &urls); cleanup: quickmedia_html_search_deinit(&html_search); - if(result != 0) { - chapter_image_urls.clear(); + if(result != 0) return ImageResult::ERR; - } - if(chapter_image_urls.empty()) + + if(urls.empty()) return ImageResult::ERR; + return ImageResult::OK; } }
\ No newline at end of file diff --git a/src/plugins/Mangatown.cpp b/src/plugins/Mangatown.cpp index 04a1049..4db1ebc 100644 --- a/src/plugins/Mangatown.cpp +++ b/src/plugins/Mangatown.cpp @@ -139,65 +139,30 @@ namespace QuickMedia { return true; } - ImageResult MangatownImagesPage::get_number_of_images(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 MangatownImagesPage::for_each_page_in_chapter(PageCallback callback) { - int num_pages; - ImageResult image_result = get_number_of_images(num_pages); - if(image_result != ImageResult::OK) - return image_result; - - for(const std::string &full_url : chapter_image_urls) { - std::string image_src; - std::string website_data; - if(download_to_string_cache(full_url, website_data, {}, true) != DownloadResult::OK) - break; - - QuickMediaHtmlSearch html_search; - int result = quickmedia_html_search_init(&html_search, website_data.c_str()); - if(result != 0) - goto cleanup; - - result = quickmedia_html_find_nodes_xpath(&html_search, "//div[id='viewer']//img", - [](QuickMediaHtmlNode *node, void *userdata) { - std::string *image_src = (std::string*)userdata; - const char *src = quickmedia_html_node_get_attribute_value(node, "src"); - if(src && strstr(src, "/store/manga/")) { - if(strncmp(src, "//", 2) == 0) - *image_src = strip(src + 2); - else - *image_src = strip(src); - } - }, &image_src); - - cleanup: - quickmedia_html_search_deinit(&html_search); - - if(result != 0) - return ImageResult::ERR; - - if(image_src.empty()) - break; - - if(!callback(image_src)) - break; - } - - return ImageResult::OK; + // First page = 0 + static std::string first_image_url_to_page_image_url(const std::string &image_url, int page) { + size_t slash_index = image_url.rfind('/'); + if(slash_index == std::string::npos) + return ""; + + size_t dot_index = image_url.rfind('.'); + if(dot_index == std::string::npos || dot_index <= slash_index) + return ""; + + std::string filename_without_ext = image_url.substr(slash_index + 1, dot_index - (slash_index + 1)); + if(!filename_without_ext.empty() && filename_without_ext.back() == '1') + ++page; + + std::string page_str = std::to_string(page); + if(page_str.size() > filename_without_ext.size()) + return ""; + + filename_without_ext.replace(filename_without_ext.size() - page_str.size(), page_str.size(), page_str); + return image_url.substr(0, slash_index) + '/' + filename_without_ext + image_url.substr(dot_index); } - ImageResult MangatownImagesPage::get_image_urls_for_chapter(const std::string &url) { - if(!chapter_image_urls.empty()) - return ImageResult::OK; - + ImageResult MangatownImagesPage::get_page_image_urls(std::vector<std::string> &urls) { + std::string image_src; int num_pages = 0; std::string website_data; @@ -209,6 +174,21 @@ namespace QuickMedia { if(result != 0) goto cleanup; + result = quickmedia_html_find_nodes_xpath(&html_search, "//div[id='viewer']//img", + [](QuickMediaHtmlNode *node, void *userdata) { + std::string *image_src = (std::string*)userdata; + const char *src = quickmedia_html_node_get_attribute_value(node, "src"); + if(src && strstr(src, "/store/manga/")) { + if(strncmp(src, "//", 2) == 0) + *image_src = strip(src + 2); + else + *image_src = strip(src); + } + }, &image_src); + + if(result != 0 || image_src.empty()) + goto cleanup; + result = quickmedia_html_find_nodes_xpath(&html_search, "//div[class='page_select']//option", [](QuickMediaHtmlNode *node, void *userdata) { int *last_num_pages = (int*)userdata; @@ -225,15 +205,13 @@ namespace QuickMedia { cleanup: quickmedia_html_search_deinit(&html_search); - if(result != 0) { - chapter_image_urls.clear(); - return ImageResult::ERR; - } - if(num_pages == 0) + if(result != 0 || image_src.empty() || num_pages == 0) return ImageResult::ERR; + for(int i = 0; i < num_pages; ++i) { - chapter_image_urls.push_back(url + std::to_string(1 + i) + ".html"); + urls.push_back(first_image_url_to_page_image_url(image_src, i)); } + return ImageResult::OK; } }
\ No newline at end of file |