Merge branch 'master' of github.com:rg3/youtube-dl into weibo
[ytdl] / youtube_dl / extractor / scrippsnetworks.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5 import hashlib
6 import re
7
8 from .aws import AWSIE
9 from .anvato import AnvatoIE
10 from ..utils import (
11     smuggle_url,
12     urlencode_postdata,
13     xpath_text,
14 )
15
16
17 class ScrippsNetworksWatchIE(AWSIE):
18     IE_NAME = 'scrippsnetworks:watch'
19     _VALID_URL = r'''(?x)
20                     https?://
21                         watch\.
22                         (?P<site>hgtv|foodnetwork|travelchannel|diynetwork|cookingchanneltv|geniuskitchen)\.com/
23                         (?:
24                             player\.[A-Z0-9]+\.html\#|
25                             show/(?:[^/]+/){2}|
26                             player/
27                         )
28                         (?P<id>\d+)
29                     '''
30     _TESTS = [{
31         'url': 'http://watch.hgtv.com/show/HGTVE/Best-Ever-Treehouses/2241515/Best-Ever-Treehouses/',
32         'md5': '26545fd676d939954c6808274bdb905a',
33         'info_dict': {
34             'id': '4173834',
35             'ext': 'mp4',
36             'title': 'Best Ever Treehouses',
37             'description': "We're searching for the most over the top treehouses.",
38             'uploader': 'ANV',
39             'upload_date': '20170922',
40             'timestamp': 1506056400,
41         },
42         'params': {
43             'skip_download': True,
44         },
45         'add_ie': [AnvatoIE.ie_key()],
46     }, {
47         'url': 'http://watch.diynetwork.com/show/DSAL/Salvage-Dawgs/2656646/Covington-Church/',
48         'only_matching': True,
49     }, {
50         'url': 'http://watch.diynetwork.com/player.HNT.html#2656646',
51         'only_matching': True,
52     }, {
53         'url': 'http://watch.geniuskitchen.com/player/3787617/Ample-Hills-Ice-Cream-Bike/',
54         'only_matching': True,
55     }]
56
57     _SNI_TABLE = {
58         'hgtv': 'hgtv',
59         'diynetwork': 'diy',
60         'foodnetwork': 'food',
61         'cookingchanneltv': 'cook',
62         'travelchannel': 'trav',
63         'geniuskitchen': 'genius',
64     }
65
66     _AWS_API_KEY = 'E7wSQmq0qK6xPrF13WmzKiHo4BQ7tip4pQcSXVl1'
67     _AWS_PROXY_HOST = 'web.api.video.snidigital.com'
68
69     _AWS_USER_AGENT = 'aws-sdk-js/2.80.0 callback'
70
71     def _real_extract(self, url):
72         mobj = re.match(self._VALID_URL, url)
73         site_id, video_id = mobj.group('site', 'id')
74
75         aws_identity_id_json = json.dumps({
76             'IdentityId': '%s:7655847c-0ae7-4d9b-80d6-56c062927eb3' % self._AWS_REGION
77         }).encode('utf-8')
78         token = self._download_json(
79             'https://cognito-identity.%s.amazonaws.com/' % self._AWS_REGION, video_id,
80             data=aws_identity_id_json,
81             headers={
82                 'Accept': '*/*',
83                 'Content-Type': 'application/x-amz-json-1.1',
84                 'Referer': url,
85                 'X-Amz-Content-Sha256': hashlib.sha256(aws_identity_id_json).hexdigest(),
86                 'X-Amz-Target': 'AWSCognitoIdentityService.GetOpenIdToken',
87                 'X-Amz-User-Agent': self._AWS_USER_AGENT,
88             })['Token']
89
90         sts = self._download_xml(
91             'https://sts.amazonaws.com/', video_id, data=urlencode_postdata({
92                 'Action': 'AssumeRoleWithWebIdentity',
93                 'RoleArn': 'arn:aws:iam::710330595350:role/Cognito_WebAPIUnauth_Role',
94                 'RoleSessionName': 'web-identity',
95                 'Version': '2011-06-15',
96                 'WebIdentityToken': token,
97             }), headers={
98                 'Referer': url,
99                 'X-Amz-User-Agent': self._AWS_USER_AGENT,
100                 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
101             })
102
103         def get(key):
104             return xpath_text(
105                 sts, './/{https://sts.amazonaws.com/doc/2011-06-15/}%s' % key,
106                 fatal=True)
107
108         mcp_id = self._aws_execute_api({
109             'uri': '/1/web/brands/%s/episodes/scrid/%s' % (self._SNI_TABLE[site_id], video_id),
110             'access_key': get('AccessKeyId'),
111             'secret_key': get('SecretAccessKey'),
112             'session_token': get('SessionToken'),
113         }, video_id)['results'][0]['mcpId']
114
115         return self.url_result(
116             smuggle_url(
117                 'anvato:anvato_scripps_app_web_prod_0837996dbe373629133857ae9eb72e740424d80a:%s' % mcp_id,
118                 {'geo_countries': ['US']}),
119             AnvatoIE.ie_key(), video_id=mcp_id)