Skip to content

Commit

Permalink
Avoid unnecessary duplicated client creation/checks in yt_subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
MoojMidge committed May 26, 2024
1 parent da341b0 commit 797ebf6
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions resources/lib/youtube_plugin/youtube/helper/yt_subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,39 @@
from ...kodion.items import UriItem


def _process_list(provider, context):
json_data = provider.get_client(context).get_subscription(
def _process_list(provider, context, client):
json_data = client.get_subscription(
'mine', page_token=context.get_param('page_token', '')
)
if not json_data:
return []
return v3.response_to_items(provider, context, json_data)


def _process_add(provider, context):
def _process_add(_provider, context, client):
listitem_subscription_id = context.get_listitem_detail('subscription_id')

subscription_id = context.get_param('subscription_id', '')
if (not subscription_id and listitem_subscription_id
and listitem_subscription_id.lower().startswith('uc')):
subscription_id = listitem_subscription_id

if subscription_id:
json_data = provider.get_client(context).subscribe(subscription_id)
if not json_data:
return False

context.get_ui().show_notification(
context.localize('subscribed.to.channel'),
time_ms=2500,
audible=False
)
if not subscription_id:
return False

return True
json_data = client.subscribe(subscription_id)
if not json_data:
return False

return False
context.get_ui().show_notification(
context.localize('subscribed.to.channel'),
time_ms=2500,
audible=False
)
return True


def _process_remove(provider, context):
def _process_remove(_provider, context, client):
listitem_subscription_id = context.get_listitem_detail('channel_subscription_id')
listitem_channel_id = context.get_listitem_detail('channel_id')

Expand All @@ -61,9 +60,9 @@ def _process_remove(provider, context):
channel_id = listitem_channel_id

if subscription_id:
success = provider.get_client(context).unsubscribe(subscription_id)
success = client.unsubscribe(subscription_id)
elif channel_id:
success = provider.get_client(context).unsubscribe_channel(channel_id)
success = client.unsubscribe_channel(channel_id)
else:
success = False

Expand All @@ -81,14 +80,14 @@ def _process_remove(provider, context):

def process(method, provider, context):
# we need a login
_ = provider.get_client(context)
client = provider.get_client(context)
if not provider.is_logged_in():
return UriItem(context.create_uri(('sign', 'in')))

if method == 'list':
return _process_list(provider, context)
return _process_list(provider, context, client)
if method == 'add':
return _process_add(provider, context)
return _process_add(provider, context, client)
if method == 'remove':
return _process_remove(provider, context)
return _process_remove(provider, context, client)
raise KodionException("Unknown subscriptions method '%s'" % method)

0 comments on commit 797ebf6

Please sign in to comment.