From 0a26a319b241978ee317bbe768eb61c4eb7a39d9 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 25 Aug 2021 18:48:34 +0200 Subject: Faster mangakatana search on exact match --- src/DownloadUtils.cpp | 40 ++++++++++++++++++++++++---------------- src/NetUtils.cpp | 4 +++- src/StringUtils.cpp | 21 +++++++++++++++++++++ src/VideoPlayer.cpp | 5 ++++- src/plugins/MangaGeneric.cpp | 16 +++++----------- src/plugins/Youtube.cpp | 12 +++++++++--- 6 files changed, 66 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/DownloadUtils.cpp b/src/DownloadUtils.cpp index 5f0c547..a56a56e 100644 --- a/src/DownloadUtils.cpp +++ b/src/DownloadUtils.cpp @@ -12,10 +12,10 @@ namespace QuickMedia { struct DownloadUserdata { - std::string *header = nullptr; + std::vector *headers = nullptr; std::string *body = nullptr; int64_t download_limit = 1024 * 1024 * 100; // 100mb - bool header_finished = false; + bool headers_finished = false; int64_t total_downloaded_size = 0; }; @@ -37,19 +37,27 @@ namespace QuickMedia { static int accumulate_string_with_header(char *data, int size, void *userdata) { DownloadUserdata *download_userdata = (DownloadUserdata*)userdata; - if(download_userdata->header_finished) { + if(download_userdata->headers_finished) { download_userdata->body->append(data, size); } else { - download_userdata->header->append(data, size); + if(download_userdata->headers->empty()) + download_userdata->headers->push_back(""); + + std::string *current_header = &download_userdata->headers->back(); + current_header->append(data, size); + bool end_of_header_found = false; - size_t end_of_headers_index = download_userdata->header->find("\r\n\r\n"); + size_t end_of_headers_index = current_header->find("\r\n\r\n"); if(end_of_headers_index != std::string::npos) { while(true) { - const bool is_redirect = http_is_redirect(download_userdata->header->c_str(), end_of_headers_index); + const bool is_redirect = http_is_redirect(current_header->c_str(), end_of_headers_index); end_of_headers_index += 4; - if(is_redirect && download_userdata->header->size() - end_of_headers_index > 0) { - download_userdata->header->erase(download_userdata->header->begin(), download_userdata->header->begin() + end_of_headers_index); - end_of_headers_index = download_userdata->header->find("\r\n\r\n"); + if(is_redirect) { + std::string header_after_this_header = current_header->substr(end_of_headers_index); + current_header->erase(current_header->begin() + end_of_headers_index, current_header->end()); + download_userdata->headers->push_back(std::move(header_after_this_header)); + current_header = &download_userdata->headers->back(); + end_of_headers_index = current_header->find("\r\n\r\n"); if(end_of_headers_index == std::string::npos) break; } else { @@ -60,9 +68,9 @@ namespace QuickMedia { } if(end_of_header_found) { - download_userdata->body->append(download_userdata->header->begin() + end_of_headers_index, download_userdata->header->end()); - download_userdata->header->erase(download_userdata->header->begin() + end_of_headers_index, download_userdata->header->end()); - download_userdata->header_finished = true; + download_userdata->body->append(current_header->begin() + end_of_headers_index, current_header->end()); + current_header->erase(current_header->begin() + end_of_headers_index, current_header->end()); + download_userdata->headers_finished = true; } } @@ -185,7 +193,7 @@ namespace QuickMedia { } // TODO: Add timeout - DownloadResult download_to_string(const std::string &url, std::string &result, const std::vector &additional_args, bool use_browser_useragent, bool fail_on_error, bool cloudflare_bypass, std::string *header, int download_limit) { + DownloadResult download_to_string(const std::string &url, std::string &result, const std::vector &additional_args, bool use_browser_useragent, bool fail_on_error, bool cloudflare_bypass, std::vector *headers, int download_limit) { result.clear(); sf::Clock timer; std::vector args; @@ -203,7 +211,7 @@ namespace QuickMedia { } if(cloudflare_bypass) args.insert(args.end(), { "--tls-max", "1.1", "-A", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; FSL 7.0.6" }); - if(header) + if(headers) args.push_back("-i"); args.push_back("--"); args.push_back(url.c_str()); @@ -218,10 +226,10 @@ namespace QuickMedia { } DownloadUserdata download_userdata; - download_userdata.header = header; + download_userdata.headers = headers; download_userdata.body = &result; download_userdata.download_limit = download_limit; - download_userdata.header_finished = !header; + download_userdata.headers_finished = !headers; if(exec_program(args.data(), accumulate_string_with_header, &download_userdata) != 0) return DownloadResult::NET_ERR; diff --git a/src/NetUtils.cpp b/src/NetUtils.cpp index 0f957d5..9cf88c7 100644 --- a/src/NetUtils.cpp +++ b/src/NetUtils.cpp @@ -311,7 +311,7 @@ namespace QuickMedia { std::string result; string_split(header, '\n', [&type, &result](const char *str, size_t size) { while(size > 0 && (*str == ' ' || *str == '\t')) { ++str; --size; } - if(size < type.size() || strncasecmp(str, type.c_str(), type.size()) != 0 || size == type.size()) + if(size < type.size() || !strncase_equals(str, type.c_str(), type.size()) || size == type.size()) return true; str += type.size(); @@ -325,7 +325,9 @@ namespace QuickMedia { str += (colon_offset + 1); size -= (colon_offset + 1); + // lstrip space while(size > 0 && (*str == ' ' || *str == '\t')) { ++str; --size; } + // rstrip whitespace while(size > 0 && (str[size - 1] == ' ' || str[size - 1] == '\t' || str[size - 1] == '\r' || str[size - 1] == '\n')) { --size; } result.assign(str, size); diff --git a/src/StringUtils.cpp b/src/StringUtils.cpp index 5dfeca9..8a3a0ef 100644 --- a/src/StringUtils.cpp +++ b/src/StringUtils.cpp @@ -117,14 +117,35 @@ namespace QuickMedia { return c; } + bool strncase_equals(const char *str1, const char *str2, size_t length) { + size_t i = 0; + for(;;) { + if(i == length) + return true; + ++i; + + const char c1 = *str1; + const char c2 = *str2; + if(to_upper(c1) != to_upper(c2)) + return false; + else if(c1 == '\0') + return true; + + ++str1; + ++str2; + } + } + bool strcase_equals(const char *str1, const char *str2) { for(;;) { const char c1 = *str1; const char c2 = *str2; + if(to_upper(c1) != to_upper(c2)) return false; else if(c1 == '\0') return true; + ++str1; ++str2; } diff --git a/src/VideoPlayer.cpp b/src/VideoPlayer.cpp index 98d187f..ac18151 100644 --- a/src/VideoPlayer.cpp +++ b/src/VideoPlayer.cpp @@ -154,9 +154,12 @@ namespace QuickMedia { "--cache-on-disk=yes", "--cache-secs=86400", // 24 hours "--sub-font-size=40", + //"--sub-font=DejaVuSans-Bold", + //"--sub-bold=yes", "--sub-margin-y=45", "--sub-border-size=1.95", - "--sub-border-color=0.01", + "--sub-shadow-offset=1.25", + "--sub-shadow-color=0.2/0.9", cache_dir.c_str(), input_conf.c_str(), wid_arg.c_str() diff --git a/src/plugins/MangaGeneric.cpp b/src/plugins/MangaGeneric.cpp index 738c6dc..87b4f1d 100644 --- a/src/plugins/MangaGeneric.cpp +++ b/src/plugins/MangaGeneric.cpp @@ -169,7 +169,8 @@ namespace QuickMedia { std::string target_url; std::string website_data; - if(download_to_string(url, website_data, args, true, fail_on_http_error) != DownloadResult::OK) + std::vector website_headers; + if(download_to_string(url, website_data, args, true, fail_on_http_error, false, &website_headers) != DownloadResult::OK) return PluginResult::NET_ERR; if(website_data.empty()) @@ -199,17 +200,10 @@ namespace QuickMedia { goto cleanup; if(!text_query.url_field && !new_result_items.empty()) { - if(target_url.empty()) { - std::string response_headers; - DownloadResult download_result = download_head_to_string(url, response_headers, true); - if(download_result != DownloadResult::OK) { - result = -1; - goto cleanup; - } - - target_url = header_extract_value(response_headers, "location"); + if(target_url.empty() && !website_headers.empty()) { + target_url = header_extract_value(website_headers.front(), "location"); if(target_url.empty()) { - fprintf(stderr, "Failed to extract target location from %s HEAD\n", url.c_str()); + fprintf(stderr, "Failed to extract target location from %s\n", url.c_str()); result = -1; goto cleanup; } diff --git a/src/plugins/Youtube.cpp b/src/plugins/Youtube.cpp index 4934c43..b490e97 100644 --- a/src/plugins/Youtube.cpp +++ b/src/plugins/Youtube.cpp @@ -237,20 +237,26 @@ namespace QuickMedia { std::vector additional_args = get_cookies(); additional_args.push_back({"--no-buffer", ""}); + std::vector response_headers; const int max_redirects = 5; for(int i = 0; i < max_redirects; ++i) { std::string response_body; - std::string response_headers; + response_headers.clear(); download_to_string(playback_url, response_body, additional_args, true, true, false, &response_headers, 4096); - std::string content_type = header_extract_value(response_headers, "content-type"); + if(response_headers.empty()) { + fprintf(stderr, "Youtube video header not found\n"); + return ""; + } + + std::string content_type = header_extract_value(response_headers.back(), "content-type"); if(content_type.empty()) { fprintf(stderr, "Failed to find content-type in youtube video header\n"); return ""; } if(string_starts_with(content_type, "video") || string_starts_with(content_type, "audio")) { - std::string content_length_str = header_extract_value(response_headers, "content-length"); + std::string content_length_str = header_extract_value(response_headers.back(), "content-length"); if(content_length_str.empty()) return ""; -- cgit v1.2.3