Skip to content

Commit

Permalink
Move plugin play route processing from provider to yt_play module
Browse files Browse the repository at this point in the history
  • Loading branch information
MoojMidge committed Jul 6, 2024
1 parent 72d7d11 commit 464064e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 62 deletions.
61 changes: 57 additions & 4 deletions resources/lib/youtube_plugin/youtube/helper/yt_play.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@
PLAYER_DATA,
PLAY_FORCE_AUDIO,
PLAY_PROMPT_QUALITY,
PLAY_PROMPT_SUBTITLES,
PLAY_WITH,
SERVER_POST_START,
SERVER_WAKEUP,
)
from ...kodion.items import VideoItem
from ...kodion.network import get_connect_address
from ...kodion.utils import select_stream
from ...kodion.utils import find_video_id, select_stream


def play_video(provider, context):
def _play_video(provider, context):
ui = context.get_ui()
params = context.get_params()
video_id = params.get('video_id')
Expand Down Expand Up @@ -157,7 +160,7 @@ def play_video(provider, context):
return video_item


def play_playlist(provider, context):
def _play_playlist(provider, context):
videos = []
params = context.get_params()

Expand Down Expand Up @@ -260,7 +263,7 @@ def play_playlist(provider, context):
return videos[playlist_position], options


def play_channel_live(provider, context):
def _play_channel_live(provider, context):
channel_id = context.get_param('channel_id')
index = context.get_param('live', 1) - 1
if index < 0:
Expand Down Expand Up @@ -294,3 +297,53 @@ def play_channel_live(provider, context):
player.play(playlist_index=0)
return False
return video_item


def process(provider, context, _re_match):
ui = context.get_ui()

params = context.get_params()
param_keys = params.keys()

if ({'channel_id', 'playlist_id', 'playlist_ids', 'video_id'}
.isdisjoint(param_keys)):
listitem_path = context.get_listitem_info('FileNameAndPath')
if context.is_plugin_path(listitem_path, PATHS.PLAY):
video_id = find_video_id(listitem_path)
if video_id:
context.set_param('video_id', video_id)
params['video_id'] = video_id
else:
return False
else:
return False

video_id = params.get('video_id')
playlist_id = params.get('playlist_id')

force_play = False
for param in {PLAY_FORCE_AUDIO,
PLAY_PROMPT_QUALITY,
PLAY_PROMPT_SUBTITLES,
PLAY_WITH}.intersection(param_keys):
del params[param]
ui.set_property(param)
force_play = True

if video_id and not playlist_id:
# This is required to trigger Kodi resume prompt, along with using
# RunPlugin. Prompt will not be used if using PlayMedia
if force_play:
context.execute('Action(Play)')
return False
context.wakeup(SERVER_WAKEUP, timeout=5)
video = _play_video(provider, context)
ui.set_property(SERVER_POST_START)
return video

if playlist_id or 'playlist_ids' in params:
return _play_playlist(provider, context)

if 'channel_id' in params:
return _play_channel_live(provider, context)
return False
64 changes: 6 additions & 58 deletions resources/lib/youtube_plugin/youtube/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@
CONTENT,
DEVELOPER_CONFIGS,
PATHS,
PLAY_FORCE_AUDIO,
PLAY_PROMPT_QUALITY,
PLAY_PROMPT_SUBTITLES,
PLAY_WITH,
SERVER_POST_START,
SERVER_WAKEUP,
)
from ..kodion.items import (
BaseItem,
Expand All @@ -53,7 +47,7 @@
UriItem,
menu_items,
)
from ..kodion.utils import find_video_id, strip_html_from_text
from ..kodion.utils import strip_html_from_text


class Provider(AbstractProvider):
Expand All @@ -74,6 +68,11 @@ def __init__(self):
yt_playlist.process,
)

self.register_path(
'^/play/?$',
yt_play.process,
)

self.register_path(
'^/special/(?P<category>[^/]+)/?$',
yt_specials.process,
Expand Down Expand Up @@ -675,57 +674,6 @@ def _on_my_location(self, context, re_match):
streams. 1 (default) for first live stream
"""

# noinspection PyUnusedLocal
@RegisterProviderPath('^/play/?$')
def on_play(self, context, re_match):
ui = context.get_ui()

params = context.get_params()
param_keys = params.keys()

if ({'channel_id', 'playlist_id', 'playlist_ids', 'video_id'}
.isdisjoint(param_keys)):
listitem_path = context.get_listitem_info('FileNameAndPath')
if context.is_plugin_path(listitem_path, PATHS.PLAY):
video_id = find_video_id(listitem_path)
if video_id:
context.set_param('video_id', video_id)
params['video_id'] = video_id
else:
return False
else:
return False

video_id = params.get('video_id')
playlist_id = params.get('playlist_id')

force_play = False
for param in {PLAY_FORCE_AUDIO,
PLAY_PROMPT_QUALITY,
PLAY_PROMPT_SUBTITLES,
PLAY_WITH}.intersection(param_keys):
del params[param]
ui.set_property(param)
force_play = True

if video_id and not playlist_id:
# This is required to trigger Kodi resume prompt, along with using
# RunPlugin. Prompt will not be used if using PlayMedia
if force_play:
context.execute('Action(Play)')
return False
context.wakeup(SERVER_WAKEUP, timeout=5)
video = yt_play.play_video(self, context)
ui.set_property(SERVER_POST_START)
return video

if playlist_id or 'playlist_ids' in params:
return yt_play.play_playlist(self, context)

if 'channel_id' in params:
return yt_play.play_channel_live(self, context)
return False

@RegisterProviderPath('^/users/(?P<action>[^/]+)/?$')
def _on_users(self, _context, re_match):
action = re_match.group('action')
Expand Down

0 comments on commit 464064e

Please sign in to comment.