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

Code before:
class Mx3BaseIE(InfoExtractor):
    _MX3_DOMAIN = None

    def _real_extract(self, url):
        track_id = self._match_id(url)
        webpage = self._download_webpage(url, track_id)
        json = self._download_json(f'https://{self._MX3_DOMAIN}/t/{track_id}.json', track_id)

        artists = []
        if json.get('artist'):
            artists.append(json['artist'])
        performer = json.get('performer_name')
        if performer and performer not in artists:
            artists.append(performer)

        genre = self._html_search_regex(r'<div\b[^>]+class="single-band-genre"[^>]*>([^<]+)</div>',
                                        webpage, 'genre', fatal=False, flags=re.DOTALL)

        formats = []

        def add_format(fmt, fatal):
            if fatal:
                urlh = self._request_webpage(HEADRequest(fmt['url']), track_id, note='Fetching default media headers')
            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 {

Review comment (suggestion):
I think all formats should be non-fatal
```suggestion
            urlh = self._request_webpage(
                HEADRequest(fmt['url']), track_id, fatal=False, expected_status=404,
                note=f'Checking for format {fmt["format_id"]}')
```

Code after:

class Mx3BaseIE(InfoExtractor):
    _VALID_URL_TMPL = r'https?://(?:www\.)?%s/t/(?P<id>\w+)'
    _FORMATS = [{
        'url': 'player_asset',
        'format_id': 'default',
        'quality': 0,
    }, {
        'url': 'player_asset?quality=hd',
        'format_id': 'hd',
        'quality': 1,
    }, {
        'url': 'download',
        'format_id': 'download',
        'quality': 2,
    }, {
        'url': 'player_asset?quality=source',
        'format_id': 'source',
        'quality': 2,
    }]

    def _extract_formats(self, track_id):
        formats = []
        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),