Skip to content

Commit

Permalink
Add filler parameter to v3.response_to_items
Browse files Browse the repository at this point in the history
- Allows listings to be recursively filled after post request filtering
  • Loading branch information
MoojMidge committed Nov 25, 2024
1 parent e838c2b commit 7c39a97
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
16 changes: 13 additions & 3 deletions resources/lib/youtube_plugin/youtube/client/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -1592,9 +1592,19 @@ def callback(item, _all_criteria=all_criteria):
else:
return True
return False
def filler():
return self.get_my_subscriptions(
page_token=(page_token + 1),
logged_in=logged_in,
do_filter=do_filter,
refresh=refresh,
use_subscriptions_cache=True,
progress_dialog=progress_dialog,
**kwargs)
else:
channel_filters = None
callback = None
filler = None

page = page_token or 1
totals = {
Expand Down Expand Up @@ -1988,18 +1998,18 @@ def _threaded_fetch(kwargs,
progress_dialog=progress_dialog,
)
if not items:
return None, None
return None, None, None

if totals['num'] > totals['end']:
v3_response['nextPageToken'] = page + 1
if totals['num'] > totals['start']:
items = items[totals['start']:min(totals['num'], totals['end'])]
else:
return None, None
return None, None, None

v3_response['pageInfo']['totalResults'] = totals['num']
v3_response['items'] = items
return v3_response, callback
return v3_response, callback, filler

def get_saved_playlists(self, page_token, offset):
if not page_token:
Expand Down
32 changes: 29 additions & 3 deletions resources/lib/youtube_plugin/youtube/helper/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ def response_to_items(provider,
process_next_page=True,
item_filter=None,
callback=None,
filler=None,
progress_dialog=None):
kind, is_youtube, is_plugin, kind_type = _parse_kind(json_data)
if not is_youtube and not is_plugin:
Expand Down Expand Up @@ -566,14 +567,40 @@ def response_to_items(provider,
else:
raise KodionException('Unknown kind: %s' % kind)

current_page = params.get('page')
if item_filter or do_callbacks or callback:
items = filter_videos(items, callback=callback, **item_filter)
num_original_items = len(items)
filtered_items = filter_videos(items, callback=callback, **item_filter)
remaining = num_original_items - len(filtered_items)
while remaining > 0 and filler:
json_data, callback, filler = filler()
if not json_data:
break
result = _process_list_response(
provider,
context,
json_data,
item_filter=item_filter,
progress_dialog=progress_dialog,
)
if not result:
break
items, do_callbacks = result
if not items:
break
if item_filter or do_callbacks or callback:
items = filter_videos(items, callback=callback, **item_filter)
if items:
filtered_items.extend(items[:remaining])
remaining = num_original_items - len(filtered_items)
current_page = current_page + 1 if current_page else 2
items = filtered_items

if sort is not None:
items.sort(key=sort, reverse=reverse)

# no processing of next page item
if not process_next_page or params.get('hide_next_page'):
if not json_data or not process_next_page or params.get('hide_next_page'):
return items

# next page
Expand All @@ -584,7 +611,6 @@ def response_to_items(provider,
We implemented our own calculation for the token into the YouTube client
This should work for up to ~2000 entries.
"""
current_page = params.get('page')
next_page = current_page + 1 if current_page else 2
new_params = dict(params, page=next_page)

Expand Down
3 changes: 2 additions & 1 deletion resources/lib/youtube_plugin/youtube/helper/yt_specials.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def _process_my_subscriptions(provider, context, client, filtered=False):
),
) as progress_dialog:
params = context.get_params()
json_data, callback = client.get_my_subscriptions(
json_data, callback, filler = client.get_my_subscriptions(
page_token=params.get('page', 1),
logged_in=provider.is_logged_in(),
do_filter=filtered,
Expand All @@ -318,6 +318,7 @@ def _process_my_subscriptions(provider, context, client, filtered=False):
json_data,
progress_dialog=progress_dialog,
callback=callback,
filler=filler,
)


Expand Down

0 comments on commit 7c39a97

Please sign in to comment.