From 7e8a2f23a40e6374ddfb551920257846021e86fa Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 5 Aug 2019 00:37:57 +0200 Subject: Skip youtube ads in search --- src/Youtube.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src/Youtube.cpp') diff --git a/src/Youtube.cpp b/src/Youtube.cpp index 4647887..780244c 100644 --- a/src/Youtube.cpp +++ b/src/Youtube.cpp @@ -1,8 +1,13 @@ #include "../plugins/Youtube.hpp" #include #include +#include namespace QuickMedia { + static bool begins_with(const char *str, const char *begin_with) { + return strncmp(str, begin_with, strlen(begin_with)) == 0; + } + SearchResult Youtube::search(const std::string &text, std::vector> &result_items) { std::string url = "https://youtube.com/results?search_query="; url += url_param_encode(text); @@ -21,8 +26,9 @@ namespace QuickMedia { auto *result_items = (std::vector>*)userdata; const char *href = quickmedia_html_node_get_attribute_value(node, "href"); const char *title = quickmedia_html_node_get_attribute_value(node, "title"); - if(href && title) { - auto item = std::make_unique(title); + // Checking for watch?v helps skipping ads + if(href && title && begins_with(href, "/watch?v=")) { + auto item = std::make_unique(title); item->url = std::string("https://www.youtube.com") + href; result_items->push_back(std::move(item)); } @@ -98,7 +104,7 @@ namespace QuickMedia { auto *result_items = (std::vector>*)userdata; const char *href = quickmedia_html_node_get_attribute_value(node, "href"); // TODO: Also add title for related media - if(href) { + if(href && begins_with(href, "/watch?v=")) { auto item = std::make_unique(""); item->url = std::string("https://www.youtube.com") + href; result_items->push_back(std::move(item)); -- cgit v1.2.3