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) --- src/QuickMedia.cpp | 76 ++++++++++++++++++++++++++---------------------------- 1 file changed, 36 insertions(+), 40 deletions(-) (limited to 'src/QuickMedia.cpp') 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; -- cgit v1.2.3