[go] add support for adobe pass auth(closes #11468)(closes #10831)
[youtube-dl] / youtube_dl / extractor / go.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .adobepass import AdobePassIE
7 from ..utils import (
8     int_or_none,
9     determine_ext,
10     parse_age_limit,
11     urlencode_postdata,
12     ExtractorError,
13 )
14
15
16 class GoIE(AdobePassIE):
17     _SITE_INFO = {
18         'abc': {
19             'brand': '001',
20             'requestor_id': 'ABC',
21         },
22         'freeform': {
23             'brand': '002',
24             'requestor_id': 'ABCFamily',
25         },
26         'watchdisneychannel': {
27             'brand': '004',
28             'requestor_id': 'Disney',
29         },
30         'watchdisneyjunior': {
31             'brand': '008',
32             'requestor_id': 'DisneyJunior',
33         },
34         'watchdisneyxd': {
35             'brand': '009',
36             'requestor_id': 'DisneyXD',
37         }
38     }
39     _VALID_URL = r'https?://(?:(?P<sub_domain>%s)\.)?go\.com/(?:[^/]+/)*(?:vdka(?P<id>\w+)|season-\d+/\d+-(?P<display_id>[^/?#]+))' % '|'.join(_SITE_INFO.keys())
40     _TESTS = [{
41         'url': 'http://abc.go.com/shows/castle/video/most-recent/vdka0_g86w5onx',
42         'info_dict': {
43             'id': '0_g86w5onx',
44             'ext': 'mp4',
45             'title': 'Sneak Peek: Language Arts',
46             'description': 'md5:7dcdab3b2d17e5217c953256af964e9c',
47         },
48         'params': {
49             # m3u8 download
50             'skip_download': True,
51         },
52     }, {
53         'url': 'http://abc.go.com/shows/after-paradise/video/most-recent/vdka3335601',
54         'only_matching': True,
55     }]
56
57     def _real_extract(self, url):
58         sub_domain, video_id, display_id = re.match(self._VALID_URL, url).groups()
59         if not video_id:
60             webpage = self._download_webpage(url, display_id)
61             video_id = self._search_regex(
62                 # There may be inner quotes, e.g. data-video-id="'VDKA3609139'"
63                 # from http://freeform.go.com/shows/shadowhunters/episodes/season-2/1-this-guilty-blood
64                 r'data-video-id=["\']*VDKA(\w+)', webpage, 'video id')
65         site_info = self._SITE_INFO[sub_domain]
66         brand = site_info['brand']
67         video_data = self._download_json(
68             'http://api.contents.watchabc.go.com/vp2/ws/contents/3000/videos/%s/001/-1/-1/-1/%s/-1/-1.json' % (brand, video_id),
69             video_id)['video'][0]
70         title = video_data['title']
71
72         formats = []
73         for asset in video_data.get('assets', {}).get('asset', []):
74             asset_url = asset.get('value')
75             if not asset_url:
76                 continue
77             format_id = asset.get('format')
78             ext = determine_ext(asset_url)
79             if ext == 'm3u8':
80                 video_type = video_data.get('type')
81                 if video_type == 'lf':
82                     data = {
83                         'video_id': video_data['id'],
84                         'video_type': video_type,
85                         'brand': brand,
86                         'device': '001',
87                     }
88                     if video_data.get('accesslevel') == '1':
89                         requestor_id = site_info['requestor_id']
90                         resource = self._get_mvpd_resource(
91                             requestor_id, title, video_id, None)
92                         auth = self._extract_mvpd_auth(
93                             url, video_id, requestor_id, resource)
94                         data.update({
95                             'token': auth,
96                             'token_type': 'ap',
97                             'adobe_requestor_id': requestor_id,
98                         })
99                     entitlement = self._download_json(
100                         'https://api.entitlement.watchabc.go.com/vp2/ws-secure/entitlement/2020/authorize.json',
101                         video_id, data=urlencode_postdata(data), headers=self.geo_verification_headers())
102                     errors = entitlement.get('errors', {}).get('errors', [])
103                     if errors:
104                         error_message = ', '.join([error['message'] for error in errors])
105                         raise ExtractorError('%s said: %s' % (self.IE_NAME, error_message), expected=True)
106                     asset_url += '?' + entitlement['uplynkData']['sessionKey']
107                 formats.extend(self._extract_m3u8_formats(
108                     asset_url, video_id, 'mp4', m3u8_id=format_id or 'hls', fatal=False))
109             else:
110                 formats.append({
111                     'format_id': format_id,
112                     'url': asset_url,
113                     'ext': ext,
114                 })
115         self._sort_formats(formats)
116
117         subtitles = {}
118         for cc in video_data.get('closedcaption', {}).get('src', []):
119             cc_url = cc.get('value')
120             if not cc_url:
121                 continue
122             ext = determine_ext(cc_url)
123             if ext == 'xml':
124                 ext = 'ttml'
125             subtitles.setdefault(cc.get('lang'), []).append({
126                 'url': cc_url,
127                 'ext': ext,
128             })
129
130         thumbnails = []
131         for thumbnail in video_data.get('thumbnails', {}).get('thumbnail', []):
132             thumbnail_url = thumbnail.get('value')
133             if not thumbnail_url:
134                 continue
135             thumbnails.append({
136                 'url': thumbnail_url,
137                 'width': int_or_none(thumbnail.get('width')),
138                 'height': int_or_none(thumbnail.get('height')),
139             })
140
141         return {
142             'id': video_id,
143             'title': title,
144             'description': video_data.get('longdescription') or video_data.get('description'),
145             'duration': int_or_none(video_data.get('duration', {}).get('value'), 1000),
146             'age_limit': parse_age_limit(video_data.get('tvrating', {}).get('rating')),
147             'episode_number': int_or_none(video_data.get('episodenumber')),
148             'series': video_data.get('show', {}).get('title'),
149             'season_number': int_or_none(video_data.get('season', {}).get('num')),
150             'thumbnails': thumbnails,
151             'formats': formats,
152             'subtitles': subtitles,
153         }