aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-04-30 04:05:26 +0200
committerdec05eba <dec05eba@protonmail.com>2021-04-30 04:05:26 +0200
commit575ccde5d1e896a279d58375704f4ace3f48bf6a (patch)
tree93618e0934bcbd7d9f83d9cb696df9945540543d /src
parent7dc7ee78094a3d76e8cb70d724a7054d59347369 (diff)
Make initial youtube request faster (faster cookies setup)
Diffstat (limited to 'src')
-rw-r--r--src/QuickMedia.cpp76
-rw-r--r--src/plugins/Youtube.cpp12
2 files changed, 39 insertions, 49 deletions
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<VideoPlayer>(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<CommandArg> 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<CommandArg> additional_args = {