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

Code before:
        'only_matching': True,
    }]

    def _real_extract(self, url):
        video_id = self._match_id(url)

        base_id, ac_idx = self._search_regex(r'(?P<id>aa[_\d]+)(?:\?ac=(?P<ac_idx>\d+))?', video_id,
                                             'ac_idx_parse', group=['id', 'ac_idx']) or (None, None)
        if base_id is not None:
            video_id = f'{base_id}{format_field(ac_idx, template="_%s")}'

        webpage = self._download_webpage(url, video_id)
        json_bangumi_data = self._search_json(r'window.bangumiData\s*=\s*', webpage, 'bangumiData', video_id)

        info = {}
        if not ac_idx:
            if 'currentVideoInfo' not in json_bangumi_data:
                raise ExtractorError(f'Unknown webpage json schema{bug_reports_message()}')
            video_info = json_bangumi_data['currentVideoInfo']
            title = json_bangumi_data.get('showTitle', '')
            season_id = json_bangumi_data.get('bangumiId')

            season_number = None
            if season_id:
                season_number = next((
                    idx + 1 for (idx, v) in enumerate(json_bangumi_data.get('relatedBangumis', []))
                    if v.get('id') == season_id), 1)

            json_bangumi_list = self._search_json(r'window.bangumiList\s*=\s*', webpage, 'bangumiList', video_id) or {}
            video_internal_id = int_or_none(traverse_obj(json_bangumi_data, ('currentVideoInfo', 'id')))
            episode_number = None
            if video_internal_id:
                episode_number = next(
                    idx + 1 for (idx, v) in enumerate(json_bangumi_list.get('items', []))
                    if v.get('videoId') == video_internal_id)

            info.update({
                'thumbnail': json_bangumi_data.get('image'),
                'comment_count': int_or_none(json_bangumi_data.get('commentCount')),
                'season': json_bangumi_data.get('bangumiTitle'),
                'season_id': season_id,
                'season_number': season_number,
                'episode': json_bangumi_data.get('title'),
                'episode_number': episode_number,
            })
        else:
            # if has ac_idx, this url is a proxy to other video which is at https://www.acfun.cn/v/ac
            # the normal video_id is not in json
            if 'hlVideoInfo' not in json_bangumi_data:
                raise ExtractorError(f'Unknown webpage json schema{bug_reports_message()}')
            video_info = json_bangumi_data['hlVideoInfo']

Review comment (suggestion):
```suggestion
                    idx + 1 for (idx, v) in enumerate(json_bangumi_data.get('relatedBangumis') or [])
```

Code after:
            'title': '红孩儿之趴趴蛙寻石记 第5话 ',
            'duration': 760.0,
            'season': '红孩儿之趴趴蛙寻石记',
            'season_id': 5023171,
            'season_number': 1,  # series has only 1 season
            'episode': 'Episode 5',
            'episode_number': 5,
            'upload_date': '20181223',
            'timestamp': 1545552185,
            'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)',
            'comment_count': int,
        },
    }, {
        'url': 'https://www.acfun.cn/bangumi/aa6065485_36188_1885061',
        'info_dict': {
            'id': 'aa6065485_36188_1885061',
            'ext': 'mp4',
            'title': '叽歪老表（第二季） 第5话 坚不可摧',
            'season': '叽歪老表（第二季）',
            'season_number': 2,
            'season_id': 6065485,
            'episode': '坚不可摧',
            'episode_number': 5,
            'upload_date': '20220324',
            'timestamp': 1648082786,
            'duration': 105.002,
            'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)',
            'comment_count': int,
        },
    }]

    def _real_extract(self, url):
        video_id = self._match_id(url)
        ac_idx = parse_qs(url).get('ac', [None])[-1]
        video_id = f'{video_id}{format_field(ac_idx, template="__%s")}'

        webpage = self._download_webpage(url, video_id)
        json_bangumi_data = self._search_json(r'window.bangumiData\s*=\s*', webpage, 'bangumiData', video_id)

        if ac_idx:
            video_info = json_bangumi_data['hlVideoInfo']
            return {
                **self._extract_metadata(video_id, video_info),
                'title': video_info.get('title'),
            }

        video_info = json_bangumi_data['currentVideoInfo']

        season_id = json_bangumi_data.get('bangumiId')
        season_number = season_id and next((
            idx for idx, v in enumerate(json_bangumi_data.get('relatedBangumis') or [], 1)