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

Code before:


class AcFunVideoBaseIE(InfoExtractor):
    def parse_format_and_subtitle(self, video_id, video_info):
        if 'ksPlayJson' not in video_info:
            raise ExtractorError(f'Unknown webpage json schema{bug_reports_message()}')
        playjson = self._parse_json(video_info['ksPlayJson'], video_id)

        fmt_jobj = traverse_obj(playjson, ('adaptationSet', 0, 'representation'))
        formats = []
        subtitles = []
        for video in fmt_jobj:
            format_list, subtitle_list = self._extract_m3u8_formats_and_subtitles(video['url'], video_id)

            formats += [{
                **fmt,
                'fps': int_or_none(video.get('frameRate')),
                'width': int_or_none(video.get('width')),
                'height': int_or_none(video.get('height')),
                'tbr': float_or_none(video.get('avgBitrate')),
                'ext': 'mp4',
                'subtitles': subtitle_list,  # it seems AcFun do not have subtitles
                **parse_codecs(video.get('codecs', ''))
            } for fmt in format_list]

            subtitles += subtitle_list

        self._sort_formats(formats)

        return {
            'formats': formats,
            'subtitles': subtitles,
            'duration': float_or_none(video_info.get('durationMillis'), 1000),
            'timestamp': int_or_none(video_info.get('uploadTime'), 1000),
        }


class AcFunVideoIE(AcFunVideoBaseIE):
    _VALID_URL = r'(?x)https?://(?:www\.acfun\.cn/v/)ac(?P<id>[_\d]+)'

    _TESTS = [{
        'url': 'https://www.acfun.cn/v/ac35457073',
        'info_dict': {
            'id': '35457073',
            'title': '1 8 岁 现 状',
            'thumbnail': 'https://tx-free-imgs.acfun.cn/newUpload/51246077_82bcf86c32c54c4d80cbd624ba4cc38c.jpeg?imageslim',
            'description': '“赶紧回去！班主任查班了！”',
            'uploader': '锤子game',
            'uploader_id': '51246077',
            'tags': ['电子竞技', 'LOL', 'CF', '搞笑', '真人'],
            'view_count': 31917,

Review comment (refactor):
```suggestion
        formats, subtitles = [], {}
        for video in fmt_jobj:
            fmts, subs = self._extract_m3u8_formats_and_subtitles(video['url'], video_id, 'mp4', fatal=False)
            formats.extend(fmts)
            self._merge_subtitles(subs, target=subtitles)
            for f in fmts:
                fmts.update({
                    'fps': int_or_none(video.get('frameRate')),
                    'width': int_or_none(video.get('width')),
                    'height': int_or_none(video.get('height')),
                    'tbr': float_or_none(video.get('avgBitrate')),
                    **parse_codecs(video.get('codecs', ''))
                })
```

Code after:
class AcFunVideoBaseIE(InfoExtractor):
    def _extract_metadata(self, video_id, video_info):
        playjson = self._parse_json(video_info['ksPlayJson'], video_id)

        formats, subtitles = [], {}
        for video in traverse_obj(playjson, ('adaptationSet', 0, 'representation')):
            fmts, subs = self._extract_m3u8_formats_and_subtitles(video['url'], video_id, 'mp4', fatal=False)
            formats.extend(fmts)
            self._merge_subtitles(subs, target=subtitles)
            for f in fmts:
                f.update({
                    'fps': float_or_none(video.get('frameRate')),
                    'width': int_or_none(video.get('width')),
                    'height': int_or_none(video.get('height')),
                    'tbr': float_or_none(video.get('avgBitrate')),
                    **parse_codecs(video.get('codecs', ''))
                })

        self._sort_formats(formats)
        return {
            'id': video_id,
            'formats': formats,
            'subtitles': subtitles,
            'duration': float_or_none(video_info.get('durationMillis'), 1000),
            'timestamp': int_or_none(video_info.get('uploadTime'), 1000),
            'http_headers': {'Referer': 'https://www.acfun.cn/'},
        }


class AcFunVideoIE(AcFunVideoBaseIE):
    _VALID_URL = r'https?://www\.acfun\.cn/v/ac(?P<id>[_\d]+)'

    _TESTS = [{
        'url': 'https://www.acfun.cn/v/ac35457073',
        'info_dict': {
            'id': '35457073',
            'ext': 'mp4',
            'duration': 174.208,
            'timestamp': 1656403967,
            'title': '1 8 岁 现 状',
            'description': '“赶紧回去！班主任查班了！”',
            'uploader': '锤子game',
            'uploader_id': '51246077',
            'thumbnail': r're:^https?://.*\.(jpg|jpeg)',
            'upload_date': '20220628',
            'like_count': int,
            'view_count': int,
            'comment_count': int,
            'tags': list,
        },
    }, {