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

Code before:
    }, {
        'url': 'https://www.bilibili.com/video/BV1Ks411f7aQ/',
        'info_dict': {
            'id': 'BV1Ks411f7aQ',
            'title': '【BD1080P】狼与香辛料I【华盟】',
            'ext': 'mp4',
        },
        'skip': 'login required',
    }, {
        'url': 'https://www.bilibili.com/video/BV1GJ411x7h7/',
        'info_dict': {
            'id': 'BV1GJ411x7h7',
            'title': '【官方 MV】Never Gonna Give You Up - Rick Astley',
            'ext': 'mp4',
        },
        'skip': 'geo-restricted',
    }]

    def _real_extract(self, url):
        video_id = self._match_id(url)
        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 = {**headers, 'Referer': url}

        initial_state = self._search_json(r'window\.__INITIAL_STATE__\s*=', webpage, 'initial state', video_id)
        is_festival = 'videoData' not in initial_state
        if is_festival:
            video_data = initial_state['videoInfo']
        else:
            play_info_obj = self._search_json(
                r'window\.__playinfo__\s*=', webpage, 'play info', video_id, fatal=False)
            if not play_info_obj:
                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)
            play_info = traverse_obj(play_info_obj, ('data', {dict}))
            if not play_info:
                if traverse_obj(play_info_obj, 'code') == 87007:
                    toast = get_element_by_class('tips-toast', webpage) or ''
                    msg = clean_html(
                        f'{get_element_by_class("belongs-to", toast) or ""}，'
                        + (get_element_by_class('level', toast) or ''))
                    raise ExtractorError(
                        f'This is a supporter-only video: {msg}. {self._login_hint()}', expected=True)
                raise ExtractorError('Failed to extract play info')

Review comment (suggestion):
```suggestion
        headers['Referer'] = url
```

Code after:
        },
        'skip': 'supporter-only video',
    }, {
        'url': 'https://www.bilibili.com/video/BV1Ks411f7aQ/',
        'info_dict': {
            'id': 'BV1Ks411f7aQ',
            'title': '【BD1080P】狼与香辛料I【华盟】',
            'ext': 'mp4',
        },
        'skip': 'login required',
    }, {
        'url': 'https://www.bilibili.com/video/BV1GJ411x7h7/',
        'info_dict': {
            'id': 'BV1GJ411x7h7',
            'title': '【官方 MV】Never Gonna Give You Up - Rick Astley',
            'ext': 'mp4',
        },
        'skip': 'geo-restricted',
    }]

    def _real_extract(self, url):
        video_id = self._match_id(url)
        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)
        is_festival = 'videoData' not in initial_state
        if is_festival:
            video_data = initial_state['videoInfo']
        else:
            play_info_obj = self._search_json(
                r'window\.__playinfo__\s*=', webpage, 'play info', video_id, fatal=False)
            if not play_info_obj:
                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)
            play_info = traverse_obj(play_info_obj, ('data', {dict}))
            if not play_info:
                if traverse_obj(play_info_obj, 'code') == 87007:
                    toast = get_element_by_class('tips-toast', webpage) or ''
                    msg = clean_html(
                        f'{get_element_by_class("belongs-to", toast) or ""}，'
                        + (get_element_by_class('level', toast) or ''))
                    raise ExtractorError(