From 7e119654f9a3c10d4bfa956f4637038902ebe78f Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 20 Apr 2018 09:38:04 +0200 Subject: [PATCH] Retry with the next YouTube video until some of the ones from the search can be played without errors --- platypush/plugins/video/omxplayer.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/platypush/plugins/video/omxplayer.py b/platypush/plugins/video/omxplayer.py index e211aefa12..037c8fa459 100644 --- a/platypush/plugins/video/omxplayer.py +++ b/platypush/plugins/video/omxplayer.py @@ -112,9 +112,20 @@ class VideoOmxplayerPlugin(Plugin): 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) + ret = None + + while self.videos_queue: + url = self.videos_queue.pop(0) + logging.info('Playing {}'.format(url)) + + try: + ret = self.play(url) + break + except Exception as e: + logging.exception(e) + logging.info('YouTube playback error, trying next video') + + return ret def youtube_search(self, query): query = urllib.parse.quote(query) @@ -125,8 +136,9 @@ class VideoOmxplayerPlugin(Plugin): results = [] for vid in soup.findAll(attrs={'class':'yt-uix-tile-link'}): - if vid['href'].startswith('/watch?v='): - results.append('https://www.youtube.com' + vid['href']) + m = re.match('(/watch?v=.+?)(&.*)', vid['href']) + if m: + results.append('https://www.youtube.com' + m.group(1)) logging.info('{} YouTube video results for the search query "{}"' .format(len(results), query))