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

Code before:
                                   playlist_id, note=f'Downloading page {page_idx}',
                                   query={'mid': mid, 'season_id': sid, 'page_num': page_idx, 'page_size': 30}
                                   )['data']

    def _get_metadata(self, page_data, **kwargs):
        page_size = page_data['page']['page_size']
        entry_count = page_data['page']['total']
        return {
            'page_count': math.ceil(entry_count / page_size),
            'page_size': page_size,
            'entry_count': entry_count,
            'title': traverse_obj(page_data, ('meta', 'name'))
        }

    def _get_entries(self, page_data, **kwargs):
        for entry in page_data.get('archives', []):
            yield self.url_result(f'https://www.bilibili.com/video/{entry["bvid"]}',
                                  BiliBiliIE.ie_key(), entry['bvid'])

    def _real_extract(self, url):
        mid, sid = self._match_valid_url(url).group('mid', 'sid')
        video_id = f'{mid}_{sid}'

        metadata, pagedlist = self._extract_internal(video_id, mid=mid, sid=sid)

        return self.playlist_result(pagedlist, video_id, metadata['title'])


class BilibiliCategoryIE(InfoExtractor):
    IE_NAME = 'Bilibili category extractor'
    _MAX_RESULTS = 1000000
    _VALID_URL = r'https?://www\.bilibili\.com/v/[a-zA-Z]+\/[a-zA-Z]+'
    _TESTS = [{
        'url': 'https://www.bilibili.com/v/kichiku/mad',
        'info_dict': {
            'id': 'kichiku: mad',
            'title': 'kichiku: mad'
        },
        'playlist_mincount': 45,
        'params': {
            'playlistend': 45
        }
    }]

    def _fetch_page(self, api_url, num_pages, query, page_num):
        parsed_json = self._download_json(
            api_url, query, query={'Search_key': query, 'pn': page_num},
            note='Extracting results from page %s of %s' % (page_num, num_pages))

        video_list = traverse_obj(parsed_json, ('data', 'archives'), expected_type=list)
        if not video_list:

Review comment (refactor):
```suggestion
        mid, sid = self._match_valid_url(url).group('mid', 'sid')
        video_id = f'{mid}_{sid}'
        metadata, paged_list = self._extract_internal(video_id, mid=mid, sid=sid)

        return self.playlist_result(paged_list, video_id, metadata['title'])
```

Code after:
                                      BiliBiliIE, entry['bvid'])

        metadata, paged_list = self._extract_playlist(fetch_page, get_metadata, get_entries)
        return self.playlist_result(paged_list, playlist_id, metadata['title'])


class BilibiliCategoryIE(InfoExtractor):
    IE_NAME = 'Bilibili category extractor'
    _MAX_RESULTS = 1000000
    _VALID_URL = r'https?://www\.bilibili\.com/v/[a-zA-Z]+\/[a-zA-Z]+'
    _TESTS = [{
        'url': 'https://www.bilibili.com/v/kichiku/mad',
        'info_dict': {
            'id': 'kichiku: mad',
            'title': 'kichiku: mad'
        },
        'playlist_mincount': 45,
        'params': {
            'playlistend': 45
        }
    }]

    def _fetch_page(self, api_url, num_pages, query, page_num):
        parsed_json = self._download_json(
            api_url, query, query={'Search_key': query, 'pn': page_num},
            note='Extracting results from page %s of %s' % (page_num, num_pages))

        video_list = traverse_obj(parsed_json, ('data', 'archives'), expected_type=list)
        if not video_list:
            raise ExtractorError('Failed to retrieve video list for page %d' % page_num)

        for video in video_list:
            yield self.url_result(
                'https://www.bilibili.com/video/%s' % video['bvid'], 'BiliBili', video['bvid'])

    def _entries(self, category, subcategory, query):
        # map of categories : subcategories : RIDs
        rid_map = {
            'kichiku': {
                'mad': 26,
                'manual_vocaloid': 126,
                'guide': 22,
                'theatre': 216,
                'course': 127
            },
        }

        if category not in rid_map:
            raise ExtractorError(
                f'The category {category} isn\'t supported. Supported categories: {list(rid_map.keys())}')
        if subcategory not in rid_map[category]: