aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2019-08-05 00:37:57 +0200
committerdec05eba <dec05eba@protonmail.com>2019-08-05 00:38:00 +0200
commit7e8a2f23a40e6374ddfb551920257846021e86fa (patch)
treee9fae05dbc3c2b83f580f5c40fd4cae97e03b536
parent887b6003010a0c455667fb67c12a5577c60498db (diff)
Skip youtube ads in search
-rw-r--r--include/VideoPlayer.hpp2
-rw-r--r--src/QuickMedia.cpp2
-rw-r--r--src/VideoPlayer.cpp35
-rw-r--r--src/Youtube.cpp12
4 files changed, 37 insertions, 14 deletions
diff --git a/include/VideoPlayer.hpp b/include/VideoPlayer.hpp
index 5fd5e60..c52c1e5 100644
--- a/include/VideoPlayer.hpp
+++ b/include/VideoPlayer.hpp
@@ -25,7 +25,7 @@ namespace QuickMedia {
class VideoPlayer {
public:
// Throws VideoInitializationException on error
- VideoPlayer(unsigned int width, unsigned int height, const char *file, bool loop = false);
+ VideoPlayer(unsigned int width, unsigned int height, sf::WindowHandle window_handle, const char *file, bool loop = false);
~VideoPlayer();
void setPosition(float x, float y);
diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp
index 70f520a..c7d89e2 100644
--- a/src/QuickMedia.cpp
+++ b/src/QuickMedia.cpp
@@ -380,7 +380,7 @@ namespace QuickMedia {
std::unique_ptr<VideoPlayer> video_player;
try {
printf("Play video: %s\n", video_url.c_str());
- video_player = std::make_unique<VideoPlayer>(window_size.x, window_size.y, video_url.c_str());
+ video_player = std::make_unique<VideoPlayer>(window_size.x, window_size.y, window.getSystemHandle(), video_url.c_str());
} catch(VideoInitializationException &e) {
fprintf(stderr, "Failed to create video player!. TODO: Show this to the user");
}
diff --git a/src/VideoPlayer.cpp b/src/VideoPlayer.cpp
index e3dab43..ee4c715 100644
--- a/src/VideoPlayer.cpp
+++ b/src/VideoPlayer.cpp
@@ -14,8 +14,22 @@ namespace QuickMedia {
VideoPlayer *video_player = (VideoPlayer*)rawVideo;
++video_player->redrawCounter;
}
+
+ static void check_error(int status) {
+ if(status < 0)
+ fprintf(stderr, "mpv api error: %s\n", mpv_error_string(status));
+ }
+
+ static void mpv_set_option_bool(mpv_handle *mpv, const char *option, bool value) {
+ int int_value = value;
+ check_error(mpv_set_option(mpv, option, MPV_FORMAT_FLAG, &int_value));
+ }
+
+ static void mpv_set_option_int64(mpv_handle *mpv, const char *option, int64_t value) {
+ check_error(mpv_set_option(mpv, option, MPV_FORMAT_INT64, &value));
+ }
- VideoPlayer::VideoPlayer(unsigned int width, unsigned int height, const char *file, bool loop) :
+ VideoPlayer::VideoPlayer(unsigned int width, unsigned int height, sf::WindowHandle window_handle, const char *file, bool loop) :
redrawCounter(0),
onPlaybackEndedCallback(nullptr),
mpv(nullptr),
@@ -33,20 +47,23 @@ namespace QuickMedia {
if(!mpv)
throw VideoInitializationException("Failed to create mpv handle");
- //mpv_set_option_string(mpv, "input-default-bindings", "yes");
- //mpv_set_option_string(mpv, "input-vo-keyboard", "yes");
- mpv_set_option_string(mpv, "cache-secs", "120");
- mpv_set_option_string(mpv, "demuxer-max-bytes", "20M");
- mpv_set_option_string(mpv, "demuxer-max-back-bytes", "10M");
+ //check_error(mpv_set_option_string(mpv, "input-default-bindings", "yes"));
+ //check_error(mpv_set_option_string(mpv, "input-vo-keyboard", "yes"));
+ check_error(mpv_set_option_string(mpv, "cache-secs", "120"));
+ check_error(mpv_set_option_string(mpv, "demuxer-max-bytes", "20M"));
+ check_error(mpv_set_option_string(mpv, "demuxer-max-back-bytes", "10M"));
+
+ //mpv_set_option_bool(mpv, "osc", true);
+ //mpv_set_option_int64(mpv, "wid", window_handle);
if(mpv_initialize(mpv) < 0)
throw VideoInitializationException("Failed to initialize mpv");
- mpv_set_option_string(mpv, "vo", "opengl-cb");
- mpv_set_option_string(mpv, "hwdec", "auto");
+ check_error(mpv_set_option_string(mpv, "vo", "opengl-cb"));
+ check_error(mpv_set_option_string(mpv, "hwdec", "auto"));
if(loop)
- mpv_set_option_string(mpv, "loop", "inf");
+ check_error(mpv_set_option_string(mpv, "loop", "inf"));
mpvGl = (mpv_opengl_cb_context*)mpv_get_sub_api(mpv, MPV_SUB_API_OPENGL_CB);
if(!mpvGl)
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 <quickmedia/HtmlSearch.h>
#include <json/reader.h>
+#include <string.h>
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<std::unique_ptr<BodyItem>> &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<std::unique_ptr<BodyItem>>*)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<BodyItem>(title);
+ // Checking for watch?v helps skipping ads
+ if(href && title && begins_with(href, "/watch?v=")) {
+ auto item = std::make_unique<BodyItem>(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<std::unique_ptr<BodyItem>>*)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<BodyItem>("");
item->url = std::string("https://www.youtube.com") + href;
result_items->push_back(std::move(item));