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

Code before:
        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': {
            'id': '3985676',
        },
        'playlist_mincount': 178,
    }]

    def _real_extract(self, url):
        mobj = self._match_valid_url(url)
        if not mobj.group('video'):
            self.to_screen(f'{url} may have multiple media types. This run will only download videos.\n'
                           + f'For example, use {url}/audio for audios.')

        return BilibiliSpaceBaseIE._real_extract(self, url)

    def _fetch_page(self, playlist_id, page_idx, **kwargs):
        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']


Review comment (refactor):
I would merge _fetch_page and _get_metadata into one, but this is about the order i was talking about

```python
def _fetch_page():
   ....

def _get_metadata():
    ....
    
def _get_entries():
   ....
   
def _extract_internal():
   ....
```

Code after:
        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']
            entry_count = page_data['page']['count']
            return {
                'page_count': math.ceil(entry_count / page_size),
                'page_size': page_size,
            }

        def get_entries(page_data):
            for entry in traverse_obj(page_data, ('list', 'vlist')) or []:
                yield self.url_result(f'https://www.bilibili.com/video/{entry["bvid"]}', BiliBiliIE, entry['bvid'])

        metadata, paged_list = self._extract_playlist(fetch_page, get_metadata, get_entries)
        return self.playlist_result(paged_list, playlist_id)


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