]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/hungama.py
[ie/Canal1,CaracolTvPlay] Add extractors (#7151)
[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',
14183d1f 117 'album': None,
06b4b90c 118 'release_year': 2000,
4b3a6ef1
OA
119 'thumbnail': 'https://stat2.hungama.ind.in/assets/images/default_images/da-200x200.png',
120 },
121 }, {
122 'url': 'https://un.hungama.com/song/tum-kya-mile-from-rocky-aur-rani-kii-prem-kahaani/103553672',
123 'md5': '964f46828e8b250aa35e5fdcfdcac367',
124 'info_dict': {
125 'id': '103553672',
126 'ext': 'mp3',
127 'title': 'md5:5ebeb1e10771b634ce5f700ce68ae5f4',
128 'track': 'Tum Kya Mile (From "Rocky Aur Rani Kii Prem Kahaani")',
129 'artist': 'Pritam Chakraborty, Arijit Singh, Shreya Ghoshal, Amitabh Bhattacharya',
130 'album': 'Tum Kya Mile (From "Rocky Aur Rani Kii Prem Kahaani")',
131 'release_year': 2023,
132 'thumbnail': 'https://images.hungama.com/c/1/7c2/c7b/103553671/103553671_200x200.jpg',
133 },
134 }]
8cb5c218
AG
135
136 def _real_extract(self, url):
2543938b 137 audio_id = self._match_id(url)
8cb5c218 138
06b4b90c 139 data = self._download_json(
2543938b
S
140 'https://www.hungama.com/audio-player-data/track/%s' % audio_id,
141 audio_id, query={'_country': 'IN'})[0]
06b4b90c
S
142 track = data['song_name']
143 artist = data.get('singer_name')
14183d1f
A
144 formats = []
145 media_json = self._download_json(data.get('file') or data['preview_link'], audio_id)
146 media_url = try_get(media_json, lambda x: x['response']['media_url'], str)
147 media_type = try_get(media_json, lambda x: x['response']['type'], str)
148
149 if media_url:
150 formats.append({
151 'url': media_url,
152 'ext': media_type,
153 'vcodec': 'none',
154 'acodec': media_type,
155 })
06b4b90c
S
156
157 title = '%s - %s' % (artist, track) if artist else track
158 thumbnail = data.get('img_src') or data.get('album_image')
8cb5c218
AG
159
160 return {
2543938b 161 'id': audio_id,
8cb5c218 162 'title': title,
06b4b90c
S
163 'thumbnail': thumbnail,
164 'track': track,
165 'artist': artist,
14183d1f 166 'album': data.get('album_name') or None,
06b4b90c
S
167 'release_year': int_or_none(data.get('date')),
168 'formats': formats,
8cb5c218 169 }
14183d1f
A
170
171
4b3a6ef1
OA
172class HungamaAlbumPlaylistIE(HungamaBaseIE):
173 _VALID_URL = r'https?://(?:www\.|un\.)?hungama\.com/(?P<path>playlists|album)/[^/]+/(?P<id>\d+)'
14183d1f
A
174 _TESTS = [{
175 'url': 'https://www.hungama.com/album/bhuj-the-pride-of-india/69481490/',
176 'playlist_mincount': 7,
177 'info_dict': {
178 'id': '69481490',
179 },
180 }, {
181 'url': 'https://www.hungama.com/playlists/hindi-jan-to-june-2021/123063/',
4b3a6ef1 182 'playlist_mincount': 33,
14183d1f
A
183 'info_dict': {
184 'id': '123063',
185 },
4b3a6ef1
OA
186 }, {
187 'url': 'https://un.hungama.com/album/what-jhumka-%3F-from-rocky-aur-rani-kii-prem-kahaani/103891805/',
188 'playlist_mincount': 1,
189 'info_dict': {
190 'id': '103891805',
191 },
14183d1f
A
192 }]
193
194 def _real_extract(self, url):
4b3a6ef1
OA
195 playlist_id, path = self._match_valid_url(url).group('id', 'path')
196 data = self._call_api(remove_end(path, 's'), playlist_id, fatal=True)
197
198 def entries():
199 for song_url in traverse_obj(data, ('body', 'rows', ..., 'data', 'misc', 'share', {url_or_none})):
200 yield self.url_result(song_url, HungamaSongIE)
201
202 return self.playlist_result(entries(), playlist_id)