Skip to content

Commit

Permalink
Add items_per_page query parameter to allow number of items in widget…
Browse files Browse the repository at this point in the history
…s to be customised anxdpanic#896
  • Loading branch information
MoojMidge committed Sep 13, 2024
1 parent bf19eee commit ffc0bf0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class AbstractContext(object):
}
_INT_PARAMS = {
'fanart_type',
'items_per_page',
'live',
'next_page_token',
'offset',
Expand Down
58 changes: 29 additions & 29 deletions resources/lib/youtube_plugin/youtube/client/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ def __init__(self, context, **kwargs):

super(YouTube, self).__init__(context=context, **kwargs)

def get_max_results(self):
return self._max_results
def max_results(self):
return self._context.get_param('items_per_page') or self._max_results

def get_language(self):
return self._language
Expand Down Expand Up @@ -348,7 +348,7 @@ def get_subscription(self,
:return:
"""
params = {'part': 'snippet',
'maxResults': str(self._max_results),
'maxResults': str(self.max_results()),
'order': order}
if channel_id == 'mine':
params['mine'] = 'true'
Expand All @@ -364,7 +364,7 @@ def get_subscription(self,

def get_guide_category(self, guide_category_id, page_token='', **kwargs):
params = {'part': 'snippet,contentDetails,brandingSettings',
'maxResults': str(self._max_results),
'maxResults': str(self.max_results()),
'categoryId': guide_category_id,
'regionCode': self._region,
'hl': self._language}
Expand All @@ -377,7 +377,7 @@ def get_guide_category(self, guide_category_id, page_token='', **kwargs):

def get_guide_categories(self, page_token='', **kwargs):
params = {'part': 'snippet',
'maxResults': str(self._max_results),
'maxResults': str(self.max_results()),
'regionCode': self._region,
'hl': self._language}
if page_token:
Expand All @@ -390,7 +390,7 @@ def get_guide_categories(self, page_token='', **kwargs):

def get_trending_videos(self, page_token='', **kwargs):
params = {'part': 'snippet,status',
'maxResults': str(self._max_results),
'maxResults': str(self.max_results()),
'regionCode': self._region,
'hl': self._language,
'chart': 'mostPopular'}
Expand All @@ -403,7 +403,7 @@ def get_trending_videos(self, page_token='', **kwargs):

def get_video_category(self, video_category_id, page_token='', **kwargs):
params = {'part': 'snippet,contentDetails,status',
'maxResults': str(self._max_results),
'maxResults': str(self.max_results()),
'videoCategoryId': video_category_id,
'chart': 'mostPopular',
'regionCode': self._region,
Expand All @@ -417,7 +417,7 @@ def get_video_category(self, video_category_id, page_token='', **kwargs):

def get_video_categories(self, page_token='', **kwargs):
params = {'part': 'snippet',
'maxResults': str(self._max_results),
'maxResults': str(self.max_results()),
'regionCode': self._region,
'hl': self._language}
if page_token:
Expand Down Expand Up @@ -604,7 +604,7 @@ def get_related_for_home(self, page_token='', refresh=False):
# Increase value to recursively retrieve recommendations for the first
# recommended video, up to the set maximum recursion depth
max_depth = 2
items_per_page = self._max_results
items_per_page = self.max_results()
diversity_limits = items_per_page // (num_items * max_depth)
items = [[] for _ in range(max_depth * len(video_ids))]
counts = {
Expand Down Expand Up @@ -811,7 +811,7 @@ def rank_and_sort(item):

def get_activities(self, channel_id, page_token='', **kwargs):
params = {'part': 'snippet,contentDetails',
'maxResults': str(self._max_results),
'maxResults': str(self.max_results()),
'regionCode': self._region,
'hl': self._language}

Expand Down Expand Up @@ -844,7 +844,7 @@ def get_channel_sections(self, channel_id, **kwargs):

def get_playlists_of_channel(self, channel_id, page_token='', **kwargs):
params = {'part': 'snippet',
'maxResults': str(self._max_results)}
'maxResults': str(self.max_results())}
if channel_id != 'mine':
params['channelId'] = channel_id
else:
Expand All @@ -864,7 +864,7 @@ def get_playlist_item_id_of_video_id(self,
json_data = self.get_playlist_items(
playlist_id=playlist_id,
page_token=page_token,
max_results=50,
max_results=self.max_results(),
)
if not json_data:
return None
Expand All @@ -890,7 +890,7 @@ def get_playlist_items(self,
**kwargs):
# prepare params
if max_results is None:
max_results = self._max_results
max_results = self.max_results()
params = {'part': 'snippet',
'maxResults': str(max_results),
'playlistId': playlist_id}
Expand Down Expand Up @@ -954,7 +954,7 @@ def get_disliked_videos(self, page_token='', **kwargs):
# prepare params
params = {'part': 'snippet,status',
'myRating': 'dislike',
'maxResults': str(self._max_results)}
'maxResults': str(self.max_results())}
if page_token:
params['pageToken'] = page_token

Expand Down Expand Up @@ -1022,7 +1022,7 @@ def get_live_events(self,
'regionCode': self._region,
'hl': self._language,
'relevanceLanguage': self._language,
'maxResults': str(self._max_results)}
'maxResults': str(self.max_results())}

if location:
settings = self._context.get_settings()
Expand All @@ -1049,7 +1049,7 @@ def get_related_videos(self,
offset=0,
retry=0,
**kwargs):
max_results = self._max_results if max_results <= 0 else max_results
max_results = self.max_results() if max_results <= 0 else max_results

post_data = {'videoId': video_id}
if page_token:
Expand Down Expand Up @@ -1248,7 +1248,7 @@ def get_parent_comments(self,
page_token='',
max_results=0,
**kwargs):
max_results = self._max_results if max_results <= 0 else max_results
max_results = self.max_results() if max_results <= 0 else max_results

# prepare params
params = {'part': 'snippet',
Expand All @@ -1270,7 +1270,7 @@ def get_child_comments(self,
page_token='',
max_results=0,
**kwargs):
max_results = self._max_results if max_results <= 0 else max_results
max_results = self.max_results() if max_results <= 0 else max_results

# prepare params
params = {'part': 'snippet',
Expand All @@ -1293,7 +1293,7 @@ def get_channel_videos(self, channel_id, page_token='', **kwargs):

params = {'part': 'snippet',
'hl': self._language,
'maxResults': str(self._max_results),
'maxResults': str(self.max_results()),
'type': 'video',
'safeSearch': 'none',
'order': 'date'}
Expand Down Expand Up @@ -1355,7 +1355,7 @@ def search(self,
'regionCode': self._region,
'hl': self._language,
'relevanceLanguage': self._language,
'maxResults': str(self._max_results)}
'maxResults': str(self.max_results())}

if event_type and event_type in {'live', 'upcoming', 'completed'}:
params['eventType'] = event_type
Expand Down Expand Up @@ -1405,7 +1405,7 @@ def get_my_subscriptions(self,
'items': [],
'pageInfo': {
'totalResults': 0,
'resultsPerPage': self._max_results,
'resultsPerPage': self.max_results(),
},
}

Expand All @@ -1430,8 +1430,8 @@ def get_my_subscriptions(self,
page = page_token or 1
totals = {
'num': 0,
'start': -self._max_results,
'end': page * self._max_results,
'start': -self.max_results(),
'end': page * self.max_results(),
'video_ids': set(),
}
totals['start'] += totals['end']
Expand Down Expand Up @@ -1837,7 +1837,7 @@ def _perform(_playlist_idx, _page_token, _offset, _result):
if not _result:
_result = {'items': []}

_new_offset = self._max_results - len(_result['items']) + _offset
_new_offset = self.max_results() - len(_result['items']) + _offset
if _offset > 0:
_items = _items[_offset:]
_result['offset'] = _new_offset
Expand Down Expand Up @@ -1867,23 +1867,23 @@ def _perform(_playlist_idx, _page_token, _offset, _result):
_continuations = (_data.get('continuations', [{}])[0]
.get('nextContinuationData', {})
.get('continuation', ''))
if _continuations and len(_result['items']) <= self._max_results:
if _continuations and len(_result['items']) <= self.max_results():
_result['next_page_token'] = _continuations

if len(_result['items']) < self._max_results:
if len(_result['items']) < self.max_results():
_result = _perform(_playlist_idx=playlist_index,
_page_token=_continuations,
_offset=0,
_result=_result)

# trim result
if len(_result['items']) > self._max_results:
if len(_result['items']) > self.max_results():
_items = _result['items']
_items = _items[:self._max_results]
_items = _items[:self.max_results()]
_result['items'] = _items
_result['continue'] = True

if len(_result['items']) < self._max_results:
if len(_result['items']) < self.max_results():
if 'continue' in _result:
del _result['continue']

Expand Down

0 comments on commit ffc0bf0

Please sign in to comment.