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

Code before:
import re

from .common import InfoExtractor
from ..utils.traversal import traverse_obj
from ..utils import (
    urlhandle_detect_ext,
    url_or_none,
    int_or_none,
    unified_timestamp,
)
from ..networking import HEADRequest


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)


Review comment (suggestion):
Don't redefine `json`. Can also be non-fatal, since we only use it for metadata.
```suggestion
        data = self._download_json(
            f'https://{self._MX3_DOMAIN}/t/{track_id}.json', track_id, fatal=False)
```

Code after:
import re

from .common import InfoExtractor
from ..networking import HEADRequest
from ..utils import (
    get_element_by_class,
    int_or_none,
    try_call,
    url_or_none,
    urlhandle_detect_ext,
)
from ..utils.traversal import traverse_obj


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,