You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
5.6 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import hashlib
  4. import hmac
  5. import time
  6. from .common import InfoExtractor
  7. from ..compat import compat_HTTPError
  8. from ..utils import (
  9. determine_ext,
  10. ExtractorError,
  11. int_or_none,
  12. try_get,
  13. )
  14. class HotStarBaseIE(InfoExtractor):
  15. _AKAMAI_ENCRYPTION_KEY = b'\x05\xfc\x1a\x01\xca\xc9\x4b\xc4\x12\xfc\x53\x12\x07\x75\xf9\xee'
  16. def _call_api(self, path, video_id, query_name='contentId'):
  17. st = int(time.time())
  18. exp = st + 6000
  19. auth = 'st=%d~exp=%d~acl=/*' % (st, exp)
  20. auth += '~hmac=' + hmac.new(self._AKAMAI_ENCRYPTION_KEY, auth.encode(), hashlib.sha256).hexdigest()
  21. response = self._download_json(
  22. 'https://api.hotstar.com/' + path,
  23. video_id, headers={
  24. 'hotstarauth': auth,
  25. 'x-country-code': 'IN',
  26. 'x-platform-code': 'JIO',
  27. }, query={
  28. query_name: video_id,
  29. 'tas': 10000,
  30. })
  31. if response['statusCode'] != 'OK':
  32. raise ExtractorError(
  33. response['body']['message'], expected=True)
  34. return response['body']['results']
  35. class HotStarIE(HotStarBaseIE):
  36. IE_NAME = 'hotstar'
  37. _VALID_URL = r'https?://(?:www\.)?hotstar\.com/(?:.+?[/-])?(?P<id>\d{10})'
  38. _TESTS = [{
  39. # contentData
  40. 'url': 'https://www.hotstar.com/can-you-not-spread-rumours/1000076273',
  41. 'info_dict': {
  42. 'id': '1000076273',
  43. 'ext': 'mp4',
  44. 'title': 'Can You Not Spread Rumours?',
  45. 'description': 'md5:c957d8868e9bc793ccb813691cc4c434',
  46. 'timestamp': 1447248600,
  47. 'upload_date': '20151111',
  48. 'duration': 381,
  49. },
  50. 'params': {
  51. # m3u8 download
  52. 'skip_download': True,
  53. }
  54. }, {
  55. # contentDetail
  56. 'url': 'https://www.hotstar.com/movies/radha-gopalam/1000057157',
  57. 'only_matching': True,
  58. }, {
  59. 'url': 'http://www.hotstar.com/sports/cricket/rajitha-sizzles-on-debut-with-329/2001477583',
  60. 'only_matching': True,
  61. }, {
  62. 'url': 'http://www.hotstar.com/1000000515',
  63. 'only_matching': True,
  64. }]
  65. _GEO_BYPASS = False
  66. def _real_extract(self, url):
  67. video_id = self._match_id(url)
  68. webpage = self._download_webpage(url, video_id)
  69. app_state = self._parse_json(self._search_regex(
  70. r'<script>window\.APP_STATE\s*=\s*({.+?})</script>',
  71. webpage, 'app state'), video_id)
  72. video_data = {}
  73. getters = list(
  74. lambda x, k=k: x['initialState']['content%s' % k]['content']
  75. for k in ('Data', 'Detail')
  76. )
  77. for v in app_state.values():
  78. content = try_get(v, getters, dict)
  79. if content and content.get('contentId') == video_id:
  80. video_data = content
  81. break
  82. title = video_data['title']
  83. if video_data.get('drmProtected'):
  84. raise ExtractorError('This video is DRM protected.', expected=True)
  85. formats = []
  86. format_data = self._call_api('h/v1/play', video_id)['item']
  87. format_url = format_data['playbackUrl']
  88. ext = determine_ext(format_url)
  89. if ext == 'm3u8':
  90. try:
  91. formats.extend(self._extract_m3u8_formats(
  92. format_url, video_id, 'mp4', m3u8_id='hls'))
  93. except ExtractorError as e:
  94. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
  95. self.raise_geo_restricted(countries=['IN'])
  96. raise
  97. elif ext == 'f4m':
  98. # produce broken files
  99. pass
  100. else:
  101. formats.append({
  102. 'url': format_url,
  103. 'width': int_or_none(format_data.get('width')),
  104. 'height': int_or_none(format_data.get('height')),
  105. })
  106. self._sort_formats(formats)
  107. return {
  108. 'id': video_id,
  109. 'title': title,
  110. 'description': video_data.get('description'),
  111. 'duration': int_or_none(video_data.get('duration')),
  112. 'timestamp': int_or_none(video_data.get('broadcastDate') or video_data.get('startDate')),
  113. 'formats': formats,
  114. 'channel': video_data.get('channelName'),
  115. 'channel_id': video_data.get('channelId'),
  116. 'series': video_data.get('showName'),
  117. 'season': video_data.get('seasonName'),
  118. 'season_number': int_or_none(video_data.get('seasonNo')),
  119. 'season_id': video_data.get('seasonId'),
  120. 'episode': title,
  121. 'episode_number': int_or_none(video_data.get('episodeNo')),
  122. }
  123. class HotStarPlaylistIE(HotStarBaseIE):
  124. IE_NAME = 'hotstar:playlist'
  125. _VALID_URL = r'https?://(?:www\.)?hotstar\.com/tv/[^/]+/s-\w+/list/[^/]+/t-(?P<id>\w+)'
  126. _TESTS = [{
  127. 'url': 'https://www.hotstar.com/tv/savdhaan-india/s-26/list/popular-clips/t-3_2_26',
  128. 'info_dict': {
  129. 'id': '3_2_26',
  130. },
  131. 'playlist_mincount': 20,
  132. }, {
  133. 'url': 'https://www.hotstar.com/tv/savdhaan-india/s-26/list/extras/t-2480',
  134. 'only_matching': True,
  135. }]
  136. def _real_extract(self, url):
  137. playlist_id = self._match_id(url)
  138. collection = self._call_api('o/v1/tray/find', playlist_id, 'uqId')
  139. entries = [
  140. self.url_result(
  141. 'https://www.hotstar.com/%s' % video['contentId'],
  142. ie=HotStarIE.ie_key(), video_id=video['contentId'])
  143. for video in collection['assets']['items']
  144. if video.get('contentId')]
  145. return self.playlist_result(entries, playlist_id)