Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/rokfin.py (Python)

Code before:
                yield self.url_result(url=f'{self._BASE_URL}/{video_type}/{video_id}')
        if not query:
            return
        if not self._service_url or not self._service_access_key:
            self._service_url, self._service_access_key = self._get_access_credentials()
        QUERY_DATA = {'query': query, 'page': {'size': self._ENTRIES_PER_PAGE, 'current': 1}}
        total_pages = None
        while True:
            search_results = self._download_json(
                self._service_url, self._SEARCH_KEY, headers={'authorization': self._service_access_key},
                data=json.dumps(QUERY_DATA).encode('utf-8'), encoding='utf-8',
                note='Downloading search results page %d%s' % (QUERY_DATA['page']['current'], (' of ~%d') % (total_pages,) if total_pages and total_pages >= QUERY_DATA['page']['current'] else ''),
                fatal=False) or {}
            QUERY_DATA['page']['current'] += 1
            total_pages = traverse_obj(search_results, ('meta', 'page', 'total_pages'), expected_type=int_or_none)
            yield from get_video_data(search_results)
            if not search_results.get('results'):
                return

    def _get_access_credentials(self):
        notfound_err_page = self._download_webpage('https://rokfin.com/discover', self._SEARCH_KEY, expected_status=404, fatal=False)
        js = functools.reduce(
            lambda result, m: result + self._download_webpage(
                self._BASE_URL + m.group('path'), self._SEARCH_KEY,
                note='Downloading JavaScript file', fatal=False) or '',
            re.finditer(r'<script\s+[^>]*?src\s*=\s*"(?P<path>/static/js/[^">]*)"[^>]*>', notfound_err_page), '')
        service_url = None
        services_access_key = None
        for m in re.finditer(r'(REACT_APP_SEARCH_KEY\s*:\s*"(?P<key>[^"]*)")|(REACT_APP_ENDPOINT_BASE\s*:\s*"(?P<url>[^"]*)")', js):
            if m.group('url'):
                service_url = m.group('url') + '/api/as/v1/engines/rokfin-search/search.json'
            elif m.group('key'):
                services_access_key = 'Bearer ' + m.group('key')
            if service_url and services_access_key:
                return (service_url, services_access_key)


Review comment (suggestion):
```suggestion
        js = '\n'.join(
            self._download_webpage(
                f'https://rokfin.com/{path}', self._SEARCH_KEY,
                note='Downloading JavaScript file', fatal=False) or '',
            for path in re.findall(r'<script\s[^>]*src\s*=\s*"(/static/js/[^">]+)', notfound_err_page))
```

Code after:
        'url': 'https://rokfin.com/TheConvoCouch',
        'playlist_mincount': 100,
        'info_dict': {
            'id': '12071-new',
            'title': 'TheConvoCouch - New',
            'description': 'md5:bb622b1bca100209b91cd685f7847f06',
        },
    }]

    _TABS = {
        'new': 'posts',
        'top': 'top',
        'videos': 'video',
        'podcasts': 'audio',
        'streams': 'stream',
        'stacks': 'stack',
    }

    def _real_initialize(self):
        self._validate_extractor_args()

    def _validate_extractor_args(self):
        requested_tabs = self._configuration_arg('tab', None)
        if requested_tabs is not None and (len(requested_tabs) > 1 or requested_tabs[0] not in self._TABS):
            raise ExtractorError(f'Invalid extractor-arg "tab". Must be one of {", ".join(self._TABS)}', expected=True)

    def _entries(self, channel_id, channel_name, tab):
        pages_total = None
        for page_n in itertools.count(0):
            if tab in ('posts', 'top'):
                data_url = f'{_API_BASE_URL}user/{channel_name}/{tab}?page={page_n}&size=50'
            else:
                data_url = f'{_API_BASE_URL}post/search/{tab}?page={page_n}&size=50&creator={channel_id}'
            metadata = self._download_json(
                data_url, channel_name,
                note=f'Downloading video metadata page {page_n + 1}{format_field(pages_total, template=" of %s")}')

            yield from self._get_video_data(metadata)
            pages_total = int_or_none(metadata.get('totalPages')) or None
            is_last = metadata.get('last')
            if is_last or (page_n > pages_total if pages_total else is_last is not False):
                return

    def _real_extract(self, url):
        channel_name = self._match_id(url)
        channel_info = self._download_json(f'{_API_BASE_URL}user/{channel_name}', channel_name)
        channel_id = channel_info['id']
        tab = self._configuration_arg('tab', default=['new'])[0]

        return self.playlist_result(
            self._entries(channel_id, channel_name, self._TABS[tab]),