From ab19e258bbd270c21a795ed5dbd85ad41f475f41 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Wed, 18 Apr 2018 00:23:12 +0200 Subject: [PATCH] Implemented YouTube videos search and play with support for queuse --- platypush/plugins/video/omxplayer.py | 30 ++++++++++++++++++++++++++++ setup.py | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/platypush/plugins/video/omxplayer.py b/platypush/plugins/video/omxplayer.py index 14417d4c..168da6c1 100644 --- a/platypush/plugins/video/omxplayer.py +++ b/platypush/plugins/video/omxplayer.py @@ -3,6 +3,10 @@ import logging import re import subprocess +import urllib.request +import urllib.parse + +from bs4 import BeautifulSoup from dbus.exceptions import DBusException from omxplayer import OMXPlayer @@ -14,6 +18,7 @@ class VideoOmxplayerPlugin(Plugin): def __init__(self, args=[], *argv, **kwargs): self.args = args self.player = None + self.videos_queue = [] def play(self, resource): if resource.startswith('youtube:') \ @@ -29,6 +34,21 @@ class VideoOmxplayerPlugin(Plugin): return self.status() + def youtube_search_and_play(self, query): + query = urllib.parse.quote(query) + url = "https://www.youtube.com/results?search_query=" + query + response = urllib.request.urlopen(url) + html = response.read() + soup = BeautifulSoup(html, 'lxml') + self.videos_queue = [] + + for vid in soup.findAll(attrs={'class':'yt-uix-tile-link'}): + if vid['href'].startswith('/watch?v='): + self.videos_queue.append('https://www.youtube.com' + vid['href']) + + self.play(self.videos_queue.pop(0)) + return self.status() + def pause(self): if self.player: self.player.play_pause() @@ -59,6 +79,16 @@ class VideoOmxplayerPlugin(Plugin): self.player.seek(+30) return self.status() + def next(self): + if self.player: + self.player.stop() + self.player.quit() + + if self.videos_queue: + self.play(self.videos_queue.pop(0)) + + return self.status() + def status(self): if self.player: return Response(output=json.dumps({ diff --git a/setup.py b/setup.py index ab30d965..3a4cea3e 100755 --- a/setup.py +++ b/setup.py @@ -74,7 +74,7 @@ setup( 'Support for Belkin WeMo Switch plugin': ['ouimeaux'], 'Support for text2speech plugin': ['mplayer'], 'Support for OMXPlayer plugin': ['omxplayer'], - 'Support for YouTube in the OMXPlayer plugin': ['youtube-dl'], + 'Support for YouTube in the OMXPlayer plugin': ['youtube-dl', 'beautifulsoup4'], 'Support for Google Assistant': ['google-assistant-library'], 'Support for the Google APIs': ['google-api-python-client'], 'Support for most of the HTTP poll backends': ['python-dateutil'],