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

Code before:
        }

    @staticmethod
    def _epidemic_fmt_or_none(f):
        if not f.get('format'):
            f['format'] = f.get('format_id')
        elif not f.get('format_id'):
            f['format_id'] = f['format']
        if not f['url'] or not f['format']:
            return None
        if f.get('format_note'):
            f['format_note'] = f'track ID {f["format_note"]}'
        if f['format'] != 'full':
            f['preference'] = -2
        return f

    def _real_extract(self, url):
        if self._SFX_REGEX.match(url):
            self._IS_SFX = True

        video_id = self._match_id(url)

        if self._IS_SFX:
            json_data = self._download_json(f'https://www.epidemicsound.com/json/track/kosmos-id/{video_id}', video_id)
        else:
            json_data = self._download_json(f'https://www.epidemicsound.com/json/track/{video_id}', video_id)

        thumbnails = traverse_obj(json_data, [('imageUrl', 'cover')])
        thumb_base_url = traverse_obj(json_data, ('coverArt', 'baseUrl', {url_or_none}))
        if thumb_base_url:
            thumbnails.extend(traverse_obj(json_data, (
                'coverArt', 'sizes', ..., {thumb_base_url.__add__})))

        return traverse_obj(json_data, {
            'id': ('id', {str_or_none}),
            'display_id': ('publicSlug', {str}),
            'title': ('title', {str}),
            'alt_title': ('oldTitle', {str}),
            'duration': ('length', {float_or_none}),
            'timestamp': ('added', {parse_iso8601}),
            'release_timestamp': ('releaseDate', {parse_iso8601}),
            'categories': ('genres', ..., 'tag', {str}),
            'tags': ('metadataTags', ..., {str}),
            'age_limit': ('isExplicit', {lambda b: 18 if b else None}),
            'thumbnails': ({lambda _: thumbnails}, {orderedSet}, ..., {self._epidemic_parse_thumbnail}),
            'formats': ('stems', {dict.items}, ..., {
                'format': (0, {str_or_none}),
                'format_note': (1, 's3TrackId', {str_or_none}),
                'format_id': (1, 'stemType', {str}),
                'url': (1, 'lqMp3Url', {url_or_none}),
            }, {self._epidemic_fmt_or_none}),

Review comment (suggestion):
and finally this
```suggestion
        json_data = self._download_json(join_nonempty(
            'https://www.epidemicsound.com/json/track'
            is_sfx and 'kosmos-id', video_id, delim='/'), video_id)
```

import `join_nonempty` from utils

Code after:
    def _epidemic_parse_thumbnail(url: str):
        if not url_or_none(url):
            return None

        return {
            'url': url,
            **(traverse_obj(url, ({parse_qs}, {
                'width': ('width', 0, {int_or_none}),
                'height': ('height', 0, {int_or_none}),
            })) or parse_resolution(url)),
        }

    @staticmethod
    def _epidemic_fmt_or_none(f):
        if not f.get('format'):
            f['format'] = f.get('format_id')
        elif not f.get('format_id'):
            f['format_id'] = f['format']
        if not f['url'] or not f['format']:
            return None
        if f.get('format_note'):
            f['format_note'] = f'track ID {f["format_note"]}'
        if f['format'] != 'full':
            f['preference'] = -2
        return f

    def _real_extract(self, url):
        video_id, is_sfx = self._match_valid_url(url).group('id', 'sfx')
        json_data = self._download_json(join_nonempty(
            'https://www.epidemicsound.com/json/track',
            is_sfx and 'kosmos-id', video_id, delim='/'), video_id)

        thumbnails = traverse_obj(json_data, [('imageUrl', 'cover')])
        thumb_base_url = traverse_obj(json_data, ('coverArt', 'baseUrl', {url_or_none}))
        if thumb_base_url:
            thumbnails.extend(traverse_obj(json_data, (
                'coverArt', 'sizes', ..., {thumb_base_url.__add__})))

        return traverse_obj(json_data, {
            'id': ('id', {str_or_none}),
            'display_id': ('publicSlug', {str}),
            'title': ('title', {str}),
            'alt_title': ('oldTitle', {str}),
            'duration': ('length', {float_or_none}),
            'timestamp': ('added', {parse_iso8601}),
            'release_timestamp': ('releaseDate', {parse_iso8601}),
            'categories': ('genres', ..., 'tag', {str}),
            'tags': ('metadataTags', ..., {str}),
            'age_limit': ('isExplicit', {lambda b: 18 if b else None}),
            'thumbnails': ({lambda _: thumbnails}, {orderedSet}, ..., {self._epidemic_parse_thumbnail}),
            'formats': ('stems', {dict.items}, ..., {