Implemented YouTube videos search and play with support for queuse

This commit is contained in:
Fabio Manganiello 2018-04-18 00:23:12 +02:00
parent 02e951bd57
commit ab19e258bb
2 changed files with 31 additions and 1 deletions

View file

@ -3,6 +3,10 @@ import logging
import re import re
import subprocess import subprocess
import urllib.request
import urllib.parse
from bs4 import BeautifulSoup
from dbus.exceptions import DBusException from dbus.exceptions import DBusException
from omxplayer import OMXPlayer from omxplayer import OMXPlayer
@ -14,6 +18,7 @@ class VideoOmxplayerPlugin(Plugin):
def __init__(self, args=[], *argv, **kwargs): def __init__(self, args=[], *argv, **kwargs):
self.args = args self.args = args
self.player = None self.player = None
self.videos_queue = []
def play(self, resource): def play(self, resource):
if resource.startswith('youtube:') \ if resource.startswith('youtube:') \
@ -29,6 +34,21 @@ class VideoOmxplayerPlugin(Plugin):
return self.status() 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): def pause(self):
if self.player: self.player.play_pause() if self.player: self.player.play_pause()
@ -59,6 +79,16 @@ class VideoOmxplayerPlugin(Plugin):
self.player.seek(+30) self.player.seek(+30)
return self.status() 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): def status(self):
if self.player: if self.player:
return Response(output=json.dumps({ return Response(output=json.dumps({

View file

@ -74,7 +74,7 @@ setup(
'Support for Belkin WeMo Switch plugin': ['ouimeaux'], 'Support for Belkin WeMo Switch plugin': ['ouimeaux'],
'Support for text2speech plugin': ['mplayer'], 'Support for text2speech plugin': ['mplayer'],
'Support for OMXPlayer plugin': ['omxplayer'], '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 Google Assistant': ['google-assistant-library'],
'Support for the Google APIs': ['google-api-python-client'], 'Support for the Google APIs': ['google-api-python-client'],
'Support for most of the HTTP poll backends': ['python-dateutil'], 'Support for most of the HTTP poll backends': ['python-dateutil'],