sevenplus.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import json
  2. import re
  3. from .brightcove import BrightcoveNewBaseIE
  4. from ..compat import (
  5. compat_HTTPError,
  6. compat_str,
  7. )
  8. from ..utils import (
  9. ExtractorError,
  10. try_get,
  11. update_url_query,
  12. )
  13. class SevenPlusIE(BrightcoveNewBaseIE):
  14. IE_NAME = '7plus'
  15. _VALID_URL = r'https?://(?:www\.)?7plus\.com\.au/(?P<path>[^?]+\?.*?\bepisode-id=(?P<id>[^&#]+))'
  16. _TESTS = [{
  17. 'url': 'https://7plus.com.au/MTYS?episode-id=MTYS7-003',
  18. 'info_dict': {
  19. 'id': 'MTYS7-003',
  20. 'ext': 'mp4',
  21. 'title': 'S7 E3 - Wind Surf',
  22. 'description': 'md5:29c6a69f21accda7601278f81b46483d',
  23. 'uploader_id': '5303576322001',
  24. 'upload_date': '20171201',
  25. 'timestamp': 1512106377,
  26. 'series': 'Mighty Ships',
  27. 'season_number': 7,
  28. 'episode_number': 3,
  29. 'episode': 'Wind Surf',
  30. },
  31. 'params': {
  32. 'skip_download': True,
  33. }
  34. }, {
  35. 'url': 'https://7plus.com.au/UUUU?episode-id=AUMS43-001',
  36. 'only_matching': True,
  37. }]
  38. def _real_initialize(self):
  39. self.token = None
  40. cookies = self._get_cookies('https://7plus.com.au')
  41. api_key = next((x for x in cookies if x.startswith('glt_')), '')[4:]
  42. if not api_key: # Cookies are signed out, skip login
  43. return
  44. login_resp = self._download_json(
  45. 'https://login.7plus.com.au/accounts.getJWT', None, 'Logging in', fatal=False,
  46. query={
  47. 'APIKey': api_key,
  48. 'sdk': 'js_latest',
  49. 'login_token': cookies[f'glt_{api_key}'].value,
  50. 'authMode': 'cookie',
  51. 'pageURL': 'https://7plus.com.au/',
  52. 'sdkBuild': '12471',
  53. 'format': 'json',
  54. }) or {}
  55. if 'errorMessage' in login_resp:
  56. self.report_warning(f'Unable to login: 7plus said: {login_resp["errorMessage"]}')
  57. return
  58. id_token = login_resp.get('id_token')
  59. if not id_token:
  60. self.report_warning('Unable to login: Could not extract id token')
  61. return
  62. token_resp = self._download_json(
  63. 'https://7plus.com.au/auth/token', None, 'Getting auth token', fatal=False,
  64. headers={'Content-Type': 'application/json'}, data=json.dumps({
  65. 'idToken': id_token,
  66. 'platformId': 'web',
  67. 'regSource': '7plus',
  68. }).encode('utf-8')) or {}
  69. self.token = token_resp.get('token')
  70. if not self.token:
  71. self.report_warning('Unable to log in: Could not extract auth token')
  72. def _real_extract(self, url):
  73. path, episode_id = self._match_valid_url(url).groups()
  74. headers = {}
  75. if self.token:
  76. headers['Authorization'] = f'Bearer {self.token}'
  77. try:
  78. media = self._download_json(
  79. 'https://videoservice.swm.digital/playback', episode_id, query={
  80. 'appId': '7plus',
  81. 'deviceType': 'web',
  82. 'platformType': 'web',
  83. 'accountId': 5303576322001,
  84. 'referenceId': 'ref:' + episode_id,
  85. 'deliveryId': 'csai',
  86. 'videoType': 'vod',
  87. }, headers=headers)['media']
  88. except ExtractorError as e:
  89. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
  90. raise ExtractorError(self._parse_json(
  91. e.cause.read().decode(), episode_id)[0]['error_code'], expected=True)
  92. raise
  93. for source in media.get('sources', {}):
  94. src = source.get('src')
  95. if not src:
  96. continue
  97. source['src'] = update_url_query(src, {'rule': ''})
  98. info = self._parse_brightcove_metadata(media, episode_id)
  99. content = self._download_json(
  100. 'https://component-cdn.swm.digital/content/' + path,
  101. episode_id, headers={
  102. 'market-id': 4,
  103. }, fatal=False) or {}
  104. for item in content.get('items', {}):
  105. if item.get('componentData', {}).get('componentType') == 'infoPanel':
  106. for src_key, dst_key in [('title', 'title'), ('shortSynopsis', 'description')]:
  107. value = item.get(src_key)
  108. if value:
  109. info[dst_key] = value
  110. info['series'] = try_get(
  111. item, lambda x: x['seriesLogo']['name'], compat_str)
  112. mobj = re.search(r'^S(\d+)\s+E(\d+)\s+-\s+(.+)$', info['title'])
  113. if mobj:
  114. info.update({
  115. 'season_number': int(mobj.group(1)),
  116. 'episode_number': int(mobj.group(2)),
  117. 'episode': mobj.group(3),
  118. })
  119. return info