Skip to content

Commit

Permalink
Use same response/error hooks for Youtube.perform_v3_request
Browse files Browse the repository at this point in the history
- Partially fixes #545
- Remove the other handle_error methods
  • Loading branch information
MoojMidge committed Dec 15, 2023
1 parent e9ed03f commit 860defd
Show file tree
Hide file tree
Showing 10 changed files with 422 additions and 214 deletions.
11 changes: 1 addition & 10 deletions resources/lib/youtube_plugin/kodion/utils/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,17 +348,11 @@ def run(self):
json_data = client.remove_video_from_playlist(
watch_later_id, playlist_item_id
)
_ = self.provider.v3_handle_error(self.provider,
self._context,
json_data)

history_playlist_id = access_manager.get_watch_history_id()
if history_playlist_id and history_playlist_id != 'HL':
json_data = client.add_video_to_playlist(history_playlist_id,
self.video_id)
_ = self.provider.v3_handle_error(self.provider,
self._context,
json_data)

# rate video
if settings.get_bool('youtube.post.play.rate', False):
Expand All @@ -370,10 +364,7 @@ def run(self):

if do_rating:
json_data = client.get_video_rating(self.video_id)
success = self.provider.v3_handle_error(self.provider,
self._context,
json_data)
if success:
if json_data:
items = json_data.get('items', [{'rating': 'none'}])
rating = items[0].get('rating', 'none')
if rating == 'none':
Expand Down
408 changes: 309 additions & 99 deletions resources/lib/youtube_plugin/youtube/client/youtube.py

Large diffs are not rendered by default.

34 changes: 0 additions & 34 deletions resources/lib/youtube_plugin/youtube/helper/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from ..helper import yt_context_menu
from ...kodion import KodionException
from ...kodion.items import DirectoryItem, NextPageItem, VideoItem
from ...kodion.utils import strip_html_from_text


def _process_list_response(provider, context, json_data):
Expand Down Expand Up @@ -434,39 +433,6 @@ def response_to_items(provider, context, json_data, sort=None, reverse=False, pr
return result


def handle_error(context, json_data):
if json_data and 'error' in json_data:
ok_dialog = False
message_timeout = 5000

message = strip_html_from_text(json_data['error'].get('message', ''))
log_message = strip_html_from_text(json_data['error'].get('message', ''))
reason = json_data['error']['errors'][0].get('reason', '')
title = '%s: %s' % (context.get_name(), reason)

context.log_error('Error reason: |%s| with message: |%s|' % (reason, log_message))

if reason == 'accessNotConfigured':
message = context.localize('key.requirement.notification')
ok_dialog = True

if reason == 'keyInvalid' and message == 'Bad Request':
message = context.localize('api.key.incorrect')
message_timeout = 7000

if reason in {'quotaExceeded', 'dailyLimitExceeded'}:
message_timeout = 7000

if ok_dialog:
context.get_ui().on_ok(title, message)
else:
context.get_ui().show_notification(message, title, time_ms=message_timeout)

return False

return True


def _parse_kind(item):
parts = item.get('kind', '').split('#')
is_youtube = parts[0] == 'youtube'
Expand Down
13 changes: 10 additions & 3 deletions resources/lib/youtube_plugin/youtube/helper/yt_play.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,18 @@ def play_channel_live(provider, context):
index = context.get_param('live') - 1
if index < 0:
index = 0
json_data = provider.get_client(context).search(q='', search_type='video', event_type='live', channel_id=channel_id, safe_search=False)
if not v3.handle_error(context, json_data):
json_data = provider.get_client(context).search(q='',
search_type='video',
event_type='live',
channel_id=channel_id,
safe_search=False)
if not json_data:
return False

video_items = v3.response_to_items(provider, context, json_data, process_next_page=False)
video_items = v3.response_to_items(provider,
context,
json_data,
process_next_page=False)

try:
video_item = video_items[index]
Expand Down
10 changes: 5 additions & 5 deletions resources/lib/youtube_plugin/youtube/helper/yt_playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def _process_add_video(provider, context, keymap_action=False):

if playlist_id != 'HL':
json_data = client.add_video_to_playlist(playlist_id=playlist_id, video_id=video_id)
if not v3.handle_error(context, json_data):
if not json_data:
return False

if playlist_id == watch_later_id:
Expand Down Expand Up @@ -96,7 +96,7 @@ def _process_remove_video(provider, context):
if context.get_ui().on_remove_content(video_name):
json_data = provider.get_client(context).remove_video_from_playlist(playlist_id=playlist_id,
playlist_item_id=video_id)
if not v3.handle_error(context, json_data):
if not json_data:
return False

context.get_ui().refresh_container()
Expand Down Expand Up @@ -128,7 +128,7 @@ def _process_remove_playlist(provider, context):

if context.get_ui().on_delete_content(playlist_name):
json_data = provider.get_client(context).remove_playlist(playlist_id=playlist_id)
if not v3.handle_error(context, json_data):
if not json_data:
return False

context.get_ui().refresh_container()
Expand Down Expand Up @@ -203,7 +203,7 @@ def _process_select_playlist(provider, context):
context.localize('playlist.create'))
if result and text:
json_data = client.create_playlist(title=text)
if not v3.handle_error(context, json_data):
if not json_data:
break

playlist_id = json_data.get('id', '')
Expand Down Expand Up @@ -236,7 +236,7 @@ def _process_rename_playlist(provider, context):
default=current_playlist_name)
if result and text:
json_data = provider.get_client(context).rename_playlist(playlist_id=playlist_id, new_title=text)
if not v3.handle_error(context, json_data):
if not json_data:
return

context.get_ui().refresh_container()
Expand Down
18 changes: 9 additions & 9 deletions resources/lib/youtube_plugin/youtube/helper/yt_specials.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def _process_related_videos(provider, context):
json_data = provider.get_client(context).get_related_videos(
video_id=video_id, page_token=context.get_param('page_token', '')
)
if not v3.handle_error(context, json_data):
if not json_data:
return False
return v3.response_to_items(provider,
context,
Expand All @@ -50,7 +50,7 @@ def _process_parent_comments(provider, context):
json_data = provider.get_client(context).get_parent_comments(
video_id=video_id, page_token=context.get_param('page_token', '')
)
if not v3.handle_error(context, json_data):
if not json_data:
return False
return v3.response_to_items(provider, context, json_data)

Expand All @@ -64,7 +64,7 @@ def _process_child_comments(provider, context):
json_data = provider.get_client(context).get_child_comments(
parent_id=parent_id, page_token=context.get_param('page_token', '')
)
if not v3.handle_error(context, json_data):
if not json_data:
return False
return v3.response_to_items(provider, context, json_data)

Expand All @@ -74,7 +74,7 @@ def _process_recommendations(provider, context):
json_data = provider.get_client(context).get_activities(
channel_id='home', page_token=context.get_param('page_token', '')
)
if not v3.handle_error(context, json_data):
if not json_data:
return False
return v3.response_to_items(provider, context, json_data)

Expand All @@ -84,7 +84,7 @@ def _process_popular_right_now(provider, context):
json_data = provider.get_client(context).get_popular_videos(
page_token=context.get_param('page_token', '')
)
if not v3.handle_error(context, json_data):
if not json_data:
return False
return v3.response_to_items(provider, context, json_data)

Expand All @@ -95,14 +95,14 @@ def _process_browse_channels(provider, context):
guide_id = context.get_param('guide_id', '')
if guide_id:
json_data = client.get_guide_category(guide_id)
if not v3.handle_error(context, json_data):
if not json_data:
return False
return v3.response_to_items(provider, context, json_data)

function_cache = context.get_function_cache()
json_data = function_cache.get(client.get_guide_categories,
function_cache.ONE_MONTH)
if not v3.handle_error(context, json_data):
if not json_data:
return False
return v3.response_to_items(provider, context, json_data)

Expand All @@ -112,7 +112,7 @@ def _process_disliked_videos(provider, context):
json_data = provider.get_client(context).get_disliked_videos(
page_token=context.get_param('page_token', '')
)
if not v3.handle_error(context, json_data):
if not json_data:
return False
return v3.response_to_items(provider, context, json_data)

Expand All @@ -128,7 +128,7 @@ def _sort(x):
page_token=context.get_param('page_token', ''),
location=context.get_param('location', False),
)
if not v3.handle_error(context, json_data):
if not json_data:
return False
return v3.response_to_items(provider, context, json_data, sort=_sort)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def _process_list(provider, context):
page_token = context.get_param('page_token', '')
# no caching
json_data = provider.get_client(context).get_subscription('mine', page_token=page_token)
if not v3.handle_error(context, json_data):
if not json_data:
return []
result.extend(v3.response_to_items(provider, context, json_data))

Expand All @@ -38,7 +38,7 @@ def _process_add(provider, context):

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

context.get_ui().show_notification(
Expand All @@ -61,7 +61,7 @@ def _process_remove(provider, context):

if subscription_id:
json_data = provider.get_client(context).unsubscribe(subscription_id)
if not v3.handle_error(context, json_data):
if not json_data:
return False

context.get_ui().refresh_container()
Expand Down
2 changes: 1 addition & 1 deletion resources/lib/youtube_plugin/youtube/helper/yt_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def _process_rate_video(provider, context, re_match):
if not current_rating:
client = provider.get_client(context)
json_data = client.get_video_rating(video_id)
if not v3.handle_error(context, json_data):
if not json_data:
return False

items = json_data.get('items', [])
Expand Down
13 changes: 6 additions & 7 deletions resources/lib/youtube_plugin/youtube/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def __init__(self):
self._client = None
self._is_logged_in = False

self.v3_handle_error = v3.handle_error
self.yt_video = yt_video

def get_wizard_supported_views(self):
Expand Down Expand Up @@ -354,7 +353,7 @@ def _on_channel_playlists(self, context, re_match):

# no caching
json_data = self.get_client(context).get_playlists_of_channel(channel_id, page_token)
if not v3.handle_error(context, json_data):
if not json_data:
return False
result.extend(v3.response_to_items(self, context, json_data))

Expand All @@ -377,7 +376,7 @@ def _on_channel_live(self, context, re_match):

# no caching
json_data = self.get_client(context).search(q='', search_type='video', event_type='live', channel_id=channel_id, page_token=page_token, safe_search=safe_search)
if not v3.handle_error(context, json_data):
if not json_data:
return False
result.extend(v3.response_to_items(self, context, json_data))

Expand Down Expand Up @@ -428,7 +427,7 @@ def _on_channel(self, context, re_match):
json_data = function_cache.get(client.get_channel_by_username,
function_cache.ONE_DAY,
channel_id)
if not v3.handle_error(context, json_data):
if not json_data:
return False

# we correct the channel id based on the username
Expand Down Expand Up @@ -490,7 +489,7 @@ def _on_channel(self, context, re_match):
function_cache.ONE_MINUTE * 5,
upload_playlist,
page_token=page_token)
if not v3.handle_error(context, json_data):
if not json_data:
return False

result.extend(v3.response_to_items(self, context, json_data))
Expand Down Expand Up @@ -821,7 +820,7 @@ def _search_channel_or_playlist(self, context, id_string):
elif re.match(r'[OP]L[0-9a-zA-Z_\-]{30,40}', id_string):
json_data = self.get_client(context).get_playlists(id_string)

if not json_data or not v3.handle_error(context, json_data):
if not json_data:
return []

result.extend(v3.response_to_items(self, context, json_data))
Expand Down Expand Up @@ -893,7 +892,7 @@ def on_search(self, search_text, context, re_match):
page_token=page_token,
channel_id=channel_id,
location=location)
if not v3.handle_error(context, json_data):
if not json_data:
return False
result.extend(v3.response_to_items(self, context, json_data))
return result
Expand Down
Loading

0 comments on commit 860defd

Please sign in to comment.