hotstar.py 5.2 KB

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