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

Code before:
        'info_dict': {
            'id': 'BV1GJ411x7h7',
            'title': '【官方 MV】Never Gonna Give You Up - Rick Astley',
            'ext': 'mp4',
        },
        'skip': 'geo-restricted',
    }, {
        'note': 'has - in the last path segment of the url',
        'url': 'https://www.bilibili.com/festival/bh3-7th?bvid=BV1tr4y1f7p2&',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        video_id, prefix = self._match_valid_url(url).group('id', 'prefix')
        headers = self.geo_verification_headers()
        webpage, urlh = self._download_webpage_handle(url, video_id, headers=headers)
        if not self._match_valid_url(urlh.url):
            return self.url_result(urlh.url)

        headers['Referer'] = url

        initial_state = self._search_json(r'window\.__INITIAL_STATE__\s*=', webpage, 'initial state', video_id, default=None)
        if not initial_state:
            if traverse_obj(self._search_json(
                    r'<script\s*>\s*window\._riskdata_\s*=', webpage, 'risk data', video_id, default=None), 'v_voucher'):
                raise ExtractorError('You are downloading too frequently, please wait and try later', expected=True)
            query = {'platform': 'web'}
            prefix = prefix.upper()
            if prefix == 'BV':
                query['bvid'] = prefix + video_id
            elif prefix == 'AV':
                query['aid'] = video_id
            detail = self._download_json(
                'https://api.bilibili.com/x/web-interface/wbi/view/detail', video_id,
                note='Downloading redirection URL', errnote='Failed to download redirection URL',
                query=self._sign_wbi(query, video_id), headers=headers)
            new_url = traverse_obj(detail, ('data', 'View', 'redirect_url', {url_or_none}))
            if new_url and BiliBiliBangumiIE.suitable(new_url):
                return self.url_result(new_url, BiliBiliBangumiIE)
            raise ExtractorError('Unable to extract initial state')

        if traverse_obj(initial_state, ('error', 'trueCode')) == -403:
            self.raise_login_required()
        if traverse_obj(initial_state, ('error', 'trueCode')) == -404:
            raise ExtractorError(
                'This video may be deleted or geo-restricted. '
                'You might want to try a VPN or a proxy server (with --proxy)', expected=True)

        is_festival = 'videoData' not in initial_state
        if is_festival:
            video_data = initial_state['videoInfo']

Review comment (refactor):
```suggestion
            if self._search_json(r'\bwindow\._riskdata_\s*=', webpage, 'risk', video_id, default={}).get('v_voucher'):
                raise ExtractorError('You have exceeded the rate limit. Try again later', expected=True)
```

Code after:
        'info_dict': {
            'id': 'BV1GJ411x7h7',
            'title': '【官方 MV】Never Gonna Give You Up - Rick Astley',
            'ext': 'mp4',
        },
        'skip': 'geo-restricted',
    }, {
        'note': 'has - in the last path segment of the url',
        'url': 'https://www.bilibili.com/festival/bh3-7th?bvid=BV1tr4y1f7p2&',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        video_id, prefix = self._match_valid_url(url).group('id', 'prefix')
        headers = self.geo_verification_headers()
        webpage, urlh = self._download_webpage_handle(url, video_id, headers=headers)
        if not self._match_valid_url(urlh.url):
            return self.url_result(urlh.url)

        headers['Referer'] = url

        initial_state = self._search_json(r'window\.__INITIAL_STATE__\s*=', webpage, 'initial state', video_id, default=None)
        if not initial_state:
            if self._search_json(r'\bwindow\._riskdata_\s*=', webpage, 'risk', video_id, default={}).get('v_voucher'):
                raise ExtractorError('You have exceeded the rate limit. Try again later', expected=True)
            query = {'platform': 'web'}
            prefix = prefix.upper()
            if prefix == 'BV':
                query['bvid'] = prefix + video_id
            elif prefix == 'AV':
                query['aid'] = video_id
            detail = self._download_json(
                'https://api.bilibili.com/x/web-interface/wbi/view/detail', video_id,
                note='Downloading redirection URL', errnote='Failed to download redirection URL',
                query=self._sign_wbi(query, video_id), headers=headers)
            new_url = traverse_obj(detail, ('data', 'View', 'redirect_url', {url_or_none}))
            if new_url and BiliBiliBangumiIE.suitable(new_url):
                return self.url_result(new_url, BiliBiliBangumiIE)
            raise ExtractorError('Unable to extract initial state')

        if traverse_obj(initial_state, ('error', 'trueCode')) == -403:
            self.raise_login_required()
        if traverse_obj(initial_state, ('error', 'trueCode')) == -404:
            raise ExtractorError(
                'This video may be deleted or geo-restricted. '
                'You might want to try a VPN or a proxy server (with --proxy)', expected=True)

        is_festival = 'videoData' not in initial_state
        if is_festival:
            video_data = initial_state['videoInfo']
        else: