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

Code before:
                (idx + 1, v) for (idx, v) in enumerate(video_list)
                if v['id'] == video_internal_id)

            if len(video_list) > 1:
                title = f'{title} P{p_idx:02d} {p_video_info["title"]}'

        return {
            'id': video_id,
            'title': title,
            'thumbnail': json_all.get('coverUrl'),
            'description': json_all.get('description'),
            'uploader': traverse_obj(json_all, ('user', 'name')),
            'uploader_id': traverse_obj(json_all, ('user', 'href')),
            'tags': traverse_obj(json_all, ('tagList', ..., 'name')),
            'view_count': int_or_none(json_all.get('viewCount')),
            'like_count': int_or_none(json_all.get('likeCountShow')),
            'comment_count': int_or_none(json_all.get('commentCountShow')),
            'http_headers': {
                'Referer': url,
            },
            **self.parse_format_and_subtitle(video_id, video_info)
        }


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

    _TESTS = [{
        'url': 'https://www.acfun.cn/bangumi/aa6002917',
        'info_dict': {
            'id': 'aa6002917',
            'title': '租借女友 第1话 租借女友',
            'duration': 1467,
            'timestamp': 1594432800,
        },
        'params': {
            # m3u8 download
            'skip_download': 'm3u8',
        },
        'skip': 'Geo-restricted to China',
    }, {
        'url': 'https://www.acfun.cn/bangumi/aa6002917_36188_1745457?ac=2',
        '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:

Review comment (refactor):
1. This doesnt handle URLs like `...?foo=bar&ac=1234`
2. Unncessary groups
3. You are matching everything into id but the parsing it again later

```suggestion
    _VALID_URL = r'https?://www\.acfun\.cn/bangumi/(?P<id>aa[_\d]+)/?(?:\?([^#]+&)?ac=(?<ac_id>\d+))?'
```
Then in real_extract, you can just do
```py
video_id, ac_idx = self._match_valid_url('id', 'ac_id')
```

Alternatively, you can do only
```suggestion
    _VALID_URL = r'https?://www\.acfun\.cn/bangumi/(?P<id>aa[_\d]+)'
```
and in real_extract
```py
video_id = self._match_id(url)
ac_idx = parse_qs(url).get('ac', [None])[-1]
```

Code after:

        title = json_all.get('title')
        video_list = json_all.get('videoList') or []
        video_internal_id = traverse_obj(json_all, ('currentVideoInfo', 'id'))
        if video_internal_id and len(video_list) > 1:
            part_idx, part_video_info = next(
                (idx + 1, v) for (idx, v) in enumerate(video_list)
                if v['id'] == video_internal_id)
            title = f'{title} P{part_idx:02d} {part_video_info["title"]}'

        return {
            **self._extract_metadata(video_id, json_all['currentVideoInfo']),
            'title': title,
            'thumbnail': json_all.get('coverUrl'),
            'description': json_all.get('description'),
            'uploader': traverse_obj(json_all, ('user', 'name')),
            'uploader_id': traverse_obj(json_all, ('user', 'href')),
            'tags': traverse_obj(json_all, ('tagList', ..., 'name')),
            'view_count': int_or_none(json_all.get('viewCount')),
            'like_count': int_or_none(json_all.get('likeCountShow')),
            'comment_count': int_or_none(json_all.get('commentCountShow')),
        }


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

    _TESTS = [{
        'url': 'https://www.acfun.cn/bangumi/aa6002917_36188_1745457?ac=2',
        'info_dict': {
            'id': 'aa6002917_36188_1745457__2',
            'ext': 'mp4',
            'title': '【7月】租借女友 水原千鹤角色曲『DATE』特别PV',
            'upload_date': '20200916',
            'timestamp': 1600243813,
            'duration': 92.091,
        },
    }, {
        'url': 'https://www.acfun.cn/bangumi/aa5023171_36188_1750645',
        'info_dict': {
            'id': 'aa5023171_36188_1750645',
            'ext': 'mp4',
            '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,