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

Code before:
            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 {
            'id': video_id,
            'formats': formats,
            'title': f"{traverse_obj(bangumi_info, ('episodes', episode_number - 1, 'title'), expected_type=str)} {traverse_obj(bangumi_info, ('episodes', episode_number - 1, 'long_title'), expected_type=str)}",
            'episode': traverse_obj(bangumi_info, ('episodes', episode_number - 1, 'long_title'), expected_type=str),
            'episode_id': video_id,
            'episode_number': int_or_none(episode_number),
            'series': traverse_obj(bangumi_info, ('series', 'series_title')),
            'series_id': str_or_none(traverse_obj(bangumi_info, ('series', 'series_id'))),
            'season': bangumi_info.get('season_title') or bangumi_info.get('title'),
            'season_id': str_or_none(season_id),
            'season_number': int_or_none(season_number),
            'thumbnail': bangumi_info.get('square_cover'),
            'timestamp': traverse_obj(bangumi_info, ('episodes', episode_number - 1, 'pub_time')),
            'duration': float_or_none(play_info.get('timelength'), scale=1000),
            'subtitles': self.extract_subtitles(video_id, aid, cid),
            '__post_extractor': self.extract_comments(aid),
            'http_headers': {'Referer': url, **self.geo_verification_headers()},
        }


class BiliBiliBangumiMediaIE(InfoExtractor):
    _VALID_URL = r'https?://www\.bilibili\.com/bangumi/media/md(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://www.bilibili.com/bangumi/media/md24097891',
        'info_dict': {
            'id': '24097891',
        },
        'playlist_mincount': 25,
    }]

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

        initial_state = self._search_json(r'window\.__INITIAL_STATE__\s*=', webpage, 'initial_state', media_id)
        episode_list = traverse_obj(
            self._download_json('https://api.bilibili.com/pgc/web/season/section', media_id,
                                query={'season_id': traverse_obj(initial_state, ('mediaInfo', 'season_id'))},
                                headers={'Referer': url, **self.geo_verification_headers()},
                                note='Downloading season info'),
            ('result', 'main_section', 'episodes'))


Review comment (refactor):
```suggestion
            'subtitles': self.extract_subtitles(video_id, aid, episode_info.get('cid')),
            '__post_extractor': self.extract_comments(aid),
            'http_headers': headers,
```

Code after:
        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(
                bangumi_info, ('episodes', ..., {dict})), 1)
            if str_or_none(ep.get('id')) == episode_id), (1, {}))

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

        aid = episode_info.get('aid')

        return {
            'id': video_id,
            'formats': formats,
            **traverse_obj(bangumi_info, {
                'series': ('series', 'series_title', {str}),
                'series_id': ('series', 'series_id', {str_or_none}),
                'thumbnail': ('square_cover', {url_or_none}),
            }),
            'title': join_nonempty('title', 'long_title', delim=' ', from_dict=episode_info),
            'episode': episode_info.get('long_title'),
            'episode_id': episode_id,
            'episode_number': int_or_none(episode_info.get('title')) or episode_number,
            'season_id': str_or_none(season_id),
            'season_number': season_number,
            'timestamp': int_or_none(episode_info.get('pub_time')),
            'duration': float_or_none(play_info.get('timelength'), scale=1000),
            'subtitles': self.extract_subtitles(video_id, aid, episode_info.get('cid')),
            '__post_extractor': self.extract_comments(aid),
            'http_headers': headers,
        }


class BiliBiliBangumiMediaIE(BilibiliBaseIE):
    _VALID_URL = r'https?://www\.bilibili\.com/bangumi/media/md(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://www.bilibili.com/bangumi/media/md24097891',
        'info_dict': {
            'id': '24097891',
        },
        'playlist_mincount': 25,