2 from __future__
import unicode_literals
8 from .common
import InfoExtractor
9 from ..compat
import compat_urllib_parse_urlencode
12 class AWSIE(InfoExtractor
):
13 _AWS_ALGORITHM
= 'AWS4-HMAC-SHA256'
14 _AWS_REGION
= 'us-east-1'
16 def _aws_execute_api(self
, aws_dict
, video_id
, query
=None):
18 amz_date
= datetime
.datetime
.utcnow().strftime('%Y%m%dT%H%M%SZ')
21 'Accept': 'application/json',
22 'Host': self
._AWS
_PROXY
_HOST
,
23 'X-Amz-Date': amz_date
,
24 'X-Api-Key': self
._AWS
_API
_KEY
26 session_token
= aws_dict
.get('session_token')
28 headers
['X-Amz-Security-Token'] = session_token
31 return hashlib
.sha256(s
.encode('utf-8')).hexdigest()
33 # Task 1: http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
34 canonical_querystring
= compat_urllib_parse_urlencode(query
)
35 canonical_headers
= ''
36 for header_name
, header_value
in sorted(headers
.items()):
37 canonical_headers
+= '%s:%s\n' % (header_name
.lower(), header_value
)
38 signed_headers
= ';'.join([header
.lower() for header
in sorted(headers
.keys())])
39 canonical_request
= '\n'.join([
42 canonical_querystring
,
48 # Task 2: http://docs.aws.amazon.com/general/latest/gr/sigv4-create-string-to-sign.html
49 credential_scope_list
= [date
, self
._AWS
_REGION
, 'execute-api', 'aws4_request']
50 credential_scope
= '/'.join(credential_scope_list
)
51 string_to_sign
= '\n'.join([self
._AWS
_ALGORITHM
, amz_date
, credential_scope
, aws_hash(canonical_request
)])
53 # Task 3: http://docs.aws.amazon.com/general/latest/gr/sigv4-calculate-signature.html
54 def aws_hmac(key
, msg
):
55 return hmac
.new(key
, msg
.encode('utf-8'), hashlib
.sha256
)
57 def aws_hmac_digest(key
, msg
):
58 return aws_hmac(key
, msg
).digest()
60 def aws_hmac_hexdigest(key
, msg
):
61 return aws_hmac(key
, msg
).hexdigest()
63 k_signing
= ('AWS4' + aws_dict
['secret_key']).encode('utf-8')
64 for value
in credential_scope_list
:
65 k_signing
= aws_hmac_digest(k_signing
, value
)
67 signature
= aws_hmac_hexdigest(k_signing
, string_to_sign
)
69 # Task 4: http://docs.aws.amazon.com/general/latest/gr/sigv4-add-signature-to-request.html
70 headers
['Authorization'] = ', '.join([
71 '%s Credential=%s/%s' % (self
._AWS
_ALGORITHM
, aws_dict
['access_key'], credential_scope
),
72 'SignedHeaders=%s' % signed_headers
,
73 'Signature=%s' % signature
,
76 return self
._download
_json
(
77 'https://%s%s%s' % (self
._AWS
_PROXY
_HOST
, aws_dict
['uri'], '?' + canonical_querystring
if canonical_querystring
else ''),
78 video_id
, headers
=headers
)