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

Code before:
        data = urlencode_postdata({'__call': 'song.generateAuthToken',
                                   '_format': 'json',
                                   'bitrate': '128',
                                   'url': song_data['encrypted_media_url'],
                                   })

        def clean_api_json(resp):
            return self._search_regex(r'(?P<json>\{.+?})', resp, 'api-json')

        media_url = self._download_json('https://www.jiosaavn.com/api.php',
                                        audio_id, data=data,
                                        transform_source=clean_api_json,
                                        )['auth_url']

        return {
            'id': audio_id,
            'title': song_data['title']['text'],
            'formats': [{
                'url': media_url,
                'ext': 'mp3',
                'format_note': 'MPEG audio',
                'format_id': '128',
                'vcodec': 'none',
            }],
            'album': song_data.get('album', {}).get('text'),
            'thumbnail': song_data.get('image', [None])[0],
        }


class JioSaavnAlbumIE(JioSaavnBaseInfoExtractor):
    _VALID_URL = r'''(?x)
                    https?://(?:www\.)?
                        (?:(?:jio)?saavn\.com/album/[^/]+/)
                        (?P<id>[^/]+)
                   '''
    _TESTS = [{
        'url': 'https://www.jiosaavn.com/album/96/buIOjYZDrNA_',
        'info_dict': {
            'id': 'buIOjYZDrNA_',
            'title': '96',
        },
        'playlist_count': 10,
    }]

    def _real_extract(self, url):
        album_id = self._match_id(url)
        json_data = self.extract_initial_data_as_json(url, album_id)
        album_data = json_data['albumView']['album']
        songs = json_data['albumView']['modules'][0]['data']
        songs = [self.url_result(f'https://www.jiosaavn.com{song["title"]["action"]}') for song in songs]


Review comment (security):
I got returned a mp4 file with AAC audio, so hard-coding mp3 doesn't make sense. We can also make title extraction non-fatal.
I am a bit unsure regarding the `transform_source=clean_api_json`. Is that really needed? The two test cases seem to work without. I have removed it in the suggestion.
```suggestion
        media_data = self._download_json(
            'https://www.jiosaavn.com/api.php', audio_id, data=urlencode_postdata({
                '__call': 'song.generateAuthToken',
                '_format': 'json',
                'bitrate': '128',
                'url': song_data['encrypted_media_url'],
            }))

        return {
            'id': audio_id,
            'formats': [{
                'url': media_data['auth_url'],
                'container': media_data.get('type'),
                'vcodec': 'none',
            }],
            **traverse_obj(song_data, {
                'title': ('title', 'text'),
                'album': ('album', 'text'),
                'thumbnail': ('image', 0, {url_or_none}),
            }),
```
Tests will need to be adjusted and `url_or_none` and `from ..utils.traversal import traverse_obj` added to the imports.

Code after:
        return {
            'id': audio_id,
            'url': media_data['auth_url'],
            'ext': media_data.get('type'),
            'vcodec': 'none',
            **traverse_obj(song_data, {
                'title': ('title', 'text'),
                'album': ('album', 'text'),
                'thumbnail': ('image', 0, {url_or_none}),
            }),
        }


class JioSaavnAlbumIE(JioSaavnBaseIE):
    _VALID_URL = r'https?://(?:www\.)?(?:jio)?saavn\.com/album/[^/?#]+/(?P<id>[^/?#]+)'
    _TESTS = [{
        'url': 'https://www.jiosaavn.com/album/96/buIOjYZDrNA_',
        'info_dict': {
            'id': 'buIOjYZDrNA_',
            'title': '96',
        },
        'playlist_count': 10,
    }]

    def _real_extract(self, url):
        album_id = self._match_id(url)
        album_view = self._extract_initial_data(url, album_id)['albumView']

        return self.playlist_from_matches(
            traverse_obj(album_view, (
                'modules', lambda _, x: x['key'] == 'list', 'data', ..., 'title', 'action', {str})),
            album_id, traverse_obj(album_view, ('album', 'title', 'text', {str})), ie=JioSaavnSongIE,
            getter=lambda x: urljoin('https://www.jiosaavn.com/', x))