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

Code before:
            '_type': 'url_transparent',
            'url': smuggle_url(episode['webplay_url'], {'no_bangumi_tip': 1}),
            'ie_key': BiliBiliIE.ie_key(),
            'timestamp': parse_iso8601(episode.get('update_time'), delimiter=' '),
            'episode': episode.get('index_title'),
            'episode_number': int_or_none(episode.get('index')),
        } for episode in season_info['episodes']]

        entries = sorted(entries, key=lambda entry: entry.get('episode_number'))

        return self.playlist_result(
            entries, bangumi_id,
            season_info.get('bangumi_title'), season_info.get('evaluate'))


class BilibiliSpaceBaseIE(InfoExtractor):
    def __init__(self, downloader=None):
        InfoExtractor.__init__(self, downloader)
        self.page_idx_start = 1

    def _real_extract(self, url):
        playlist_id = self._match_id(url)

        metadata, pagedlist = self._extract_internal(playlist_id)

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

    def _extract_internal(self, playlist_id, **kwargs):
        first_page = self._fetch_page(playlist_id, self.page_idx_start, **kwargs)
        metadata = self._get_metadata(first_page, **kwargs)

        pagedlist = InAdvancePagedList(
            lambda idx: self._get_entries(self._fetch_page(playlist_id, idx, **kwargs) if idx else first_page, **kwargs),
            metadata['page_count'], metadata['page_size'])
        return metadata, pagedlist

    def _fetch_page(self, playlist_id, page_idx, **kwargs):
        raise NotImplementedError('This method must be implemented by subclasses')

    def _get_metadata(self, page_data, **kwargs):
        raise NotImplementedError('This method must be implemented by subclasses')

    def _get_entries(self, page_data, **kwargs):
        raise NotImplementedError('This method must be implemented by subclasses')


class BilibiliSpaceVideoIE(BilibiliSpaceBaseIE):
    _VALID_URL = r'https?://space\.bilibili\.com/(?P<id>\d+)(?P<video>/video)?$'
    _TESTS = [{
        'url': 'https://space.bilibili.com/3985676/video',
        'info_dict': {

Review comment (refactor):
If it's a Base extractor you shouldn't have _real_extract, you can rename it to  _extract_playlist and later call it

Also, usually you pre-declare the functions that you use, so that _real_extract is at the bottom

Code after:
            '_type': 'url_transparent',
            'url': smuggle_url(episode['webplay_url'], {'no_bangumi_tip': 1}),
            'ie_key': BiliBiliIE.ie_key(),
            'timestamp': parse_iso8601(episode.get('update_time'), delimiter=' '),
            'episode': episode.get('index_title'),
            'episode_number': int_or_none(episode.get('index')),
        } for episode in season_info['episodes']]

        entries = sorted(entries, key=lambda entry: entry.get('episode_number'))

        return self.playlist_result(
            entries, bangumi_id,
            season_info.get('bangumi_title'), season_info.get('evaluate'))


class BilibiliSpaceBaseIE(InfoExtractor):
    def _extract_playlist(self, fetch_page, get_metadata, get_entries):
        first_page = fetch_page(1)
        metadata = get_metadata(first_page)

        paged_list = InAdvancePagedList(
            lambda idx: get_entries(fetch_page(idx) if idx > 1 else first_page),
            metadata['page_count'], metadata['page_size'])

        return metadata, paged_list


class BilibiliSpaceVideoIE(BilibiliSpaceBaseIE):
    _VALID_URL = r'https?://space\.bilibili\.com/(?P<id>\d+)(?P<video>/video)?/?(?:[?#]|$)'
    _TESTS = [{
        'url': 'https://space.bilibili.com/3985676/video',
        'info_dict': {
            'id': '3985676',
        },
        'playlist_mincount': 178,
    }]

    def _real_extract(self, url):
        playlist_id, is_video_url = self._match_valid_url(url).group('id', 'video')
        if not is_video_url:
            self.to_screen('A channel URL was given. Only the channel\'s videos will be downloaded. '
                           'To download audios, add a "/audio" to the URL')

        def fetch_page(page_idx):
            return self._download_json(
                'https://api.bilibili.com/x/space/arc/search', playlist_id,
                note=f'Downloading page {page_idx}',
                query={'mid': playlist_id, 'pn': page_idx, 'jsonp': 'jsonp'})['data']

        def get_metadata(page_data):
            page_size = page_data['page']['ps']