[ie/facebook] Support more groups URLs (#11576)
[yt-dlp3.git] / yt_dlp / extractor / hungama.py
bloba687b12dc59e1ab81132331891c54798badcd1f7
1 from .common import InfoExtractor
2 from ..utils import (
3 int_or_none,
4 remove_end,
5 traverse_obj,
6 try_get,
7 unified_timestamp,
8 url_or_none,
9 urlencode_postdata,
13 class 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 {}
24 class HungamaIE(HungamaBaseIE):
25 _VALID_URL = r'''(?x)
26 https?://
27 (?:www\.|un\.)?hungama\.com/
28 (?:
29 (?:video|movie|short-film)/[^/]+/|
30 tv-show/(?:[^/]+/){2}\d+/episode/[^/]+/
32 (?P<id>\d+)
33 '''
34 _TESTS = [{
35 'url': 'http://www.hungama.com/video/krishna-chants/39349649/',
36 'md5': '687c5f1e9f832f3b59f44ed0eb1f120a',
37 'info_dict': {
38 'id': '39349649',
39 'ext': 'mp4',
40 'title': 'Krishna Chants',
41 'description': ' ',
42 'upload_date': '20180829',
43 'duration': 264,
44 'timestamp': 1535500800,
45 'view_count': int,
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',
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',
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,
71 def _real_extract(self, url):
72 video_id = self._match_id(url)
73 video_json = self._download_json(
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',
82 formats = self._extract_m3u8_formats(video_json['stream_url'], video_id, ext='mp4', m3u8_id='hls')
83 metadata = self._call_api('movie', video_id)
85 return {
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 })),
95 'id': video_id,
96 'formats': formats,
97 'subtitles': {
98 'en': [{
99 'url': video_json['sub_title'],
100 'ext': 'vtt',
102 } if video_json.get('sub_title') else None,
106 class HungamaSongIE(InfoExtractor):
107 _VALID_URL = r'https?://(?:www\.|un\.)?hungama\.com/song/[^/]+/(?P<id>\d+)'
108 _TESTS = [{
109 'url': 'https://www.hungama.com/song/kitni-haseen-zindagi/2931166/',
110 'md5': '964f46828e8b250aa35e5fdcfdcac367',
111 'info_dict': {
112 'id': '2931166',
113 'ext': 'mp3',
114 'title': 'Lucky Ali - Kitni Haseen Zindagi',
115 'track': 'Kitni Haseen Zindagi',
116 'artist': 'Lucky Ali',
117 'release_year': 2000,
118 'thumbnail': 'https://stat2.hungama.ind.in/assets/images/default_images/da-200x200.png',
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',
135 def _real_extract(self, url):
136 audio_id = self._match_id(url)
138 data = self._download_json(
139 f'https://www.hungama.com/audio-player-data/track/{audio_id}',
140 audio_id, query={'_country': 'IN'})[0]
141 track = data['song_name']
142 artist = data.get('singer_name')
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)
148 if media_url:
149 formats.append({
150 'url': media_url,
151 'ext': media_type,
152 'vcodec': 'none',
153 'acodec': media_type,
156 title = f'{artist} - {track}' if artist else track
157 thumbnail = data.get('img_src') or data.get('album_image')
159 return {
160 'id': audio_id,
161 'title': title,
162 'thumbnail': thumbnail,
163 'track': track,
164 'artist': artist,
165 'album': data.get('album_name') or None,
166 'release_year': int_or_none(data.get('date')),
167 'formats': formats,
171 class HungamaAlbumPlaylistIE(HungamaBaseIE):
172 _VALID_URL = r'https?://(?:www\.|un\.)?hungama\.com/(?P<path>playlists|album)/[^/]+/(?P<id>\d+)'
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',
179 }, {
180 'url': 'https://www.hungama.com/playlists/hindi-jan-to-june-2021/123063/',
181 'playlist_mincount': 33,
182 'info_dict': {
183 'id': '123063',
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',
193 def _real_extract(self, url):
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)
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)
201 return self.playlist_result(entries(), playlist_id)