From 4a5bb766af4f4f4dfc1e98e509b1246be6cd0cf0 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 17 Oct 2021 16:03:57 +0200 Subject: [PATCH] Fixed some media plugins inconsistencies - Removed replication of logic between media.get_youtube_url (action) and media.get_youtube_video_url (internal method) - Added differentiation between MediaPlayEvent and MediaResumeEvent --- platypush/message/event/media.py | 9 +++++++++ platypush/plugins/media/__init__.py | 12 +++++------- platypush/plugins/media/kodi/__init__.py | 2 +- platypush/plugins/media/mpv/__init__.py | 5 +++-- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/platypush/message/event/media.py b/platypush/message/event/media.py index 01843bfe6..5ca0e77e0 100644 --- a/platypush/message/event/media.py +++ b/platypush/message/event/media.py @@ -54,6 +54,15 @@ class MediaPauseEvent(MediaEvent): super().__init__(*args, player=player, plugin=plugin, **kwargs) +class MediaResumeEvent(MediaEvent): + """ + Event triggered when a media playback is resumed + """ + + def __init__(self, player=None, plugin=None, *args, **kwargs): + super().__init__(*args, player=player, plugin=plugin, **kwargs) + + class MediaSeekEvent(MediaEvent): """ Event triggered when the time position in the media changes diff --git a/platypush/plugins/media/__init__.py b/platypush/plugins/media/__init__.py index 8b4f39540..f3db96a43 100644 --- a/platypush/plugins/media/__init__.py +++ b/platypush/plugins/media/__init__.py @@ -501,8 +501,10 @@ class MediaPlugin(Plugin, ABC): # noinspection PyProtectedMember return YoutubeMediaSearcher()._youtube_search_html_parse(query) - def get_youtube_video_url(self, url): - youtube_dl = subprocess.Popen(['youtube-dl', '-f', self.youtube_format, '-g', url], stdout=subprocess.PIPE) + def get_youtube_video_url(self, url, youtube_format: Optional[str] = None): + ytdl_cmd = ['youtube-dl', '-f', youtube_format or self.youtube_format, '-g', url] + self.logger.info(f'Executing command {" ".join(ytdl_cmd)}') + youtube_dl = subprocess.Popen(ytdl_cmd, stdout=subprocess.PIPE) url = youtube_dl.communicate()[0].decode().strip() youtube_dl.wait() return url @@ -529,11 +531,7 @@ class MediaPlugin(Plugin, ABC): 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', youtube_format or self.youtube_format, '-g', url], stdout=subprocess.PIPE - ) - raw_url = proc.stdout.read().decode("utf-8", "strict")[:-1] - return raw_url if raw_url else url + return self.get_youtube_video_url(url, youtube_format=youtube_format) @action def get_youtube_info(self, url): diff --git a/platypush/plugins/media/kodi/__init__.py b/platypush/plugins/media/kodi/__init__.py index c5cedc0cc..b56694814 100644 --- a/platypush/plugins/media/kodi/__init__.py +++ b/platypush/plugins/media/kodi/__init__.py @@ -158,7 +158,7 @@ class MediaKodiPlugin(MediaPlugin): youtube_id = self.get_youtube_id(resource) if youtube_id: try: - resource = self.get_youtube_url('https://www.youtube.com/watch?v=' + youtube_id).output + resource = self.get_youtube_url(youtube_id).output 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 diff --git a/platypush/plugins/media/mpv/__init__.py b/platypush/plugins/media/mpv/__init__.py index 0ffc09571..0de1a9675 100644 --- a/platypush/plugins/media/mpv/__init__.py +++ b/platypush/plugins/media/mpv/__init__.py @@ -4,7 +4,7 @@ import threading from platypush.context import get_bus from platypush.plugins.media import PlayerState, MediaPlugin from platypush.message.event.media import MediaPlayEvent, MediaPlayRequestEvent, \ - MediaPauseEvent, MediaStopEvent, NewPlayingMediaEvent, MediaSeekEvent + MediaPauseEvent, MediaStopEvent, NewPlayingMediaEvent, MediaSeekEvent, MediaResumeEvent from platypush.plugins import action @@ -81,10 +81,11 @@ class MediaMpvPlugin(MediaPlugin): title=self._player.filename) elif evt == Event.PLAYBACK_RESTART: self._playback_rebounce_event.set() + self._post_event(MediaPlayEvent, resource=self._get_current_resource(), title=self._player.filename) elif evt == Event.PAUSE: self._post_event(MediaPauseEvent, resource=self._get_current_resource(), title=self._player.filename) elif evt == Event.UNPAUSE: - self._post_event(MediaPlayEvent, resource=self._get_current_resource(), title=self._player.filename) + self._post_event(MediaResumeEvent, resource=self._get_current_resource(), title=self._player.filename) elif evt == Event.SHUTDOWN or evt == Event.IDLE or ( evt == Event.END_FILE and event.get('event', {}).get('reason') in [EndFile.EOF, EndFile.ABORTED, EndFile.QUIT]):