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

Code before:
        data = self._download_json(f'{self._API_BASE}/videos/{video_id}', video_id)
        thumbnails = [{
            'id': f'{quality}p',
            'url': f'{self._CDN_BASE}/video/{video_id}/{quality}.webp',
        } for quality in [48, 96, 144, 240, 512, 1080]]
        formats = []
        url_data = self._download_json(f'{self._API_BASE}/videos/{video_id}/url', video_id, data=b'')
        master_playlist_url = traverse_obj(url_data, ('src', 'hls', 'masterPlaylist'))

        if master_playlist_url:
            formats = self._extract_m3u8_formats(master_playlist_url, video_id, 'mp4', m3u8_id='hls', fatal=False)

        if not formats:
            for format_id, url in traverse_obj(url_data, (
                    'src', ('mp4', 'hls'), 'levels', {dict.items}, lambda _, v: url_or_none(v[1]))):
                if determine_ext(url) == 'm3u8':
                    fmts = self._extract_m3u8_formats(url, video_id, 'mp4', m3u8_id='hls', fatal=False)
                    for f in fmts:
                        f['height'] = int_or_none(format_id)
                    formats.extend(fmts)
                else:
                    formats.append({
                        'url': url,
                        'format_id': format_id,
                        'height': int_or_none(format_id),
                    })

        return {
            'id': video_id,
            'title': data.get('title'),
            'description': data.get('desc'),
            'uploader': traverse_obj(data, ('channel', 'name')),
            'channel_id': data.get('channelId'),
            'channel_url': format_field(data, 'channelId', 'https://banbye.com/channel/%s'),
            'timestamp': unified_timestamp(data.get('publishedAt')),
            'duration': data.get('duration'),
            'tags': data.get('tags'),
            'formats': formats,
            'thumbnails': thumbnails,
            'like_count': data.get('likes'),
            'dislike_count': data.get('dislikes'),
            'view_count': data.get('views'),
            'comment_count': data.get('commentCount'),
        }


class BanByeChannelIE(BanByeBaseIE):
    _VALID_URL = r'https?://(?:www\.)?banbye\.com/(?:en/)?channel/(?P<id>\w+)'
    _TESTS = [{
        'url': 'https://banbye.com/channel/ch_wrealu24',
        'info_dict': {

Review comment (refactor):
- we should always reach this code path (not only `if not formats`) in case there are direct mp4 formats available as well as m3u8 formats
- the `levels` m3u8s are variant playlists, so we don't need to call `_extract_m3u8_formats`, we can just append to `formats`, which simplifies this a bit
- call `self._remove_duplicate_formats` in case the `levels` m3u8 formats are dupes of the master m3u8 formats
- I don't like reusing `url`, let's name it `format_url`
```suggestion
        for format_id, format_url in traverse_obj(url_data, (
                'src', ('mp4', 'hls'), 'levels', {dict.items}, lambda _, v: url_or_none(v[1]))):
            is_hls = determine_ext(format_url) == 'm3u8'
            formats.append({
                'url': format_url,
                'format_id': join_nonempty(is_hls and 'hls', format_id),
                'protocol': 'm3u8_native' if is_hls else 'https',
                'height': int_or_none(format_id),
            })
        self._remove_duplicate_formats(formats)
```

Code after:
            'title': 'md5:183524056bebdfa245fd6d214f63c0fe',
            'description': 'md5:943ac87287ca98d28d8b8797719827c6',
            'uploader': 'wRealu24',
            'channel_id': 'ch_wrealu24',
            'channel_url': 'https://banbye.com/channel/ch_wrealu24',
            'upload_date': '20231113',
            'timestamp': 1699874062,
            'view_count': int,
            'like_count': int,
            'dislike_count': int,
            'comment_count': int,
            'thumbnail': 'https://cdn.banbye.com/video/v_a_gPFuC9LoW5/96.webp',
            'tags': ['jaszczur', 'sejm', 'lewica', 'polska', 'ukrainizacja', 'pierwszeposiedzeniesejmu'],
        },
        'expected_warnings': ['Failed to download m3u8'],
    }, {
        # ['src']['hls']['masterPlaylist'] m3u8 only
        'url': 'https://banbye.com/watch/v_B0rsKWsr-aaa',
        'info_dict': {
            'id': 'v_B0rsKWsr-aaa',
            'ext': 'mp4',
            'title': 'md5:00b254164b82101b3f9e5326037447ed',
            'description': 'md5:3fd8b48aa81954ba024bc60f5de6e167',
            'uploader': 'PSTV Piotr Szlachtowicz ',
            'channel_id': 'ch_KV9EVObkB9wB',
            'channel_url': 'https://banbye.com/channel/ch_KV9EVObkB9wB',
            'upload_date': '20240629',
            'timestamp': 1719646816,
            'duration': 2377,
            'view_count': int,
            'like_count': int,
            'dislike_count': int,
            'comment_count': int,
            'thumbnail': 'https://cdn.banbye.com/video/v_B0rsKWsr-aaa/96.webp',
            'tags': ['Biden', 'Trump', 'Wybory', 'USA'],
        },
    }]

    def _real_extract(self, url):
        video_id = self._match_id(url)
        playlist_id = self._extract_playlist_id(url, 'playlistId')

        if self._yes_playlist(playlist_id, video_id):
            return self._extract_playlist(playlist_id)

        data = self._download_json(f'{self._API_BASE}/videos/{video_id}', video_id)
        thumbnails = [{
            'id': f'{quality}p',
            'url': f'{self._CDN_BASE}/video/{video_id}/{quality}.webp',
        } for quality in [48, 96, 144, 240, 512, 1080]]