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

Code before:
from ..utils import (
    js_to_json,
    urlencode_postdata,
)


class JioSaavnBaseInfoExtractor(InfoExtractor):
    def extract_initial_data_as_json(self, url, id):
        webpage = self._download_webpage(url, id)
        init_data_re = r'window.__INITIAL_DATA__\s*=\s*(?P<json>.+?);*\s*\</script>'

        init_data = self._parse_json(self._search_regex(init_data_re, webpage,
                                                        'init-json'),
                                     id, transform_source=js_to_json)
        return init_data


class JioSaavnSongIE(JioSaavnBaseInfoExtractor):
    _VALID_URL = r'''(?x)
                    https?://(?:www\.)?
                        (?:
                            jiosaavn\.com/song/[^/]+/|
                            saavn.com/s/song/(?:[^/]+/){3}
                        )
                        (?P<id>[^/]+)
                   '''
    _TESTS = [{
        'url': 'https://www.jiosaavn.com/song/leja-re/OQsEfQFVUXk',
        'md5': '7b1f70de088ede3a152ea34aece4df42',
        'info_dict': {
            'id': 'OQsEfQFVUXk',
            'ext': 'mp3',
            'title': 'Leja Re',
            'album': 'Leja Re',
            'thumbnail': 'https://c.saavncdn.com/258/Leja-Re-Hindi-2018-20181124024539-500x500.jpg',
        },
    }, {
        'url': 'https://www.saavn.com/s/song/hindi/Saathiya/O-Humdum-Suniyo-Re/KAMiazoCblU',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        audio_id = self._match_id(url)
        song_data = self.extract_initial_data_as_json(url, audio_id)['song']['song']

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


Review comment (suggestion):
```suggestion
    _VALID_URL = r'https?://(?:www\.)?(?:jiosaavn\.com/song/[^/?#]+/|saavn\.com/s/song/(?:[^/?#]+/){3})(?P<id>[^/?#]+)'
```

Code after:
from ..utils import (
    js_to_json,
    url_or_none,
    urlencode_postdata,
    urljoin,
)
from ..utils.traversal import traverse_obj


class JioSaavnBaseIE(InfoExtractor):
    def _extract_initial_data(self, url, audio_id):
        webpage = self._download_webpage(url, audio_id)
        return self._search_json(
            r'window\.__INITIAL_DATA__\s*=', webpage,
            'init json', audio_id, transform_source=js_to_json)


class JioSaavnSongIE(JioSaavnBaseIE):
    _VALID_URL = r'https?://(?:www\.)?(?:jiosaavn\.com/song/[^/?#]+/|saavn\.com/s/song/(?:[^/?#]+/){3})(?P<id>[^/?#]+)'
    _TESTS = [{
        'url': 'https://www.jiosaavn.com/song/leja-re/OQsEfQFVUXk',
        'md5': '7b1f70de088ede3a152ea34aece4df42',
        'info_dict': {
            'id': 'OQsEfQFVUXk',
            'ext': 'mp3',
            'title': 'Leja Re',
            'album': 'Leja Re',
            'thumbnail': 'https://c.saavncdn.com/258/Leja-Re-Hindi-2018-20181124024539-500x500.jpg',
        },
    }, {
        'url': 'https://www.saavn.com/s/song/hindi/Saathiya/O-Humdum-Suniyo-Re/KAMiazoCblU',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        audio_id = self._match_id(url)
        song_data = self._extract_initial_data(url, audio_id)['song']['song']
        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,
            'url': media_data['auth_url'],
            'ext': media_data.get('type'),
            'vcodec': 'none',
            **traverse_obj(song_data, {