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

Code before:
            'format_note': '1080P - 1920x1080',
            'duration': 1864,
            'uploader': '卡尔',
            'uploader_id': '367138632',
            'upload_date': '20210811',
            'view_count': int,
            'comment_count': int,
            'like_count': int,
        }, 'params': {
            'skip_download': True,
        },
    }]

    def _real_extract(self, url: str) -> dict:
        video_id = self._match_id(url)
        api_url = f'https://liveapi.huya.com/moment/getMomentContent?videoId={video_id}'

        try:
            response = self._download_json(api_url, video_id)
        except Exception as e:
            raise ExtractorError(f'Failed to download JSON data: {e}', expected=True)

        video_data = traverse_obj(response, ('data', 'moment', 'videoInfo'))

        if not isinstance(video_data, dict):
            raise ExtractorError('No video data found')

        formats = []
        for definition in video_data.get('definitions', []):
            format_info = {
                'format_id': definition.get('defName'),
                'url': definition.get('url'),
                'ext': determine_ext(definition.get('url', '')),
                'width': int_or_none(definition.get('width')),
                'height': int_or_none(definition.get('height')),
                'filesize': int_or_none(definition.get('size')),
                'protocol': 'http',  # Assuming HTTP protocol
                'format_note': f'{definition.get("defName", "")} - {definition.get("width", "")}x{definition.get("height", "")}',
            }
            formats.append(format_info)

        return {
            'id': video_id,
            'title': video_data.get('videoTitle', 'Untitled'),
            'thumbnail': video_data.get('videoCover'),
            'formats': formats,
            'duration': self._parse_duration(video_data.get('videoDuration', '0')),
            'uploader': video_data.get('nickName'),
            'uploader_id': str_or_none(video_data.get('uid')),
            'upload_date': self._parse_date(video_data.get('videoUploadTime', '')),
            'view_count': video_data.get('videoPlayNum'),

Review comment (refactor):
Why is the first extractor error expected?
Also, if all this data is required anyway, we might as well make it fatal:
```suggestion
        video_data = self._download_json(
            'https://liveapi.huya.com/moment/getMomentContent', video_id,
            query={'videoId': video_id})['data']['moment']['videoInfo']
```

Code after:
        formats = []
        for definition in traverse_obj(video_data, ('definitions', lambda _, v: url_or_none(v['url']))):
            formats.append({
                'url': definition['url'],
                **traverse_obj(definition, {
                    'format_id': ('defName', {str}),
                    'width': ('width', {int_or_none}),
                    'height': ('height', {int_or_none}),
                    'filesize': ('size', {int_or_none}),
                }),
            })

        return {
            'id': video_id,
            'formats': formats,
            **traverse_obj(video_data, {
                'title': ('videoTitle', {str}),
                'thumbnail': ('videoCover', {url_or_none}),
                'duration': ('videoDuration', {parse_duration}),
                'uploader': ('nickName', {str}),
                'uploader_id': ('uid', {str_or_none}),
                'upload_date': ('videoUploadTime', {unified_strdate}),
                'view_count': ('videoPlayNum', {int_or_none}),
                'comment_count': ('videoCommentNum', {int_or_none}),
                'like_count': ('favorCount', {int_or_none}),
            }),
        }