]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/hungama.py
[cleanup] Fix infodict returned fields (#8906)
[yt-dlp.git] / yt_dlp / extractor / hungama.py
CommitLineData
8cb5c218 1from .common import InfoExtractor
2543938b
S
2from ..utils import (
3 int_or_none,
4b3a6ef1
OA
4 remove_end,
5 traverse_obj,
14183d1f 6 try_get,
4b3a6ef1
OA
7 unified_timestamp,
8 url_or_none,
2543938b
S
9 urlencode_postdata,
10)
8cb5c218
AG
11
12
4b3a6ef1
OA
13class HungamaBaseIE(InfoExtractor):
14 def _call_api(self, path, content_id, fatal=False):
15 return traverse_obj(self._download_json(
16 f'https://cpage.api.hungama.com/v2/page/content/{content_id}/{path}/detail',
17 content_id, fatal=fatal, query={
18 'device': 'web',
19 'platform': 'a',
20 'storeId': '1',
21 }), ('data', {dict})) or {}
22
23
24class HungamaIE(HungamaBaseIE):
2543938b
S
25 _VALID_URL = r'''(?x)
26 https?://
4b3a6ef1 27 (?:www\.|un\.)?hungama\.com/
2543938b 28 (?:
4b3a6ef1 29 (?:video|movie|short-film)/[^/]+/|
2543938b
S
30 tv-show/(?:[^/]+/){2}\d+/episode/[^/]+/
31 )
32 (?P<id>\d+)
33 '''
34 _TESTS = [{
35 'url': 'http://www.hungama.com/video/krishna-chants/39349649/',
1c09783f 36 'md5': '687c5f1e9f832f3b59f44ed0eb1f120a',
2543938b 37 'info_dict': {
1c09783f 38 'id': '39349649',
2543938b 39 'ext': 'mp4',
1c09783f 40 'title': 'Krishna Chants',
4b3a6ef1 41 'description': ' ',
1c09783f
G
42 'upload_date': '20180829',
43 'duration': 264,
44 'timestamp': 1535500800,
45 'view_count': int,
4b3a6ef1
OA
46 'thumbnail': 'https://images1.hungama.com/tr:n-a_169_m/c/1/0dc/2ca/39349649/39349649_350x197.jpg?v=8',
47 'tags': 'count:6',
48 },
49 }, {
50 'url': 'https://un.hungama.com/short-film/adira/102524179/',
51 'md5': '2278463f5dc9db9054d0c02602d44666',
52 'info_dict': {
53 'id': '102524179',
54 'ext': 'mp4',
55 'title': 'Adira',
56 'description': 'md5:df20cd4d41eabb33634f06de1025a4b4',
57 'upload_date': '20230417',
58 'timestamp': 1681689600,
59 'view_count': int,
60 'thumbnail': 'https://images1.hungama.com/tr:n-a_23_m/c/1/197/ac9/102524179/102524179_350x525.jpg?v=1',
61 'tags': 'count:7',
62 },
2543938b
S
63 }, {
64 'url': 'https://www.hungama.com/movie/kahaani-2/44129919/',
65 'only_matching': True,
66 }, {
67 'url': 'https://www.hungama.com/tv-show/padded-ki-pushup/season-1/44139461/episode/ep-02-training-sasu-pathlaag-karing/44139503/',
68 'only_matching': True,
69 }]
70
71 def _real_extract(self, url):
72 video_id = self._match_id(url)
1c09783f 73 video_json = self._download_json(
2543938b
S
74 'https://www.hungama.com/index.php', video_id,
75 data=urlencode_postdata({'content_id': video_id}), headers={
76 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
77 'X-Requested-With': 'XMLHttpRequest',
78 }, query={
79 'c': 'common',
80 'm': 'get_video_mdn_url',
1c09783f 81 })
1c09783f 82 formats = self._extract_m3u8_formats(video_json['stream_url'], video_id, ext='mp4', m3u8_id='hls')
4b3a6ef1 83 metadata = self._call_api('movie', video_id)
1c09783f
G
84
85 return {
4b3a6ef1
OA
86 **traverse_obj(metadata, ('head', 'data', {
87 'title': ('title', {str}),
88 'description': ('misc', 'description', {str}),
89 'duration': ('duration', {int}), # duration in JSON is incorrect if string
90 'timestamp': ('releasedate', {unified_timestamp}),
91 'view_count': ('misc', 'playcount', {int_or_none}),
92 'thumbnail': ('image', {url_or_none}),
93 'tags': ('misc', 'keywords', ..., {str}),
94 })),
2543938b
S
95 'id': video_id,
96 'formats': formats,
1c09783f
G
97 'subtitles': {
98 'en': [{
99 'url': video_json['sub_title'],
100 'ext': 'vtt',
101 }]
102 } if video_json.get('sub_title') else None,
103 }
2543938b
S
104
105
106class HungamaSongIE(InfoExtractor):
4b3a6ef1
OA
107 _VALID_URL = r'https?://(?:www\.|un\.)?hungama\.com/song/[^/]+/(?P<id>\d+)'
108 _TESTS = [{
8cb5c218 109 'url': 'https://www.hungama.com/song/kitni-haseen-zindagi/2931166/',
4b3a6ef1 110 'md5': '964f46828e8b250aa35e5fdcfdcac367',
8cb5c218
AG
111 'info_dict': {
112 'id': '2931166',
14183d1f 113 'ext': 'mp3',
06b4b90c
S
114 'title': 'Lucky Ali - Kitni Haseen Zindagi',
115 'track': 'Kitni Haseen Zindagi',
116 'artist': 'Lucky Ali',
06b4b90c 117 'release_year': 2000,
4b3a6ef1
OA
118 'thumbnail': 'https://stat2.hungama.ind.in/assets/images/default_images/da-200x200.png',
119 },
120 }, {
121 'url': 'https://un.hungama.com/song/tum-kya-mile-from-rocky-aur-rani-kii-prem-kahaani/103553672',
122 'md5': '964f46828e8b250aa35e5fdcfdcac367',
123 'info_dict': {
124 'id': '103553672',
125 'ext': 'mp3',
126 'title': 'md5:5ebeb1e10771b634ce5f700ce68ae5f4',
127 'track': 'Tum Kya Mile (From "Rocky Aur Rani Kii Prem Kahaani")',
128 'artist': 'Pritam Chakraborty, Arijit Singh, Shreya Ghoshal, Amitabh Bhattacharya',
129 'album': 'Tum Kya Mile (From "Rocky Aur Rani Kii Prem Kahaani")',
130 'release_year': 2023,
131 'thumbnail': 'https://images.hungama.com/c/1/7c2/c7b/103553671/103553671_200x200.jpg',
132 },
133 }]
8cb5c218
AG
134
135 def _real_extract(self, url):
2543938b 136 audio_id = self._match_id(url)
8cb5c218 137
06b4b90c 138 data = self._download_json(
2543938b
S
139 'https://www.hungama.com/audio-player-data/track/%s' % audio_id,
140 audio_id, query={'_country': 'IN'})[0]
06b4b90c
S
141 track = data['song_name']
142 artist = data.get('singer_name')
14183d1f
A
143 formats = []
144 media_json = self._download_json(data.get('file') or data['preview_link'], audio_id)
145 media_url = try_get(media_json, lambda x: x['response']['media_url'], str)
146 media_type = try_get(media_json, lambda x: x['response']['type'], str)
147
148 if media_url:
149 formats.append({
150 'url': media_url,
151 'ext': media_type,
152 'vcodec': 'none',
153 'acodec': media_type,
154 })
06b4b90c
S
155
156 title = '%s - %s' % (artist, track) if artist else track
157 thumbnail = data.get('img_src') or data.get('album_image')
8cb5c218
AG
158
159 return {
2543938b 160 'id': audio_id,
8cb5c218 161 'title': title,
06b4b90c
S
162 'thumbnail': thumbnail,
163 'track': track,
164 'artist': artist,
14183d1f 165 'album': data.get('album_name') or None,
06b4b90c
S
166 'release_year': int_or_none(data.get('date')),
167 'formats': formats,
8cb5c218 168 }
14183d1f
A
169
170
4b3a6ef1
OA
171class HungamaAlbumPlaylistIE(HungamaBaseIE):
172 _VALID_URL = r'https?://(?:www\.|un\.)?hungama\.com/(?P<path>playlists|album)/[^/]+/(?P<id>\d+)'
14183d1f
A
173 _TESTS = [{
174 'url': 'https://www.hungama.com/album/bhuj-the-pride-of-india/69481490/',
175 'playlist_mincount': 7,
176 'info_dict': {
177 'id': '69481490',
178 },
179 }, {
180 'url': 'https://www.hungama.com/playlists/hindi-jan-to-june-2021/123063/',
4b3a6ef1 181 'playlist_mincount': 33,
14183d1f
A
182 'info_dict': {
183 'id': '123063',
184 },
4b3a6ef1
OA
185 }, {
186 'url': 'https://un.hungama.com/album/what-jhumka-%3F-from-rocky-aur-rani-kii-prem-kahaani/103891805/',
187 'playlist_mincount': 1,
188 'info_dict': {
189 'id': '103891805',
190 },
14183d1f
A
191 }]
192
193 def _real_extract(self, url):
4b3a6ef1
OA
194 playlist_id, path = self._match_valid_url(url).group('id', 'path')
195 data = self._call_api(remove_end(path, 's'), playlist_id, fatal=True)
196
197 def entries():
198 for song_url in traverse_obj(data, ('body', 'rows', ..., 'data', 'misc', 'share', {url_or_none})):
199 yield self.url_result(song_url, HungamaSongIE)
200
201 return self.playlist_result(entries(), playlist_id)