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

Code before:
            'upload_date': '20241110',
            'uploader': '何同学工作室',
            'like_count': int,
            'view_count': int,
            'title': '美国小朋友就玩这个？！何同学工作室11月开箱',
            'description': '本期产品信息：\n机器狗\n气味模拟器\nCloudboom Strike LS\n无弦吉他\n蓝牙磁带音箱\n神奇画板',
            'timestamp': 1731232800,
            'tags': list,
            'chapters': list,
        },
    }]

    def _real_extract(self, url):
        post_id = self._match_id(url)
        # Without the newer chrome UA, the API will return a error (-352)
        post_data = self._download_json(
            'https://api.bilibili.com/x/polymer/web-dynamic/v1/detail', post_id,
            query={'id': post_id}, headers={
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
            })
        video_url = traverse_obj(post_data, (
            'data', 'item', (None, 'orig'), 'modules', 'module_dynamic',
            (('major', ('archive', 'pgc')), ('additional', ('reserve', 'common'))),
            'jump_url', {url_or_none}, any, {sanitize_url}))
        if not video_url:
            self.raise_no_formats('No video found!', expected=True, video_id=post_id)
        return self.url_result(video_url)


class BiliIntlBaseIE(InfoExtractor):
    _API_URL = 'https://api.bilibili.tv/intl/gateway'
    _NETRC_MACHINE = 'biliintl'
    _HEADERS = {'Referer': 'https://www.bilibili.tv/'}

    def _call_api(self, endpoint, *args, **kwargs):
        json = self._download_json(self._API_URL + endpoint, *args, **kwargs)
        if json.get('code'):
            if json['code'] in (10004004, 10004005, 10023006):
                self.raise_login_required()
            elif json['code'] == 10004001:
                self.raise_geo_restricted()
            else:
                if json.get('message') and str(json['code']) != json['message']:
                    errmsg = f'{kwargs.get("errnote", "Unable to download JSON metadata")}: {self.IE_NAME} said: {json["message"]}'
                else:
                    errmsg = kwargs.get('errnote', 'Unable to download JSON metadata')
                if kwargs.get('fatal'):
                    raise ExtractorError(errmsg)
                else:
                    self.report_warning(errmsg)
        return json.get('data')

Review comment (refactor):
why is the error expected?
```suggestion
        if not video_url or video_url == url:
            raise ExtractorError('No valid URL found')
```

Code after:
            'upload_date': '20241110',
            'uploader': '何同学工作室',
            'like_count': int,
            'view_count': int,
            'title': '美国小朋友就玩这个？！何同学工作室11月开箱',
            'description': '本期产品信息：\n机器狗\n气味模拟器\nCloudboom Strike LS\n无弦吉他\n蓝牙磁带音箱\n神奇画板',
            'timestamp': 1731232800,
            'tags': list,
            'chapters': list,
        },
    }]

    def _real_extract(self, url):
        post_id = self._match_id(url)
        # Without the newer chrome UA, the API will return an error (-352)
        post_data = self._download_json(
            'https://api.bilibili.com/x/polymer/web-dynamic/v1/detail', post_id,
            query={'id': post_id}, headers={
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
            })
        video_url = traverse_obj(post_data, (
            'data', 'item', (None, 'orig'), 'modules', 'module_dynamic',
            (('major', ('archive', 'pgc')), ('additional', ('reserve', 'common'))),
            'jump_url', {url_or_none}, any, {sanitize_url}))
        if not video_url or (self.suitable(video_url) and post_id == self._match_id(video_url)):
            raise ExtractorError('No valid video URL found', expected=True)
        return self.url_result(video_url)


class BiliIntlBaseIE(InfoExtractor):
    _API_URL = 'https://api.bilibili.tv/intl/gateway'
    _NETRC_MACHINE = 'biliintl'
    _HEADERS = {'Referer': 'https://www.bilibili.tv/'}

    def _call_api(self, endpoint, *args, **kwargs):
        json = self._download_json(self._API_URL + endpoint, *args, **kwargs)
        if json.get('code'):
            if json['code'] in (10004004, 10004005, 10023006):
                self.raise_login_required()
            elif json['code'] == 10004001:
                self.raise_geo_restricted()
            else:
                if json.get('message') and str(json['code']) != json['message']:
                    errmsg = f'{kwargs.get("errnote", "Unable to download JSON metadata")}: {self.IE_NAME} said: {json["message"]}'
                else:
                    errmsg = kwargs.get('errnote', 'Unable to download JSON metadata')
                if kwargs.get('fatal'):
                    raise ExtractorError(errmsg)
                else:
                    self.report_warning(errmsg)
        return json.get('data')