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.

154 lines
5.4 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. 'url': 'https://www.hotstar.com/can-you-not-spread-rumours/1000076273',
  40. 'info_dict': {
  41. 'id': '1000076273',
  42. 'ext': 'mp4',
  43. 'title': 'Can You Not Spread Rumours?',
  44. 'description': 'md5:c957d8868e9bc793ccb813691cc4c434',
  45. 'timestamp': 1447248600,
  46. 'upload_date': '20151111',
  47. 'duration': 381,
  48. },
  49. 'params': {
  50. # m3u8 download
  51. 'skip_download': True,
  52. }
  53. }, {
  54. 'url': 'http://www.hotstar.com/sports/cricket/rajitha-sizzles-on-debut-with-329/2001477583',
  55. 'only_matching': True,
  56. }, {
  57. 'url': 'http://www.hotstar.com/1000000515',
  58. 'only_matching': True,
  59. }]
  60. _GEO_BYPASS = False
  61. def _real_extract(self, url):
  62. video_id = self._match_id(url)
  63. webpage = self._download_webpage(url, video_id)
  64. app_state = self._parse_json(self._search_regex(
  65. r'<script>window\.APP_STATE\s*=\s*({.+?})</script>',
  66. webpage, 'app state'), video_id)
  67. video_data = {}
  68. for v in app_state.values():
  69. content = try_get(v, lambda x: x['initialState']['contentData']['content'], dict)
  70. if content and content.get('contentId') == video_id:
  71. video_data = content
  72. title = video_data['title']
  73. if video_data.get('drmProtected'):
  74. raise ExtractorError('This video is DRM protected.', expected=True)
  75. formats = []
  76. format_data = self._call_api('h/v1/play', video_id)['item']
  77. format_url = format_data['playbackUrl']
  78. ext = determine_ext(format_url)
  79. if ext == 'm3u8':
  80. try:
  81. formats.extend(self._extract_m3u8_formats(
  82. format_url, video_id, 'mp4', m3u8_id='hls'))
  83. except ExtractorError as e:
  84. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
  85. self.raise_geo_restricted(countries=['IN'])
  86. raise
  87. elif ext == 'f4m':
  88. # produce broken files
  89. pass
  90. else:
  91. formats.append({
  92. 'url': format_url,
  93. 'width': int_or_none(format_data.get('width')),
  94. 'height': int_or_none(format_data.get('height')),
  95. })
  96. self._sort_formats(formats)
  97. return {
  98. 'id': video_id,
  99. 'title': title,
  100. 'description': video_data.get('description'),
  101. 'duration': int_or_none(video_data.get('duration')),
  102. 'timestamp': int_or_none(video_data.get('broadcastDate') or video_data.get('startDate')),
  103. 'formats': formats,
  104. 'channel': video_data.get('channelName'),
  105. 'channel_id': video_data.get('channelId'),
  106. 'series': video_data.get('showName'),
  107. 'season': video_data.get('seasonName'),
  108. 'season_number': int_or_none(video_data.get('seasonNo')),
  109. 'season_id': video_data.get('seasonId'),
  110. 'episode': title,
  111. 'episode_number': int_or_none(video_data.get('episodeNo')),
  112. }
  113. class HotStarPlaylistIE(HotStarBaseIE):
  114. IE_NAME = 'hotstar:playlist'
  115. _VALID_URL = r'https?://(?:www\.)?hotstar\.com/tv/[^/]+/s-\w+/list/[^/]+/t-(?P<id>\w+)'
  116. _TESTS = [{
  117. 'url': 'https://www.hotstar.com/tv/savdhaan-india/s-26/list/popular-clips/t-3_2_26',
  118. 'info_dict': {
  119. 'id': '3_2_26',
  120. },
  121. 'playlist_mincount': 20,
  122. }, {
  123. 'url': 'https://www.hotstar.com/tv/savdhaan-india/s-26/list/extras/t-2480',
  124. 'only_matching': True,
  125. }]
  126. def _real_extract(self, url):
  127. playlist_id = self._match_id(url)
  128. collection = self._call_api('o/v1/tray/find', playlist_id, 'uqId')
  129. entries = [
  130. self.url_result(
  131. 'https://www.hotstar.com/%s' % video['contentId'],
  132. ie=HotStarIE.ie_key(), video_id=video['contentId'])
  133. for video in collection['assets']['items']
  134. if video.get('contentId')]
  135. return self.playlist_result(entries, playlist_id)