From c943acb9c22a2d56da1e30a383d9d78febf19339 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 4 Jul 2020 00:11:41 +0200 Subject: [PATCH] Smarter logic for parsing YouTube URLs in Kodi --- platypush/plugins/media/__init__.py | 17 +++++++++++++++++ platypush/plugins/media/kodi.py | 18 ++++++------------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/platypush/plugins/media/__init__.py b/platypush/plugins/media/__init__.py index 312e613c..fd566350 100644 --- a/platypush/plugins/media/__init__.py +++ b/platypush/plugins/media/__init__.py @@ -478,6 +478,23 @@ class MediaPlugin(Plugin): threading.Thread(target=_youtube_dl_thread).start() + @action + def get_youtube_id(self, url: str) -> Optional[str]: + patterns = [ + re.compile(pattern) + for pattern in [ + 'https?://www.youtube.com/watch?v=([^&#]+)' + 'https?://youtube.com/watch?v=([^&#]+)' + 'https?://youtu.be/([^&#/]+)' + 'youtube:video:([^&#:])' + ] + ] + + for pattern in patterns: + m = re.match(pattern, url) + if m: + return m.group(1) + @action def get_youtube_url(self, url): m = re.match('youtube:video:(.*)', url) diff --git a/platypush/plugins/media/kodi.py b/platypush/plugins/media/kodi.py index 84fe825c..0d3eb86c 100644 --- a/platypush/plugins/media/kodi.py +++ b/platypush/plugins/media/kodi.py @@ -155,18 +155,12 @@ class MediaKodiPlugin(MediaPlugin): :param resource: URL or path to the media to be played """ - if resource.startswith('youtube:video:') \ - or resource.startswith('https://www.youtube.com/watch?v='): - m1 = re.match('youtube:video:([^:?&]+)', resource) - m2 = re.match('https://www.youtube.com/watch?v=([^:?&#/]+)', resource) - youtube_id = None - - if m1: - youtube_id = m1.group(1) - elif m2: - youtube_id = m2.group(1) - - if youtube_id: + youtube_id = self.get_youtube_id(resource) + if youtube_id: + try: + resource = self.get_youtube_url('https://www.youtube.com/watch?v=' + youtube_id) + except Exception as e: + self.logger.warning('youtube-dl error, falling back to Kodi YouTube plugin: {}'.format(str(e))) resource = 'plugin://plugin.video.youtube/?action=play_video&videoid=' + youtube_id if resource.startswith('file://'):