From ec1d3c97d0d301c37fcfaa7267ee34e75c2d7cbf Mon Sep 17 00:00:00 2001 From: kclauhk <78251477+kclauhk@users.noreply.github.com> Date: Thu, 27 Jun 2024 19:38:26 +0800 Subject: [PATCH] Separate playlist-end and 5000 limit checking for accuracy and readability --- yt_dlp/extractor/giphy.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/yt_dlp/extractor/giphy.py b/yt_dlp/extractor/giphy.py index 1f2a82428..63bfad3e8 100644 --- a/yt_dlp/extractor/giphy.py +++ b/yt_dlp/extractor/giphy.py @@ -97,8 +97,8 @@ def _extract_info(self, gif_data, video_id): 'subtitles': subtitles, } - def _api_channel_feed(self, channel_id, max_offset=_GIPHY_MAX): - offset = 0 + def _api_channel_feed(self, channel_id): + count, offset = 0, 0 query_url = f'https://giphy.com/api/v4/channels/{channel_id}/feed/?offset={offset}' for _ in itertools.count(1): search_results = self._download_json(query_url, channel_id, fatal=False, @@ -110,10 +110,13 @@ def _api_channel_feed(self, channel_id, max_offset=_GIPHY_MAX): **self._extract_info(video, video['id']), 'webpage_url': video['url'], } + count += len(search_results.get('results')) + if count >= (int_or_none(self.get_param('playlistend')) or (self._GIPHY_MAX + 1)): + return query_url = url_or_none(search_results.get('next')) or '' offset = int(self._search_regex(r'offset=(\d+)', query_url, 'offset', default=0)) # offset cannot exceed 5000 - if not query_url or offset > min((max_offset or self._GIPHY_MAX) + 1, self._GIPHY_MAX): + if not query_url or offset > self._GIPHY_MAX: return @@ -242,7 +245,7 @@ def _real_extract(self, url): if channel_id := self._html_search_regex(r'\{"channelId":\s*([^\}]+)\}', webpage, 'channel_id', default=None): uploader_id = self._html_search_meta('twitter:creator', webpage).replace('@', '').lower() entries = [] - for i in self._api_channel_feed(channel_id, int_or_none(self.get_param('playlistend'))): + for i in self._api_channel_feed(channel_id): entries.append(i) return {