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

Code before:
            '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'))


def bilibili_space_extract_playlist(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 else first_page),
        metadata['page_count'], metadata['page_size'])

    return metadata, paged_list


class BilibiliSpaceVideoIE(InfoExtractor):
    _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.')

        playlist_id = self._match_id(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']

Review comment (refactor):
This is a Space Video extractor, right?

We could add another extractor specifically for the main page of a Space, e.g. https://space.bilibili.com/3985676 , extracting the links of the webpage

```suggestion
    _VALID_URL = r'https?://space\.bilibili\.com/(?P<id>\d+)/video'
```

Code after:
            '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']
            entry_count = page_data['page']['count']
            return {