YouTube search and play code moved to media.ctrl plugin

This commit is contained in:
Fabio Manganiello 2018-04-18 00:50:19 +02:00
parent 78e08cc3bb
commit d2b881fee7
2 changed files with 30 additions and 30 deletions

View file

@ -1,6 +1,10 @@
import re
import subprocess
import urllib.request
import urllib.parse
from bs4 import BeautifulSoup
from omxplayer import OMXPlayer
from platypush.context import get_plugin
@ -23,6 +27,7 @@ class MediaCtrlPlugin(Plugin):
self.torrentcast_port = torrentcast_port
self.url = None
self.plugin = None
self.videos_queue = []
@classmethod
def _get_type_and_resource_by_url(cls, url):
@ -90,5 +95,30 @@ class MediaCtrlPlugin(Plugin):
if self.plugin: return self.plugin.forward()
def next(self):
if self.plugin:
self.plugin.stop()
if self.videos_queue:
return self.play(self.videos_queue.pop(0))
return Response(output={'status': 'no media'}, errors = [])
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'])
url = self.videos_queue.pop(0)
return self.play(url)
# vim:sw=4:ts=4:et:

View file

@ -3,10 +3,6 @@ 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
@ -18,7 +14,6 @@ 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:') \
@ -34,21 +29,6 @@ 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()
@ -79,16 +59,6 @@ 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({