From b716b1a0c138548327519bb8509f72287e511451 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 4 Jul 2020 00:25:49 +0200 Subject: [PATCH] More robust URL query for YouTube --- platypush/plugins/media/__init__.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/platypush/plugins/media/__init__.py b/platypush/plugins/media/__init__.py index 3fef9d476..25bea3361 100644 --- a/platypush/plugins/media/__init__.py +++ b/platypush/plugins/media/__init__.py @@ -483,27 +483,26 @@ class MediaPlugin(Plugin): patterns = [ re.compile(pattern) for pattern in [ - r'https?://www.youtube.com/watch?v=([^&#]+)' - r'https?://youtube.com/watch?v=([^&#]+)' + r'https?://www.youtube.com/watch\?v=([^&#]+)' + r'https?://youtube.com/watch\?v=([^&#]+)' r'https?://youtu.be/([^&#/]+)' r'youtube:video:([^&#:])' ] ] for pattern in patterns: - m = pattern.match(url) + m = pattern.search(url) if m: return m.group(1) @action def get_youtube_url(self, url): - m = re.match('youtube:video:(.*)', url) - if m: - url = 'https://www.youtube.com/watch?v={}'.format(m.group(1)) - - proc = subprocess.Popen(['youtube-dl', '-f', 'best', '-g', url], stdout=subprocess.PIPE) - raw_url = proc.stdout.read().decode("utf-8", "strict")[:-1] - return raw_url if raw_url else url + youtube_id = self.get_youtube_id(url) + if youtube_id: + url = 'https://www.youtube.com/watch?v={}'.format(youtube_id) + proc = subprocess.Popen(['youtube-dl', '-f', 'best', '-g', url], stdout=subprocess.PIPE) + raw_url = proc.stdout.read().decode("utf-8", "strict")[:-1] + return raw_url if raw_url else url @action def get_youtube_info(self, url):