Added YouTube search only method

This commit is contained in:
Fabio Manganiello 2018-04-19 10:18:46 +02:00
parent 23095bc18d
commit 84ab37e44e

View file

@ -25,6 +25,8 @@ class VideoOmxplayerPlugin(Plugin):
or resource.startswith('https://www.youtube.com/watch?v='): or resource.startswith('https://www.youtube.com/watch?v='):
resource = self._get_youtube_content(resource) resource = self._get_youtube_content(resource)
logging.info('Playing {}'.format(resource))
try: try:
self.player = OMXPlayer(resource, args=self.args) self.player = OMXPlayer(resource, args=self.args)
except DBusException as e: except DBusException as e:
@ -70,7 +72,6 @@ class VideoOmxplayerPlugin(Plugin):
if self.videos_queue: if self.videos_queue:
video = self.videos_queue.pop(0) video = self.videos_queue.pop(0)
logging.info('Playing {}'.format(video))
return self.play(video) return self.play(video)
return Response(output={'status': 'no media'}, errors = []) return Response(output={'status': 'no media'}, errors = [])
@ -90,20 +91,26 @@ class VideoOmxplayerPlugin(Plugin):
})) }))
def youtube_search_and_play(self, query): def youtube_search_and_play(self, query):
self.videos_queue = self.youtube_search(query)
url = self.videos_queue.pop(0)
logging.info('Playing {}'.format(url))
return self.play(url)
def youtube_search(self, query):
query = urllib.parse.quote(query) query = urllib.parse.quote(query)
url = "https://www.youtube.com/results?search_query=" + query url = "https://www.youtube.com/results?search_query=" + query
response = urllib.request.urlopen(url) response = urllib.request.urlopen(url)
html = response.read() html = response.read()
soup = BeautifulSoup(html, 'lxml') soup = BeautifulSoup(html, 'lxml')
self.videos_queue = [] results = []
for vid in soup.findAll(attrs={'class':'yt-uix-tile-link'}): for vid in soup.findAll(attrs={'class':'yt-uix-tile-link'}):
if vid['href'].startswith('/watch?v='): if vid['href'].startswith('/watch?v='):
self.videos_queue.append('https://www.youtube.com' + vid['href']) results.append('https://www.youtube.com' + vid['href'])
logging.info('{} YouTube video results for the search query "{}"'.format(query))
return results
url = self.videos_queue.pop(0)
logging.info('Playing {}'.format(url))
return self.play(url)
@classmethod @classmethod
def _get_youtube_content(cls, url): def _get_youtube_content(cls, url):