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

Code before:
            else:
                urlh = self._request_webpage(HEADRequest(fmt['url']), track_id, fatal=False, expected_status=404,
                                             note=f'Trying media headers for optional format {fmt["format_id"]}')
            if urlh and urlh.status == 200:
                fmt['ext'] = urlhandle_detect_ext(urlh)
                fmt['filesize'] = int_or_none(urlh.headers.get('Content-Length'))
                fmt['timestamp'] = unified_timestamp(urlh.headers.get('Last-Modified'))
                formats.append(fmt)

        track_url = f'https://{self._MX3_DOMAIN}/tracks/{track_id}'
        add_format({
            'url': f'{track_url}/player_asset',
            'format_id': 'default',
            'quality': 1,
        }, fatal=True)
        # the formats below don't always exist
        add_format({
            'url': f'{track_url}/player_asset?quality=hd',
            'format_id': 'hd',
            'quality': 10,
        }, fatal=False)
        add_format({
            'url': f'{track_url}/download',
            'format_id': 'download',
            'quality': 11,
        }, fatal=False)

        return {
            'id': track_id,
            'formats': formats,
            'artist': ', '.join(artists),
            'genre': genre,
            **traverse_obj(json, {
                'title': ('title', {str}),
                'composer': ('composer_name', {str}),
                'thumbnail': (('picture_url_xlarge', 'picture_url'), {url_or_none}),
            }, get_all=False),
        }


class Mx3IE(Mx3BaseIE):
    _MX3_DOMAIN = 'mx3.ch'
    _VALID_URL = r'https?://(?:www\.)?mx3\.ch/t/(?P<id>[0-9A-Za-z]+)'
    _TESTS = [{
        'url': 'https://mx3.ch/t/1Cru',
        'md5': '82510bf4c21f17da41bff7e1ffd84e78',
        'info_dict': {
            'id': '1Cru',
            # This one is audio-only. It's a mp3, but we have to make a HEAD request to find out.
            'ext': 'mp3',
            'artist': 'Tortue Tortue, Godina',

Review comment (refactor):
imo. yt-dlp should always download the highest quality format by default. We also prefer the source file on other extractors like Vimeo. If you don't want to download the highest quality format, you can use `-f` or `-S`.
```suggestion
        add_format({
            'url': f'{track_url}/player_asset',
            'format_id': 'default',
            'quality': 1,
        })
        add_format({
            'url': f'{track_url}/player_asset?quality=hd',
            'format_id': 'hd',
            'quality': 10,
        })
        add_format({
            'url': f'{track_url}/download',
            'format_id': 'download',
            'quality': 11,
        })
        add_format({
            'url': f'{track_url}/player_asset?quality=source',
            'format_id': 'source',
            'quality': 11,
        })
```

Code after:
        for fmt in self._FORMATS:
            format_url = f'https://{self._DOMAIN}/tracks/{track_id}/{fmt["url"]}'
            urlh = self._request_webpage(
                HEADRequest(format_url), track_id, fatal=False, expected_status=404,
                note=f'Checking for format {fmt["format_id"]}')
            if urlh and urlh.status == 200:
                formats.append({
                    **fmt,
                    'url': format_url,
                    'ext': urlhandle_detect_ext(urlh),
                    'filesize': int_or_none(urlh.headers.get('Content-Length')),
                })
        return formats

    def _real_extract(self, url):
        track_id = self._match_id(url)
        webpage = self._download_webpage(url, track_id)
        more_info = get_element_by_class('single-more-info', webpage)
        data = self._download_json(f'https://{self._DOMAIN}/t/{track_id}.json', track_id, fatal=False)

        def get_info_field(name):
            return self._html_search_regex(
                rf'<dt[^>]*>\s*{name}\s*</dt>\s*<dd[^>]*>(.*?)</dd>',
                more_info, name, default=None, flags=re.DOTALL)

        return {
            'id': track_id,
            'formats': self._extract_formats(track_id),
            'genre': self._html_search_regex(
                r'<div\b[^>]+class="single-band-genre"[^>]*>([^<]+)</div>', webpage, 'genre', default=None),
            'release_year': int_or_none(get_info_field('Year of creation')),
            'description': get_info_field('Description'),
            'tags': try_call(lambda: get_info_field('Tag').split(', '), list),
            **traverse_obj(data, {
                'title': ('title', {str}),
                'artist': (('performer_name', 'artist'), {str}),
                'album_artist': ('artist', {str}),
                'composer': ('composer_name', {str}),
                'thumbnail': (('picture_url_xlarge', 'picture_url'), {url_or_none}),
            }, get_all=False),
        }


class Mx3IE(Mx3BaseIE):
    _DOMAIN = 'mx3.ch'
    _VALID_URL = Mx3BaseIE._VALID_URL_TMPL % re.escape(_DOMAIN)
    _TESTS = [{
        'url': 'https://mx3.ch/t/1Cru',
        'md5': '7ba09e9826b4447d4e1ce9d69e0e295f',
        'info_dict': {
            'id': '1Cru',