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.

190 lines
6.9 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import datetime
  4. import json
  5. import hashlib
  6. import hmac
  7. import re
  8. from .common import InfoExtractor
  9. from .anvato import AnvatoIE
  10. from ..utils import (
  11. smuggle_url,
  12. urlencode_postdata,
  13. xpath_text,
  14. )
  15. class ScrippsNetworksWatchIE(InfoExtractor):
  16. IE_NAME = 'scrippsnetworks:watch'
  17. _VALID_URL = r'''(?x)
  18. https?://
  19. watch\.
  20. (?P<site>hgtv|foodnetwork|travelchannel|diynetwork|cookingchanneltv|geniuskitchen)\.com/
  21. (?:
  22. player\.[A-Z0-9]+\.html\#|
  23. show/(?:[^/]+/){2}|
  24. player/
  25. )
  26. (?P<id>\d+)
  27. '''
  28. _TESTS = [{
  29. 'url': 'http://watch.hgtv.com/show/HGTVE/Best-Ever-Treehouses/2241515/Best-Ever-Treehouses/',
  30. 'md5': '26545fd676d939954c6808274bdb905a',
  31. 'info_dict': {
  32. 'id': '4173834',
  33. 'ext': 'mp4',
  34. 'title': 'Best Ever Treehouses',
  35. 'description': "We're searching for the most over the top treehouses.",
  36. 'uploader': 'ANV',
  37. 'upload_date': '20170922',
  38. 'timestamp': 1506056400,
  39. },
  40. 'params': {
  41. 'skip_download': True,
  42. },
  43. 'add_ie': [AnvatoIE.ie_key()],
  44. }, {
  45. 'url': 'http://watch.diynetwork.com/show/DSAL/Salvage-Dawgs/2656646/Covington-Church/',
  46. 'only_matching': True,
  47. }, {
  48. 'url': 'http://watch.diynetwork.com/player.HNT.html#2656646',
  49. 'only_matching': True,
  50. }, {
  51. 'url': 'http://watch.geniuskitchen.com/player/3787617/Ample-Hills-Ice-Cream-Bike/',
  52. 'only_matching': True,
  53. }]
  54. _SNI_TABLE = {
  55. 'hgtv': 'hgtv',
  56. 'diynetwork': 'diy',
  57. 'foodnetwork': 'food',
  58. 'cookingchanneltv': 'cook',
  59. 'travelchannel': 'trav',
  60. 'geniuskitchen': 'genius',
  61. }
  62. _SNI_HOST = 'web.api.video.snidigital.com'
  63. _AWS_REGION = 'us-east-1'
  64. _AWS_IDENTITY_ID_JSON = json.dumps({
  65. 'IdentityId': '%s:7655847c-0ae7-4d9b-80d6-56c062927eb3' % _AWS_REGION
  66. })
  67. _AWS_USER_AGENT = 'aws-sdk-js/2.80.0 callback'
  68. _AWS_API_KEY = 'E7wSQmq0qK6xPrF13WmzKiHo4BQ7tip4pQcSXVl1'
  69. _AWS_SERVICE = 'execute-api'
  70. _AWS_REQUEST = 'aws4_request'
  71. _AWS_SIGNED_HEADERS = ';'.join([
  72. 'host', 'x-amz-date', 'x-amz-security-token', 'x-api-key'])
  73. _AWS_CANONICAL_REQUEST_TEMPLATE = '''GET
  74. %(uri)s
  75. host:%(host)s
  76. x-amz-date:%(date)s
  77. x-amz-security-token:%(token)s
  78. x-api-key:%(key)s
  79. %(signed_headers)s
  80. %(payload_hash)s'''
  81. def _real_extract(self, url):
  82. mobj = re.match(self._VALID_URL, url)
  83. site_id, video_id = mobj.group('site', 'id')
  84. def aws_hash(s):
  85. return hashlib.sha256(s.encode('utf-8')).hexdigest()
  86. token = self._download_json(
  87. 'https://cognito-identity.us-east-1.amazonaws.com/', video_id,
  88. data=self._AWS_IDENTITY_ID_JSON.encode('utf-8'),
  89. headers={
  90. 'Accept': '*/*',
  91. 'Content-Type': 'application/x-amz-json-1.1',
  92. 'Referer': url,
  93. 'X-Amz-Content-Sha256': aws_hash(self._AWS_IDENTITY_ID_JSON),
  94. 'X-Amz-Target': 'AWSCognitoIdentityService.GetOpenIdToken',
  95. 'X-Amz-User-Agent': self._AWS_USER_AGENT,
  96. })['Token']
  97. sts = self._download_xml(
  98. 'https://sts.amazonaws.com/', video_id, data=urlencode_postdata({
  99. 'Action': 'AssumeRoleWithWebIdentity',
  100. 'RoleArn': 'arn:aws:iam::710330595350:role/Cognito_WebAPIUnauth_Role',
  101. 'RoleSessionName': 'web-identity',
  102. 'Version': '2011-06-15',
  103. 'WebIdentityToken': token,
  104. }), headers={
  105. 'Referer': url,
  106. 'X-Amz-User-Agent': self._AWS_USER_AGENT,
  107. 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
  108. })
  109. def get(key):
  110. return xpath_text(
  111. sts, './/{https://sts.amazonaws.com/doc/2011-06-15/}%s' % key,
  112. fatal=True)
  113. access_key_id = get('AccessKeyId')
  114. secret_access_key = get('SecretAccessKey')
  115. session_token = get('SessionToken')
  116. # Task 1: http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
  117. uri = '/1/web/brands/%s/episodes/scrid/%s' % (self._SNI_TABLE[site_id], video_id)
  118. datetime_now = datetime.datetime.utcnow().strftime('%Y%m%dT%H%M%SZ')
  119. date = datetime_now[:8]
  120. canonical_string = self._AWS_CANONICAL_REQUEST_TEMPLATE % {
  121. 'uri': uri,
  122. 'host': self._SNI_HOST,
  123. 'date': datetime_now,
  124. 'token': session_token,
  125. 'key': self._AWS_API_KEY,
  126. 'signed_headers': self._AWS_SIGNED_HEADERS,
  127. 'payload_hash': aws_hash(''),
  128. }
  129. # Task 2: http://docs.aws.amazon.com/general/latest/gr/sigv4-create-string-to-sign.html
  130. credential_string = '/'.join([date, self._AWS_REGION, self._AWS_SERVICE, self._AWS_REQUEST])
  131. string_to_sign = '\n'.join([
  132. 'AWS4-HMAC-SHA256', datetime_now, credential_string,
  133. aws_hash(canonical_string)])
  134. # Task 3: http://docs.aws.amazon.com/general/latest/gr/sigv4-calculate-signature.html
  135. def aws_hmac(key, msg):
  136. return hmac.new(key, msg.encode('utf-8'), hashlib.sha256)
  137. def aws_hmac_digest(key, msg):
  138. return aws_hmac(key, msg).digest()
  139. def aws_hmac_hexdigest(key, msg):
  140. return aws_hmac(key, msg).hexdigest()
  141. k_secret = 'AWS4' + secret_access_key
  142. k_date = aws_hmac_digest(k_secret.encode('utf-8'), date)
  143. k_region = aws_hmac_digest(k_date, self._AWS_REGION)
  144. k_service = aws_hmac_digest(k_region, self._AWS_SERVICE)
  145. k_signing = aws_hmac_digest(k_service, self._AWS_REQUEST)
  146. signature = aws_hmac_hexdigest(k_signing, string_to_sign)
  147. auth_header = ', '.join([
  148. 'AWS4-HMAC-SHA256 Credential=%s' % '/'.join(
  149. [access_key_id, date, self._AWS_REGION, self._AWS_SERVICE, self._AWS_REQUEST]),
  150. 'SignedHeaders=%s' % self._AWS_SIGNED_HEADERS,
  151. 'Signature=%s' % signature,
  152. ])
  153. mcp_id = self._download_json(
  154. 'https://%s%s' % (self._SNI_HOST, uri), video_id, headers={
  155. 'Accept': '*/*',
  156. 'Referer': url,
  157. 'Authorization': auth_header,
  158. 'X-Amz-Date': datetime_now,
  159. 'X-Amz-Security-Token': session_token,
  160. 'X-Api-Key': self._AWS_API_KEY,
  161. })['results'][0]['mcpId']
  162. return self.url_result(
  163. smuggle_url(
  164. 'anvato:anvato_scripps_app_web_prod_0837996dbe373629133857ae9eb72e740424d80a:%s' % mcp_id,
  165. {'geo_countries': ['US']}),
  166. AnvatoIE.ie_key(), video_id=mcp_id)