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

Code before:
    traverse_obj,
    url_or_none,
)


class SverigesRadioBaseIE(InfoExtractor):
    _BASE_URL = 'https://sverigesradio.se/sida/playerajax/'
    _QUALITIES = ['low', 'medium', 'high']
    _EXT_TO_CODEC_MAP = {
        'mp3': 'mp3',
        'm4a': 'aac',
    }
    _CODING_FORMAT_TO_ABR_MAP = {
        5: 128,
        11: 192,
        12: 32,
        13: 96,
    }

    def _real_extract(self, url):
        audio_id, display_id = self._match_valid_url(url).group('id', 'slug')
        if not audio_id:
            webpage = self._download_webpage(url, display_id)
            button = extract_attributes(get_element_html_by_class('audio-button', webpage) or '')
            audio_id = traverse_obj(button, 'data-audio-id', 'data-publication-id') or self._parse_json(
                get_element_by_id('gtm-metadata', webpage), display_id)['pageId']

        query = {
            'id': audio_id,
            'type': self._AUDIO_TYPE,
        }

        item = self._download_json(
            self._BASE_URL + 'audiometadata', audio_id,
            'Downloading audio JSON metadata', query=query)['items'][0]

        query['format'] = 'iis'
        urls = []
        formats = []
        for quality in self._QUALITIES:
            query['quality'] = quality
            audio_url_data = self._download_json(
                self._BASE_URL + 'getaudiourl', audio_id,
                'Downloading %s format JSON metadata' % quality,
                fatal=False, query=query) or {}
            audio_url = audio_url_data.get('audioUrl')
            if not audio_url or audio_url in urls:
                continue
            urls.append(audio_url)
            ext = determine_ext(audio_url)
            coding_format = audio_url_data.get('codingFormat')

Review comment (refactor):
Cosmetic, but I like having the `or` branch clearly separated. inlining `extract_attributes` could be done either way, since it gets `try_call`ed we need not `or ''`:
```suggestion
            audio_id = (
                traverse_obj(
                    get_element_html_by_class('audio-button', webpage),
                    ({extract_attributes}, ('data-audio-id', 'data-publication-id')), get_all=False)
                or self._parse_json(get_element_by_id('gtm-metadata', webpage), display_id)['pageId'])
```

Code after:
    traverse_obj,
    url_or_none,
)


class SverigesRadioBaseIE(InfoExtractor):
    _BASE_URL = 'https://sverigesradio.se/sida/playerajax/'
    _QUALITIES = ['low', 'medium', 'high']
    _EXT_TO_CODEC_MAP = {
        'mp3': 'mp3',
        'm4a': 'aac',
    }
    _CODING_FORMAT_TO_ABR_MAP = {
        5: 128,
        11: 192,
        12: 32,
        13: 96,
    }

    def _real_extract(self, url):
        audio_id, display_id = self._match_valid_url(url).group('id', 'slug')
        if not audio_id:
            webpage = self._download_webpage(url, display_id)
            audio_id = (
                traverse_obj(
                    get_element_html_by_class('audio-button', webpage),
                    ({extract_attributes}, ('data-audio-id', 'data-publication-id')), get_all=False)
                or self._parse_json(get_element_by_id('gtm-metadata', webpage), display_id)['pageId'])

        query = {
            'id': audio_id,
            'type': self._AUDIO_TYPE,
        }

        item = self._download_json(
            self._BASE_URL + 'audiometadata', audio_id,
            'Downloading audio JSON metadata', query=query)['items'][0]

        query['format'] = 'iis'
        urls = []
        formats = []
        for quality in self._QUALITIES:
            query['quality'] = quality
            audio_url_data = self._download_json(
                self._BASE_URL + 'getaudiourl', audio_id,
                'Downloading %s format JSON metadata' % quality,
                fatal=False, query=query) or {}
            audio_url = audio_url_data.get('audioUrl')
            if not audio_url or audio_url in urls:
                continue
            urls.append(audio_url)