[go] Relax _VALID_URL (closes #12341)
[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+)|(?:[^/]+/)*(?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         'url': 'http://abc.go.com/shows/the-catch/episode-guide/season-01/10-the-wedding',
57         'only_matching': True,
58     }, {
59         'url': 'http://abc.go.com/shows/world-news-tonight/episode-guide/2017-02/17-021717-intense-stand-off-between-man-with-rifle-and-police-in-oakland',
60         'only_matching': True,
61     }]
62
63     def _real_extract(self, url):
64         sub_domain, video_id, display_id = re.match(self._VALID_URL, url).groups()
65         if not video_id:
66             webpage = self._download_webpage(url, display_id)
67             video_id = self._search_regex(
68                 # There may be inner quotes, e.g. data-video-id="'VDKA3609139'"
69                 # from http://freeform.go.com/shows/shadowhunters/episodes/season-2/1-this-guilty-blood
70                 r'data-video-id=["\']*VDKA(\w+)', webpage, 'video id')
71         site_info = self._SITE_INFO[sub_domain]
72         brand = site_info['brand']
73         video_data = self._download_json(
74             'http://api.contents.watchabc.go.com/vp2/ws/contents/3000/videos/%s/001/-1/-1/-1/%s/-1/-1.json' % (brand, video_id),
75             video_id)['video'][0]
76         title = video_data['title']
77
78         formats = []
79         for asset in video_data.get('assets', {}).get('asset', []):
80             asset_url = asset.get('value')
81             if not asset_url:
82                 continue
83             format_id = asset.get('format')
84             ext = determine_ext(asset_url)
85             if ext == 'm3u8':
86                 video_type = video_data.get('type')
87                 data = {
88                     'video_id': video_data['id'],
89                     'video_type': video_type,
90                     'brand': brand,
91                     'device': '001',
92                 }
93                 if video_data.get('accesslevel') == '1':
94                     requestor_id = site_info['requestor_id']
95                     resource = self._get_mvpd_resource(
96                         requestor_id, title, video_id, None)
97                     auth = self._extract_mvpd_auth(
98                         url, video_id, requestor_id, resource)
99                     data.update({
100                         'token': auth,
101                         'token_type': 'ap',
102                         'adobe_requestor_id': requestor_id,
103                     })
104                 else:
105                     self._initialize_geo_bypass(['US'])
106                 entitlement = self._download_json(
107                     'https://api.entitlement.watchabc.go.com/vp2/ws-secure/entitlement/2020/authorize.json',
108                     video_id, data=urlencode_postdata(data), headers=self.geo_verification_headers())
109                 errors = entitlement.get('errors', {}).get('errors', [])
110                 if errors:
111                     for error in errors:
112                         if error.get('code') == 1002:
113                             self.raise_geo_restricted(
114                                 error['message'], countries=['US'])
115                     error_message = ', '.join([error['message'] for error in errors])
116                     raise ExtractorError('%s said: %s' % (self.IE_NAME, error_message), expected=True)
117                 asset_url += '?' + entitlement['uplynkData']['sessionKey']
118                 formats.extend(self._extract_m3u8_formats(
119                     asset_url, video_id, 'mp4', m3u8_id=format_id or 'hls', fatal=False))
120             else:
121                 f = {
122                     'format_id': format_id,
123                     'url': asset_url,
124                     'ext': ext,
125                 }
126                 if re.search(r'(?:/mp4/source/|_source\.mp4)', asset_url):
127                     f.update({
128                         'format_id': ('%s-' % format_id if format_id else '') + 'SOURCE',
129                         'preference': 1,
130                     })
131                 else:
132                     mobj = re.search(r'/(\d+)x(\d+)/', asset_url)
133                     if mobj:
134                         height = int(mobj.group(2))
135                         f.update({
136                             'format_id': ('%s-' % format_id if format_id else '') + '%dP' % height,
137                             'width': int(mobj.group(1)),
138                             'height': height,
139                         })
140                 formats.append(f)
141         self._sort_formats(formats)
142
143         subtitles = {}
144         for cc in video_data.get('closedcaption', {}).get('src', []):
145             cc_url = cc.get('value')
146             if not cc_url:
147                 continue
148             ext = determine_ext(cc_url)
149             if ext == 'xml':
150                 ext = 'ttml'
151             subtitles.setdefault(cc.get('lang'), []).append({
152                 'url': cc_url,
153                 'ext': ext,
154             })
155
156         thumbnails = []
157         for thumbnail in video_data.get('thumbnails', {}).get('thumbnail', []):
158             thumbnail_url = thumbnail.get('value')
159             if not thumbnail_url:
160                 continue
161             thumbnails.append({
162                 'url': thumbnail_url,
163                 'width': int_or_none(thumbnail.get('width')),
164                 'height': int_or_none(thumbnail.get('height')),
165             })
166
167         return {
168             'id': video_id,
169             'title': title,
170             'description': video_data.get('longdescription') or video_data.get('description'),
171             'duration': int_or_none(video_data.get('duration', {}).get('value'), 1000),
172             'age_limit': parse_age_limit(video_data.get('tvrating', {}).get('rating')),
173             'episode_number': int_or_none(video_data.get('episodenumber')),
174             'series': video_data.get('show', {}).get('title'),
175             'season_number': int_or_none(video_data.get('season', {}).get('num')),
176             'thumbnails': thumbnails,
177             'formats': formats,
178             'subtitles': subtitles,
179         }