From 575ccde5d1e896a279d58375704f4ace3f48bf6a Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 30 Apr 2021 04:05:26 +0200 Subject: Make initial youtube request faster (faster cookies setup) --- include/AsyncImageLoader.hpp | 1 - plugins/MangaCombined.hpp | 2 +- src/QuickMedia.cpp | 76 +++++++++++++++++++++----------------------- src/plugins/Youtube.cpp | 12 ++----- 4 files changed, 40 insertions(+), 51 deletions(-) diff --git a/include/AsyncImageLoader.hpp b/include/AsyncImageLoader.hpp index 69746d2..4acae5e 100644 --- a/include/AsyncImageLoader.hpp +++ b/include/AsyncImageLoader.hpp @@ -35,7 +35,6 @@ namespace QuickMedia { static AsyncImageLoader& get_instance(); // Returns false if the image loader is already loading an image. In that case, this function should be called again later. // set |resize_target_size| to {0, 0} to disable resizing. - // |thumbnail_data.loading_state| has to be LoadingState::NOT_LOADED when calling this! // Note: this method is not thread-safe void load_thumbnail(const std::string &url, bool local, sf::Vector2i resize_target_size, std::shared_ptr thumbnail_data); private: diff --git a/plugins/MangaCombined.hpp b/plugins/MangaCombined.hpp index 56b03ba..56843c2 100644 --- a/plugins/MangaCombined.hpp +++ b/plugins/MangaCombined.hpp @@ -24,6 +24,6 @@ namespace QuickMedia { sf::Vector2i get_thumbnail_max_size() override { return sf::Vector2i(101, 141); }; private: std::vector search_pages; - std::vector>> search_threads; + std::vector>> search_threads; // TODO: Use async task pool }; } \ No newline at end of file diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 7402b97..247221e 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -711,6 +711,34 @@ namespace QuickMedia { main_thread_id = std::this_thread::get_id(); } + // Returns size_t(-1) if not found + static size_t find_end_of_json_array(const char *str, size_t start, size_t size) { + if(size <= start || str[start] != '[') + return size_t(-1); + + bool inside_string = false; + bool escape = false; + int array_depth = 0; + for(size_t i = start; i < size; ++i) { + char c = str[i]; + if(c == '"' && !escape) { + inside_string = !inside_string; + } else if(c == '\\') { + escape = !escape; + } else if(c == '[' && !inside_string && !escape) { + ++array_depth; + } else if(c == ']' && !inside_string && !escape) { + --array_depth; + if(array_depth == 0) + return i + 1; + } else { + escape = false; + } + } + + return size_t(-1); + } + static void add_manganelos_handlers(MangaGenericSearchPage *manga_generic_search_page) { manga_generic_search_page->search_handler("http://manganelos.com/search?q=%s&page=%p", 1) .text_handler({{"//div[class='media-left cover-manga']//a", "title", "href", "/manga/"}}) @@ -770,12 +798,14 @@ namespace QuickMedia { if(sources_start == std::string::npos) return urls; - sources_start += 6; - size_t sources_end = html_source.find("]", sources_start); - if(sources_end == std::string::npos) + sources_start += 5; // just before [ + size_t json_end = find_end_of_json_array(html_source.c_str(), sources_start, html_source.size()); + if(json_end == size_t(-1)) return urls; - std::string urls_str = html_source.substr(sources_start, sources_end - sources_start); + sources_start += 1; + json_end -= 1; + std::string urls_str = html_source.substr(sources_start, json_end - sources_start); string_replace_all(urls_str, "'", ""); string_split(urls_str, ',', [&urls](const char *str, size_t size) { @@ -785,6 +815,7 @@ namespace QuickMedia { urls.push_back(std::move(url)); return true; }); + return urls; }) .manga_id_handler("/manga/", nullptr); @@ -806,34 +837,6 @@ namespace QuickMedia { .related_media_thumbnail_handler({{"//div[class='right']//div[class='video-item']//img", "data-src", nullptr}}); } - // Returns size_t(-1) if not found - static size_t find_end_of_json_array(const char *str, size_t start, size_t size) { - if(size <= start || str[start] != '[') - return size_t(-1); - - bool inside_string = false; - bool escape = false; - int array_depth = 0; - for(size_t i = start; i < size; ++i) { - char c = str[i]; - if(c == '"' && !escape) { - inside_string = !inside_string; - } else if(c == '\\') { - escape = !escape; - } else if(c == '[' && !inside_string && !escape) { - ++array_depth; - } else if(c == ']' && !inside_string && !escape) { - --array_depth; - if(array_depth == 0) - return i + 1; - } else { - escape = false; - } - } - - return size_t(-1); - } - static void add_xvideos_handlers(MediaGenericSearchPage *media_generic_search_page) { media_generic_search_page->search_handler("https://www.xvideos.com/?k=%s&p=%p", 0) .text_handler({{"//div[id='content']//div[class='thumb-under']//a", "title", "href", "/video"}}) @@ -2165,16 +2168,9 @@ namespace QuickMedia { video_player_window = None; added_recommendations = false; watched_videos.insert(video_url); - std::string video_url_converted; - std::string dummy_id; - if(youtube_url_extract_id(video_url, dummy_id)) { - video_url_converted = "ytdl://" + video_url; - } else { - video_url_converted = video_url; - } video_player = std::make_unique(no_video, use_system_mpv_config, resume_video, is_matrix, video_event_callback, on_window_create, resources_root, get_largest_monitor_height(disp)); - VideoPlayer::Error err = video_player->load_video(video_url_converted.c_str(), window.getSystemHandle(), plugin_name, video_title); + VideoPlayer::Error err = video_player->load_video(video_url.c_str(), window.getSystemHandle(), plugin_name, video_title); if(err != VideoPlayer::Error::OK) { std::string err_msg = "Failed to play url: "; err_msg += video_url; diff --git a/src/plugins/Youtube.cpp b/src/plugins/Youtube.cpp index d03105c..993c861 100644 --- a/src/plugins/Youtube.cpp +++ b/src/plugins/Youtube.cpp @@ -343,15 +343,9 @@ namespace QuickMedia { cookies_filepath = filename; atexit(remove_cookies_file_at_exit); - std::vector additional_args = { - CommandArg{ "-b", cookies_filepath }, - CommandArg{ "-c", cookies_filepath } - }; - // TODO: Is there any way to bypass this? this is needed to set VISITOR_INFO1_LIVE which is required to read comments - std::string website_data; - DownloadResult result = download_to_string("https://youtube.com/subscription_manager?disable_polymer=1", website_data, std::move(additional_args), true); - if(result != DownloadResult::OK) + const char *args[] = { "curl", "-I", "-s", "-b", cookies_filepath.c_str(), "-c", cookies_filepath.c_str(), "https://www.youtube.com/subscription_manager?disable_polymer=1", nullptr }; + if(exec_program(args, nullptr, nullptr) != 0) fprintf(stderr, "Failed to fetch cookies to view youtube comments\n"); } @@ -525,7 +519,7 @@ namespace QuickMedia { current_page = 0; added_videos.clear(); - search_url = "https://youtube.com/results?search_query="; + search_url = "https://www.youtube.com/results?search_query="; search_url += url_param_encode(str); std::vector additional_args = { -- cgit v1.2.3