hungama.py 7.5 KB

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