]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/go.py
[extractor/common] Relax interaction count extraction in _json_ld
[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             'resource_id': 'Disney',
29         },
30         'watchdisneyjunior': {
31             'brand': '008',
32             'resource_id': 'DisneyJunior',
33         },
34         'watchdisneyxd': {
35             'brand': '009',
36             'resource_id': 'DisneyXD',
37         },
38         'disneynow': {
39             'brand': '011',
40             'resource_id': 'Disney',
41         }
42     }
43     _VALID_URL = r'''(?x)
44                     https?://
45                         (?:
46                             (?:(?P<sub_domain>%s)\.)?go|
47                             (?P<sub_domain_2>abc|freeform|disneynow)
48                         )\.com/
49                         (?:
50                             (?:[^/]+/)*(?P<id>[Vv][Dd][Kk][Aa]\w+)|
51                             (?:[^/]+/)*(?P<display_id>[^/?\#]+)
52                         )
53                     ''' % '|'.join(list(_SITE_INFO.keys()))
54     _TESTS = [{
55         'url': 'http://abc.go.com/shows/designated-survivor/video/most-recent/VDKA3807643',
56         'info_dict': {
57             'id': 'VDKA3807643',
58             'ext': 'mp4',
59             'title': 'The Traitor in the White House',
60             'description': 'md5:05b009d2d145a1e85d25111bd37222e8',
61         },
62         'params': {
63             # m3u8 download
64             'skip_download': True,
65         },
66         'skip': 'This content is no longer available.',
67     }, {
68         'url': 'http://watchdisneyxd.go.com/doraemon',
69         'info_dict': {
70             'title': 'Doraemon',
71             'id': 'SH55574025',
72         },
73         'playlist_mincount': 51,
74     }, {
75         'url': 'http://freeform.go.com/shows/shadowhunters/episodes/season-2/1-this-guilty-blood',
76         'info_dict': {
77             'id': 'VDKA3609139',
78             'ext': 'mp4',
79             'title': 'This Guilty Blood',
80             'description': 'md5:f18e79ad1c613798d95fdabfe96cd292',
81             'age_limit': 14,
82         },
83         'params': {
84             'geo_bypass_ip_block': '3.244.239.0/24',
85             # m3u8 download
86             'skip_download': True,
87         },
88     }, {
89         'url': 'https://abc.com/shows/the-rookie/episode-guide/season-02/03-the-bet',
90         'info_dict': {
91             'id': 'VDKA13435179',
92             'ext': 'mp4',
93             'title': 'The Bet',
94             'description': 'md5:c66de8ba2e92c6c5c113c3ade84ab404',
95             'age_limit': 14,
96         },
97         'params': {
98             'geo_bypass_ip_block': '3.244.239.0/24',
99             # m3u8 download
100             'skip_download': True,
101         },
102     }, {
103         'url': 'http://abc.go.com/shows/the-catch/episode-guide/season-01/10-the-wedding',
104         'only_matching': True,
105     }, {
106         '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',
107         'only_matching': True,
108     }, {
109         # brand 004
110         'url': 'http://disneynow.go.com/shows/big-hero-6-the-series/season-01/episode-10-mr-sparkles-loses-his-sparkle/vdka4637915',
111         'only_matching': True,
112     }, {
113         # brand 008
114         'url': 'http://disneynow.go.com/shows/minnies-bow-toons/video/happy-campers/vdka4872013',
115         'only_matching': True,
116     }, {
117         'url': 'https://disneynow.com/shows/minnies-bow-toons/video/happy-campers/vdka4872013',
118         'only_matching': True,
119     }]
120
121     def _extract_videos(self, brand, video_id='-1', show_id='-1'):
122         display_id = video_id if video_id != '-1' else show_id
123         return self._download_json(
124             'http://api.contents.watchabc.go.com/vp2/ws/contents/3000/videos/%s/001/-1/%s/-1/%s/-1/-1.json' % (brand, show_id, video_id),
125             display_id)['video']
126
127     def _real_extract(self, url):
128         mobj = re.match(self._VALID_URL, url)
129         sub_domain = mobj.group('sub_domain') or mobj.group('sub_domain_2')
130         video_id, display_id = mobj.group('id', 'display_id')
131         site_info = self._SITE_INFO.get(sub_domain, {})
132         brand = site_info.get('brand')
133         if not video_id or not site_info:
134             webpage = self._download_webpage(url, display_id or video_id)
135             video_id = self._search_regex(
136                 (
137                     # There may be inner quotes, e.g. data-video-id="'VDKA3609139'"
138                     # from http://freeform.go.com/shows/shadowhunters/episodes/season-2/1-this-guilty-blood
139                     r'data-video-id=["\']*(VDKA\w+)',
140                     # https://abc.com/shows/the-rookie/episode-guide/season-02/03-the-bet
141                     r'\b(?:video)?id["\']\s*:\s*["\'](VDKA\w+)'
142                 ), webpage, 'video id', default=video_id)
143             if not site_info:
144                 brand = self._search_regex(
145                     (r'data-brand=\s*["\']\s*(\d+)',
146                      r'data-page-brand=\s*["\']\s*(\d+)'), webpage, 'brand',
147                     default='004')
148                 site_info = next(
149                     si for _, si in self._SITE_INFO.items()
150                     if si.get('brand') == brand)
151             if not video_id:
152                 # show extraction works for Disney, DisneyJunior and DisneyXD
153                 # ABC and Freeform has different layout
154                 show_id = self._search_regex(r'data-show-id=["\']*(SH\d+)', webpage, 'show id')
155                 videos = self._extract_videos(brand, show_id=show_id)
156                 show_title = self._search_regex(r'data-show-title="([^"]+)"', webpage, 'show title', fatal=False)
157                 entries = []
158                 for video in videos:
159                     entries.append(self.url_result(
160                         video['url'], 'Go', video.get('id'), video.get('title')))
161                 entries.reverse()
162                 return self.playlist_result(entries, show_id, show_title)
163         video_data = self._extract_videos(brand, video_id)[0]
164         video_id = video_data['id']
165         title = video_data['title']
166
167         formats = []
168         for asset in video_data.get('assets', {}).get('asset', []):
169             asset_url = asset.get('value')
170             if not asset_url:
171                 continue
172             format_id = asset.get('format')
173             ext = determine_ext(asset_url)
174             if ext == 'm3u8':
175                 video_type = video_data.get('type')
176                 data = {
177                     'video_id': video_data['id'],
178                     'video_type': video_type,
179                     'brand': brand,
180                     'device': '001',
181                 }
182                 if video_data.get('accesslevel') == '1':
183                     requestor_id = site_info.get('requestor_id', 'DisneyChannels')
184                     resource = site_info.get('resource_id') or self._get_mvpd_resource(
185                         requestor_id, title, video_id, None)
186                     auth = self._extract_mvpd_auth(
187                         url, video_id, requestor_id, resource)
188                     data.update({
189                         'token': auth,
190                         'token_type': 'ap',
191                         'adobe_requestor_id': requestor_id,
192                     })
193                 else:
194                     self._initialize_geo_bypass({'countries': ['US']})
195                 entitlement = self._download_json(
196                     'https://api.entitlement.watchabc.go.com/vp2/ws-secure/entitlement/2020/authorize.json',
197                     video_id, data=urlencode_postdata(data))
198                 errors = entitlement.get('errors', {}).get('errors', [])
199                 if errors:
200                     for error in errors:
201                         if error.get('code') == 1002:
202                             self.raise_geo_restricted(
203                                 error['message'], countries=['US'])
204                     error_message = ', '.join([error['message'] for error in errors])
205                     raise ExtractorError('%s said: %s' % (self.IE_NAME, error_message), expected=True)
206                 asset_url += '?' + entitlement['uplynkData']['sessionKey']
207                 formats.extend(self._extract_m3u8_formats(
208                     asset_url, video_id, 'mp4', m3u8_id=format_id or 'hls', fatal=False))
209             else:
210                 f = {
211                     'format_id': format_id,
212                     'url': asset_url,
213                     'ext': ext,
214                 }
215                 if re.search(r'(?:/mp4/source/|_source\.mp4)', asset_url):
216                     f.update({
217                         'format_id': ('%s-' % format_id if format_id else '') + 'SOURCE',
218                         'preference': 1,
219                     })
220                 else:
221                     mobj = re.search(r'/(\d+)x(\d+)/', asset_url)
222                     if mobj:
223                         height = int(mobj.group(2))
224                         f.update({
225                             'format_id': ('%s-' % format_id if format_id else '') + '%dP' % height,
226                             'width': int(mobj.group(1)),
227                             'height': height,
228                         })
229                 formats.append(f)
230         self._sort_formats(formats)
231
232         subtitles = {}
233         for cc in video_data.get('closedcaption', {}).get('src', []):
234             cc_url = cc.get('value')
235             if not cc_url:
236                 continue
237             ext = determine_ext(cc_url)
238             if ext == 'xml':
239                 ext = 'ttml'
240             subtitles.setdefault(cc.get('lang'), []).append({
241                 'url': cc_url,
242                 'ext': ext,
243             })
244
245         thumbnails = []
246         for thumbnail in video_data.get('thumbnails', {}).get('thumbnail', []):
247             thumbnail_url = thumbnail.get('value')
248             if not thumbnail_url:
249                 continue
250             thumbnails.append({
251                 'url': thumbnail_url,
252                 'width': int_or_none(thumbnail.get('width')),
253                 'height': int_or_none(thumbnail.get('height')),
254             })
255
256         return {
257             'id': video_id,
258             'title': title,
259             'description': video_data.get('longdescription') or video_data.get('description'),
260             'duration': int_or_none(video_data.get('duration', {}).get('value'), 1000),
261             'age_limit': parse_age_limit(video_data.get('tvrating', {}).get('rating')),
262             'episode_number': int_or_none(video_data.get('episodenumber')),
263             'series': video_data.get('show', {}).get('title'),
264             'season_number': int_or_none(video_data.get('season', {}).get('num')),
265             'thumbnails': thumbnails,
266             'formats': formats,
267             'subtitles': subtitles,
268         }