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

Code before:
            if primary_artists:
                info['artists'] = primary_artists

        if info.get('artists'):
            info['artists'] = list(set(info['artists']))

        return info

    def _call_api(self, type_, token, note='API', params={}):
        return self._download_json(
            self._API_URL, token, f'Downloading {note} JSON', f'Unable to download {note} JSON',
            query={
                '__call': 'webapi.get',
                '_format': 'json',
                '_marker': '0',
                'ctx': 'web6dot0',
                'token': token,
                'type': type_,
                **params,
            })

    def _yield_songs(self, playlist_data):
        for song_data in traverse_obj(playlist_data, ('songs', lambda _, v: v['id'] and v['perma_url'])):
            song_info = self._extract_song(song_data)
            song_info['formats'] = list(self._extract_formats(song_data))
            yield song_info


class JioSaavnSongIE(JioSaavnBaseIE):
    IE_NAME = 'jiosaavn:song'
    _VALID_URL = [
        JioSaavnBaseIE._URL_BASE_RE + r'/(?:song|shows)/[^/?#]+/(?P<id>[^/?#]+)$',
        JioSaavnBaseIE._URL_BASE_RE + r'/s/song/(?:[^/?#]+/){3}(?P<id>[^/?#]+)',
    ]
    _TESTS = [{
        'url': 'https://www.jiosaavn.com/song/leja-re/OQsEfQFVUXk',
        'md5': '3b84396d15ed9e083c3106f1fa589c04',
        'info_dict': {
            'id': 'IcoLuefJ',
            'display_id': 'OQsEfQFVUXk',
            'ext': 'm4a',
            'title': 'Leja Re',
            'album': 'Leja Re',
            'thumbnail': r're:https?://.*\.jpg',
            'duration': 205,
            'view_count': int,
            'release_year': 2018,
            'artists': ['Sandesh Shandilya', 'Dhvani Bhanushali', 'Tanishk Bagchi'],
            '_old_archive_ids': ['jiosaavnsong OQsEfQFVUXk'],
            'media_type': 'song',
            'channel': 'T-Series',

Review comment (refactor):
We want the formats to be extracted lazily which is why we were smuggling the `id`'s and `encrypted_media_url`s in `url_transparent` results

Code after:
            'language': ('language', {lambda x: ISO639Utils.short2long(x.casefold()) or 'und'}),
            'webpage_url': ('perma_url', {url_or_none}),
            'artists': ('more_info', 'artistMap', 'primary_artists', ..., 'name', {str}, filter, all),
        })
        if webpage_url := info.get('webpage_url') or url:
            info['display_id'] = url_basename(webpage_url)
            info['_old_archive_ids'] = [make_archive_id(JioSaavnSongIE, info['display_id'])]

        if primary_artists := traverse_obj(song_data, ('primary_artists', {lambda x: x.split(', ') if x else None})):
            info['artists'].extend(primary_artists)
        if featured_artists := traverse_obj(song_data, ('featured_artists', {str}, filter)):
            info['artists'].extend(featured_artists.split(', '))
        info['artists'] = orderedSet(info['artists']) or None

        return info

    @staticmethod
    def _extract_episode(episode_data, url=None):
        info = JioSaavnBaseIE._extract_song(episode_data, url)
        info.pop('_old_archive_ids', None)
        info.update(traverse_obj(episode_data, {
            'description': ('more_info', 'description', {str}),
            'timestamp': ('more_info', 'release_time', {unified_timestamp}),
            'series': ('more_info', 'show_title', {str}),
            'series_id': ('more_info', 'show_id', {str}),
            'season': ('more_info', 'season_title', {str}),
            'season_number': ('more_info', 'season_no', {int_or_none}),
            'season_id': ('more_info', 'season_id', {str}),
            'episode_number': ('more_info', 'episode_number', {int_or_none}),
            'cast': ('starring', {lambda x: x.split(', ') if x else None}),
        }))
        return info

    def _extract_jiosaavn_result(self, url, endpoint, response_key, parse_func):
        url, smuggled_data = unsmuggle_url(url)
        data = traverse_obj(smuggled_data, ({
            'id': ('id', {str}),
            'encrypted_media_url': ('encrypted_media_url', {str}),
        }))

        if 'id' in data and 'encrypted_media_url' in data:
            result = {'id': data['id']}
        else:
            # only extract metadata if this is not a url_transparent result
            data = self._call_api(endpoint, self._match_id(url))[response_key][0]
            result = parse_func(data, url)

        result['formats'] = list(self._extract_formats(data))
        return result

    def _yield_items(self, playlist_data, keys=None, parse_func=None):