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

Code before:
            'episode': '炭治郎篇',
            'episode_id': '345120',
            'episode_number': 27,
            'title': '#1 炭治郎篇',
            'duration': 1922.129,
            'timestamp': 1602853860,
            'upload_date': '20201016',
            'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
        },
    }]

    def _real_extract(self, url):
        episode_id = self._match_id(url)
        headers = self.geo_verification_headers()
        webpage = self._download_webpage(url, episode_id, headers=headers)

        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
        play_info = self._download_json(
            'https://api.bilibili.com/pgc/player/web/v2/playurl', episode_id,
            'Extracting episode', query={'fnval': 12240, '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', episode_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', ('section', ..., 'episodes')), ..., {dict})), 1)
            if str_or_none(ep.get('id')) == episode_id), (1, {}))

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

        aid = episode_info.get('aid')


Review comment (suggestion):
For bangumi like `https://www.bilibili.com/bangumi/play/ep829434`, all logged-in users can access premium quality, and  all qualities are directly embedded in `__playinfo__` of page HTML like non-festival `BiliBiliIE` videos. (The trial counter can be simply ignored). May be worth taking a look at that or see if we can tweak API request query to directly achieve the same.

Code after:
            'season_id': '26801',
            'season_number': 1,
            'episode': '炭治郎篇',
            'episode_id': '345120',
            'episode_number': 27,
            'title': '#1 炭治郎篇',
            'duration': 1922.129,
            'timestamp': 1602853860,
            'upload_date': '20201016',
            'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
        },
    }]

    def _real_extract(self, url):
        episode_id = self._match_id(url)
        headers = self.geo_verification_headers()
        webpage = self._download_webpage(url, episode_id, headers=headers)

        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

        play_info = (
            self._search_json(
                r'playurlSSRData\s*=', webpage, 'embedded page info', episode_id,
                end_pattern='\n', default=None)
            or self._download_json(
                'https://api.bilibili.com/pgc/player/web/v2/playurl', episode_id,
                'Extracting episode', query={'fnval': 12240, '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', episode_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', ('section', ..., 'episodes')), ..., {dict})), 1)
            if str_or_none(ep.get('id')) == episode_id), (1, {}))

        season_id = bangumi_info.get('season_id')