Implemented YouTube videos search and play with support for queuse
This commit is contained in:
parent
02e951bd57
commit
ab19e258bb
2 changed files with 31 additions and 1 deletions
|
@ -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({
|
||||
|
|
2
setup.py
2
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'],
|
||||
|
|
Loading…
Reference in a new issue