Verified Commit 4a5bb766 authored by Fabio Manganiello's avatar Fabio Manganiello
Browse files

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
parent cb4cfa40
Pipeline #79 passed with stages
in 5 minutes and 26 seconds
......@@ -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
......
......@@ -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):
......
......@@ -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
......
......@@ -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]):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment