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

Code before:
            'episode_id': '267851',
            'episode_number': 1,
            'title': '1 残酷',
            'duration': 1425.256,
            'timestamp': 1554566400,
            'upload_date': '20190406',
            'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$'
        },
        'skip': 'According to the copyright owner\'s request, you may only watch the video after you are premium member.'
    }]

    def _real_extract(self, url):
        video_id = self._match_id(url)
        webpage = self._download_webpage(url, video_id)

        if '您所在的地区无法观看本片' in webpage:
            raise GeoRestrictedError('This video is restricted')
        elif ('成为大会员免费看' in webpage
              or '正在观看预览，大会员免费看全片' in webpage
              or '开通大会员观看' in webpage):
            self.raise_login_required('This video is for premium members only')

        play_info = self._download_json('https://api.bilibili.com/pgc/player/web/v2/playurl',
                                        video_id, 'Extracting episode',
                                        query={'fnval': '4048', 'ep_id': video_id},
                                        headers={'Referer': url, **self.geo_verification_headers()})['result']['video_info']

        formats = self.extract_formats(play_info)

        bangumi_info = self._download_json('https://api.bilibili.com/pgc/view/web/season',
                                           video_id, 'Get episode details',
                                           query={'ep_id': video_id},
                                           headers={'Referer': url, **self.geo_verification_headers()})['result']

        episodes = bangumi_info.get('episodes')
        episode_number = next((
            idx + 1 for idx, e in enumerate(episodes or [])
            if str(e.get('id')) == video_id
        ), 1)

        season_id = bangumi_info.get('season_id') or []
        season_number = season_id and next((
            idx + 1 for idx, e in enumerate(
                bangumi_info.get('seasons') or [])
            if e.get('season_id') == season_id
        ), None)

        aid = traverse_obj(bangumi_info, ('episodes', episode_number, 'aid'))
        cid = traverse_obj(bangumi_info, ('episodes', episode_number, 'cid'))

        return {

Review comment (refactor):
 - add a `headers` var since we're using the same headers multiple times
 - use `episode_id` in the query
```suggestion
        headers = {'Referer': url, **self.geo_verification_headers()}
        play_info = self._download_json(
            'https://api.bilibili.com/pgc/player/web/v2/playurl', video_id,
            'Extracting episode', query={'fnval': '4048', 'ep_id': episode_id},
            headers=headers)['result']['video_info']
```

Code after:

    _TESTS = [{
        'url': 'https://www.bilibili.com/bangumi/play/ep267851',
        'info_dict': {
            'id': '267851',
            'ext': 'mp4',
            'series': '鬼灭之刃',
            'series_id': '4358',
            'season': '鬼灭之刃',
            'season_id': '26801',
            'season_number': 1,
            'episode': '残酷',
            'episode_id': '267851',
            'episode_number': 1,
            'title': '1 残酷',
            'duration': 1425.256,
            'timestamp': 1554566400,
            'upload_date': '20190406',
            'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$'
        },
        'skip': 'According to the copyright owner\'s request, you may only watch the video after you are premium member.'
    }]

    def _real_extract(self, url):
        video_id = self._match_id(url)
        episode_id = video_id[2:]
        webpage = self._download_webpage(url, video_id)

        if '您所在的地区无法观看本片' in webpage:
            raise GeoRestrictedError('This video is restricted')
        elif '正在观看预览，大会员免费看全片' in webpage:
            self.raise_login_required('This video is for premium members only')

        headers = {'Referer': url, **self.geo_verification_headers()}
        play_info = self._download_json(
            'https://api.bilibili.com/pgc/player/web/v2/playurl', video_id,
            'Extracting episode', query={'fnval': '4048', 'ep_id': episode_id},
            headers=headers)
        premium_only = play_info.get('code') == -10403
        play_info = traverse_obj(play_info, ('result', 'video_info', {dict})) or {}

        formats = self.extract_formats(play_info)
        if not formats and (premium_only or '成为大会员抢先看' in webpage or '开通大会员观看' in webpage):
            self.raise_login_required('This video is for premium members only')

        bangumi_info = self._download_json(
            'https://api.bilibili.com/pgc/view/web/season', video_id, 'Get episode details',
            query={'ep_id': episode_id}, headers=headers)['result']

        episode_number, episode_info = next((
            (idx, ep) for idx, ep in enumerate(traverse_obj(