Smarter logic for parsing YouTube URLs in Kodi

This commit is contained in:
Fabio Manganiello 2020-07-04 00:11:41 +02:00
parent a4824a4e95
commit c943acb9c2
2 changed files with 23 additions and 12 deletions

View file

@ -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)

View file

@ -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://'):