diff --git a/platypush/plugins/media/__init__.py b/platypush/plugins/media/__init__.py index 312e613cc..fd566350f 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 84fe825c8..0d3eb86cb 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://'):