]>
Commit | Line | Data |
---|---|---|
b530118e AG |
1 | from .common import InfoExtractor |
2 | from ..utils import ( | |
5154dc0a | 3 | int_or_none, |
b530118e AG |
4 | js_to_json, |
5 | url_or_none, | |
6 | urlencode_postdata, | |
7 | urljoin, | |
8 | ) | |
9 | from ..utils.traversal import traverse_obj | |
10 | ||
11 | ||
12 | class JioSaavnBaseIE(InfoExtractor): | |
13 | def _extract_initial_data(self, url, audio_id): | |
14 | webpage = self._download_webpage(url, audio_id) | |
15 | return self._search_json( | |
16 | r'window\.__INITIAL_DATA__\s*=', webpage, | |
17 | 'init json', audio_id, transform_source=js_to_json) | |
18 | ||
19 | ||
20 | class JioSaavnSongIE(JioSaavnBaseIE): | |
21 | _VALID_URL = r'https?://(?:www\.)?(?:jiosaavn\.com/song/[^/?#]+/|saavn\.com/s/song/(?:[^/?#]+/){3})(?P<id>[^/?#]+)' | |
22 | _TESTS = [{ | |
23 | 'url': 'https://www.jiosaavn.com/song/leja-re/OQsEfQFVUXk', | |
5154dc0a | 24 | 'md5': '3b84396d15ed9e083c3106f1fa589c04', |
b530118e AG |
25 | 'info_dict': { |
26 | 'id': 'OQsEfQFVUXk', | |
5154dc0a | 27 | 'ext': 'mp4', |
b530118e AG |
28 | 'title': 'Leja Re', |
29 | 'album': 'Leja Re', | |
30 | 'thumbnail': 'https://c.saavncdn.com/258/Leja-Re-Hindi-2018-20181124024539-500x500.jpg', | |
5154dc0a | 31 | 'duration': 205, |
32 | 'view_count': int, | |
33 | 'release_year': 2018, | |
b530118e AG |
34 | }, |
35 | }, { | |
36 | 'url': 'https://www.saavn.com/s/song/hindi/Saathiya/O-Humdum-Suniyo-Re/KAMiazoCblU', | |
37 | 'only_matching': True, | |
38 | }] | |
39 | ||
5154dc0a | 40 | _VALID_BITRATES = ('16', '32', '64', '128', '320') |
41 | ||
b530118e AG |
42 | def _real_extract(self, url): |
43 | audio_id = self._match_id(url) | |
5154dc0a | 44 | extract_bitrates = self._configuration_arg('bitrate', ['128', '320'], ie_key='JioSaavn') |
45 | if invalid_bitrates := [br for br in extract_bitrates if br not in self._VALID_BITRATES]: | |
46 | raise ValueError( | |
47 | f'Invalid bitrate(s): {", ".join(invalid_bitrates)}. ' | |
48 | + f'Valid bitrates are: {", ".join(self._VALID_BITRATES)}') | |
49 | ||
b530118e | 50 | song_data = self._extract_initial_data(url, audio_id)['song']['song'] |
5154dc0a | 51 | formats = [] |
52 | for bitrate in extract_bitrates: | |
53 | media_data = self._download_json( | |
54 | 'https://www.jiosaavn.com/api.php', audio_id, f'Downloading format info for {bitrate}', | |
55 | fatal=False, data=urlencode_postdata({ | |
56 | '__call': 'song.generateAuthToken', | |
57 | '_format': 'json', | |
58 | 'bitrate': bitrate, | |
59 | 'url': song_data['encrypted_media_url'], | |
60 | })) | |
61 | if not media_data.get('auth_url'): | |
62 | self.report_warning(f'Unable to extract format info for {bitrate}') | |
63 | continue | |
64 | formats.append({ | |
65 | 'url': media_data['auth_url'], | |
66 | 'ext': media_data.get('type'), | |
67 | 'format_id': bitrate, | |
68 | 'abr': int(bitrate), | |
69 | 'vcodec': 'none', | |
70 | }) | |
b530118e AG |
71 | |
72 | return { | |
73 | 'id': audio_id, | |
5154dc0a | 74 | 'formats': formats, |
b530118e AG |
75 | **traverse_obj(song_data, { |
76 | 'title': ('title', 'text'), | |
77 | 'album': ('album', 'text'), | |
78 | 'thumbnail': ('image', 0, {url_or_none}), | |
5154dc0a | 79 | 'duration': ('duration', {int_or_none}), |
80 | 'view_count': ('play_count', {int_or_none}), | |
81 | 'release_year': ('year', {int_or_none}), | |
b530118e AG |
82 | }), |
83 | } | |
84 | ||
85 | ||
86 | class JioSaavnAlbumIE(JioSaavnBaseIE): | |
87 | _VALID_URL = r'https?://(?:www\.)?(?:jio)?saavn\.com/album/[^/?#]+/(?P<id>[^/?#]+)' | |
88 | _TESTS = [{ | |
89 | 'url': 'https://www.jiosaavn.com/album/96/buIOjYZDrNA_', | |
90 | 'info_dict': { | |
91 | 'id': 'buIOjYZDrNA_', | |
92 | 'title': '96', | |
93 | }, | |
94 | 'playlist_count': 10, | |
95 | }] | |
96 | ||
97 | def _real_extract(self, url): | |
98 | album_id = self._match_id(url) | |
99 | album_view = self._extract_initial_data(url, album_id)['albumView'] | |
100 | ||
101 | return self.playlist_from_matches( | |
102 | traverse_obj(album_view, ( | |
103 | 'modules', lambda _, x: x['key'] == 'list', 'data', ..., 'title', 'action', {str})), | |
104 | album_id, traverse_obj(album_view, ('album', 'title', 'text', {str})), ie=JioSaavnSongIE, | |
105 | getter=lambda x: urljoin('https://www.jiosaavn.com/', x)) |