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
This commit is contained in:
Fabio Manganiello 2021-10-17 16:03:57 +02:00
parent cb4cfa40d8
commit 4a5bb766af
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774
4 changed files with 18 additions and 10 deletions

View file

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

View file

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

View file

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

View file

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