Repository: yt-dlp/yt-dlp

Issue:
Playlist download in Jiosaavn
### DO NOT REMOVE OR SKIP THE ISSUE TEMPLATE

- [X] I understand that I will be **blocked** if I *intentionally* remove or skip any mandatory\* field

### Checklist

- [X] I'm requesting a site-specific feature
- [X] I've verified that I have **updated yt-dlp to nightly or master** ([update instructions](https://github.com/yt-dlp/yt-dlp#update-channels))
- [X] I've checked that all provided URLs are playable in a browser with the same IP and same login details
- [X] I've searched [known issues](https://github.com/yt-dlp/yt-dlp/issues/3766) and the [bugtracker](https://github.com/yt-dlp/yt-dlp/issues?q=) for similar issues **including closed ones**. DO NOT post duplicates
- [X] I've read the [guidelines for opening an issue](https://github.com/yt-dlp/yt-dlp/blob/master/CONTRIBUTING.md#opening-an-issue)
- [X] I've read about [sharing account credentials](https://github.com/yt-dlp/yt-dlp/blob/master/CONTRIBUTING.md#are-you-willing-to-share-account-details-if-needed) and I'm willing to share it if required

### Region

India

### Example URLs

https://www.jiosaavn.com/s/playlist/2279fbe391defa793ad7076929a2f5c9/mood-english/LlJ8ZWT1ibN5084vKHRj2Q__

### Provide a description that is worded well enough to be understood

Please add the playlist download support for Jiosaavn

### Provide verbose output that clearly demonstrates the problem

- [X] Run **your** yt-dlp command with **-vU** flag added (`yt-dlp -vU <your command line>`)
- [X] If using API, add `'verbose': True` to `YoutubeDL` params instead
- [X] Copy the WHOLE output (starting with `[debug] Command-line config`) and insert it below

### Complete Verbose Output

```shell
[debug] Command-line config: ['--verbose', 'https://www.jiosaavn.com/s/playlist/2279fbe391defa793ad7076929a2f5c9/mood-english/LlJ8ZWT1ibN5084vKHRj2Q__']
[debug] Encodings: locale cp1252, fs utf-8, pref cp1252, out utf-8, error utf-8, screen utf-8
[debug] yt-dlp version stable@2024.03.10 from yt-dlp/yt-dlp [615a84447] (win_exe)
[debug] Python 3.8.10 (CPython AMD64 64bit) - Windows-10-10.0.19045-SP0 (OpenSSL 1.1.1k  25 Mar 2021)
[debug] exe versions: ffmpeg N-112207-g8eb094adb2-20230928 (setts), ffprobe N-112207-g8eb094adb2-20230928
[debug] Optional libraries: Cryptodome-3.20.0, brotli-1.1.0, certifi-2024.02.02, mutagen-1.47.0, requests-2.31.0, sqlite3-3.35.5, urllib3-2.2.1, websockets-12.0
[debug] Proxy map: {}
[debug] Request Handlers: urllib, requests, websockets
[debug] Loaded 1803 extractors
[generic] Extracting URL: https://www.jiosaavn.com/s/playlist/2279fbe391defa793ad7076929a2f5c9/mood-english/LlJ8ZWT1ibN5084vKHRj2Q__
[generic] LlJ8ZWT1ibN5084vKHRj2Q__: Downloading webpage
WARNING: [generic] Falling back on generic information extractor
[generic] LlJ8ZWT1ibN5084vKHRj2Q__: Extracting information
[debug] Looking for embeds
ERROR: Unsupported URL: https://www.jiosaavn.com/s/playlist/2279fbe391defa793ad7076929a2f5c9/mood-english/LlJ8ZWT1ibN5084vKHRj2Q__
Traceback (most recent call last):
  File "yt_dlp\YoutubeDL.py", line 1594, in wrapper
  File "yt_dlp\YoutubeDL.py", line 1729, in __extract_info
  File "yt_dlp\extractor\common.py", line 732, in extract
  File "yt_dlp\extractor\generic.py", line 2530, in _real_extract
yt_dlp.utils.UnsupportedError: Unsupported URL: https://www.jiosaavn.com/s/playlist/2279fbe391defa793ad7076929a2f5c9/mood-english/LlJ8ZWT1ibN5084vKHRj2Q__
```



Discussion:

Resolving patch:
diff --git a/yt_dlp/extractor/_extractors.py b/yt_dlp/extractor/_extractors.py
--- a/yt_dlp/extractor/_extractors.py
+++ b/yt_dlp/extractor/_extractors.py
@@ -876,6 +876,7 @@
 from .jiosaavn import (
     JioSaavnSongIE,
     JioSaavnAlbumIE,
+    JioSaavnPlaylistIE,
 )
 from .jove import JoveIE
 from .joj import JojIE
diff --git a/yt_dlp/extractor/jiosaavn.py b/yt_dlp/extractor/jiosaavn.py
--- a/yt_dlp/extractor/jiosaavn.py
+++ b/yt_dlp/extractor/jiosaavn.py
@@ -1,30 +1,90 @@
+import functools
+
 from .common import InfoExtractor
 from ..utils import (
+    format_field,
     int_or_none,
     js_to_json,
-    orderedSet,
+    make_archive_id,
+    smuggle_url,
+    unsmuggle_url,
+    url_basename,
     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)
+    _VALID_BITRATES = {'16', '32', '64', '128', '320'}
+
+    @functools.cached_property
+    def requested_bitrates(self):
+        requested_bitrates = self._configuration_arg('bitrate', ['128', '320'], ie_key='JioSaavn')
+        if invalid_bitrates := set(requested_bitrates) - self._VALID_BITRATES:
+            raise ValueError(
+                f'Invalid bitrate(s): {", ".join(invalid_bitrates)}. '
+                + f'Valid bitrates are: {", ".join(sorted(self._VALID_BITRATES, key=int))}')
+        return requested_bitrates
+
+    def _extract_formats(self, song_data):
+        for bitrate in self.requested_bitrates:
+            media_data = self._download_json(
+                'https://www.jiosaavn.com/api.php', song_data['id'],
+                f'Downloading format info for {bitrate}',
+                fatal=False, data=urlencode_postdata({
+                    '__call': 'song.generateAuthToken',
+                    '_format': 'json',
+                    'bitrate': bitrate,
+                    'url': song_data['encrypted_media_url'],
+                }))
+            if not traverse_obj(media_data, ('auth_url', {url_or_none})):
+                self.report_warning(f'Unable to extract format info for {bitrate}')
+                continue
+            ext = media_data.get('type')
+            yield {
+                'url': media_data['auth_url'],
+                'ext': 'm4a' if ext == 'mp4' else ext,
+                'format_id': bitrate,
+                'abr': int(bitrate),
+                'vcodec': 'none',
+            }
+
+    def _extract_song(self, song_data):
+        info = traverse_obj(song_data, {
+            'id': ('id', {str}),
+            'title': ('title', 'text', {str}),
+            'album': ('album', 'text', {str}),
+            'thumbnail': ('image', 0, {url_or_none}),
+            'duration': ('duration', {int_or_none}),
+            'view_count': ('play_count', {int_or_none}),
+            'release_year': ('year', {int_or_none}),
+            'artists': ('artists', lambda _, v: v['role'] == 'singer', 'name', {str}),
+            'webpage_url': ('perma_url', {url_or_none}),  # for song, playlist extraction
+        })
+        if not info.get('webpage_url'):  # for album extraction / fallback
+            info['webpage_url'] = format_field(
+                song_data, [('title', 'action')], 'https://www.jiosaavn.com%s') or None
+        if webpage_url := info['webpage_url']:
+            info['_old_archive_ids'] = [make_archive_id(JioSaavnSongIE, url_basename(webpage_url))]
+
+        return info
+
+    def _extract_initial_data(self, url, display_id):
+        webpage = self._download_webpage(url, display_id)
         return self._search_json(
             r'window\.__INITIAL_DATA__\s*=', webpage,
-            'init json', audio_id, transform_source=js_to_json)
+            'initial data', display_id, transform_source=js_to_json)
 
 
 class JioSaavnSongIE(JioSaavnBaseIE):
+    IE_NAME = 'jiosaavn:song'
     _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': '3b84396d15ed9e083c3106f1fa589c04',
         'info_dict': {
-            'id': 'OQsEfQFVUXk',
+            'id': 'IcoLuefJ',
             'ext': 'm4a',
             'title': 'Leja Re',
             'album': 'Leja Re',
@@ -32,62 +92,34 @@ class JioSaavnSongIE(JioSaavnBaseIE):
             'duration': 205,
             'view_count': int,
             'release_year': 2018,
-            'artists': ['Sandesh Shandilya', 'Dhvani Bhanushali', 'Tanishk Bagchi', 'Rashmi Virag', 'Irshad Kamil'],
+            'artists': ['Sandesh Shandilya', 'Dhvani Bhanushali', 'Tanishk Bagchi'],
+            '_old_archive_ids': ['jiosaavnsong OQsEfQFVUXk'],
         },
     }, {
         'url': 'https://www.saavn.com/s/song/hindi/Saathiya/O-Humdum-Suniyo-Re/KAMiazoCblU',
         'only_matching': True,
     }]
 
-    _VALID_BITRATES = ('16', '32', '64', '128', '320')
-
     def _real_extract(self, url):
-        audio_id = self._match_id(url)
-        extract_bitrates = self._configuration_arg('bitrate', ['128', '320'], ie_key='JioSaavn')
-        if invalid_bitrates := [br for br in extract_bitrates if br not in self._VALID_BITRATES]:
-            raise ValueError(
-                f'Invalid bitrate(s): {", ".join(invalid_bitrates)}. '
-                + f'Valid bitrates are: {", ".join(self._VALID_BITRATES)}')
+        url, smuggled_data = unsmuggle_url(url)
+        song_data = traverse_obj(smuggled_data, ({
+            'id': ('id', {str}),
+            'encrypted_media_url': ('encrypted_media_url', {str}),
+        }))
 
-        song_data = self._extract_initial_data(url, audio_id)['song']['song']
-        formats = []
-        for bitrate in extract_bitrates:
-            media_data = self._download_json(
-                'https://www.jiosaavn.com/api.php', audio_id, f'Downloading format info for {bitrate}',
-                fatal=False, data=urlencode_postdata({
-                    '__call': 'song.generateAuthToken',
-                    '_format': 'json',
-                    'bitrate': bitrate,
-                    'url': song_data['encrypted_media_url'],
-                }))
-            if not media_data.get('auth_url'):
-                self.report_warning(f'Unable to extract format info for {bitrate}')
-                continue
-            ext = media_data.get('type')
-            formats.append({
-                'url': media_data['auth_url'],
-                'ext': 'm4a' if ext == 'mp4' else ext,
-                'format_id': bitrate,
-                'abr': int(bitrate),
-                'vcodec': 'none',
-            })
+        if 'id' in song_data and 'encrypted_media_url' in song_data:
+            result = {'id': song_data['id']}
+        else:
+            # only extract metadata if this is not a url_transparent result
+            song_data = self._extract_initial_data(url, self._match_id(url))['song']['song']
+            result = self._extract_song(song_data)
 
-        return {
-            'id': audio_id,
-            'formats': formats,
-            **traverse_obj(song_data, {
-                'title': ('title', 'text'),
-                'album': ('album', 'text'),
-                'thumbnail': ('image', 0, {url_or_none}),
-                'duration': ('duration', {int_or_none}),
-                'view_count': ('play_count', {int_or_none}),
-                'release_year': ('year', {int_or_none}),
-                'artists': ('artists', ..., 'name', {str}, all, {orderedSet}),
-            }),
-        }
+        result['formats'] = list(self._extract_formats(song_data))
+        return result
 
 
 class JioSaavnAlbumIE(JioSaavnBaseIE):
+    IE_NAME = 'jiosaavn:album'
     _VALID_URL = r'https?://(?:www\.)?(?:jio)?saavn\.com/album/[^/?#]+/(?P<id>[^/?#]+)'
     _TESTS = [{
         'url': 'https://www.jiosaavn.com/album/96/buIOjYZDrNA_',
@@ -98,12 +130,45 @@ class JioSaavnAlbumIE(JioSaavnBaseIE):
         'playlist_count': 10,
     }]
 
+    def _entries(self, playlist_data):
+        for song_data in traverse_obj(playlist_data, (
+                'modules', lambda _, x: x['key'] == 'list', 'data', lambda _, v: v['title']['action'])):
+            song_info = self._extract_song(song_data)
+            # album song data is missing artists and release_year, need to re-extract metadata
+            yield self.url_result(song_info['webpage_url'], JioSaavnSongIE, **song_info)
+
     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))
+        display_id = self._match_id(url)
+        album_data = self._extract_initial_data(url, display_id)['albumView']
+
+        return self.playlist_result(
+            self._entries(album_data), display_id, traverse_obj(album_data, ('album', 'title', 'text', {str})))
+
+
+class JioSaavnPlaylistIE(JioSaavnBaseIE):
+    IE_NAME = 'jiosaavn:playlist'
+    _VALID_URL = r'https?://(?:www\.)?(?:jio)?saavn\.com/s/playlist/(?:[^/?#]+/){2}(?P<id>[^/?#]+)'
+    _TESTS = [{
+        'url': 'https://www.jiosaavn.com/s/playlist/2279fbe391defa793ad7076929a2f5c9/mood-english/LlJ8ZWT1ibN5084vKHRj2Q__',
+        'info_dict': {
+            'id': 'LlJ8ZWT1ibN5084vKHRj2Q__',
+            'title': 'Mood English',
+        },
+        'playlist_mincount': 50,
+    }]
+
+    def _entries(self, playlist_data):
+        for song_data in traverse_obj(playlist_data, ('list', lambda _, v: v['perma_url'])):
+            song_info = self._extract_song(song_data)
+            url = smuggle_url(song_info['webpage_url'], {
+                'id': song_data['id'],
+                'encrypted_media_url': song_data['encrypted_media_url'],
+            })
+            yield self.url_result(url, JioSaavnSongIE, url_transparent=True, **song_info)
+
+    def _real_extract(self, url):
+        display_id = self._match_id(url)
+        playlist_data = self._extract_initial_data(url, display_id)['playlist']['playlist']
+
+        return self.playlist_result(
+            self._entries(playlist_data), display_id, traverse_obj(playlist_data, ('title', 'text', {str})))


Test patch: