]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/go.py
[extractor] Fix bug in 617f658b7ec1193749848c1b7343acab125dbc46
[yt-dlp.git] / yt_dlp / extractor / go.py
1 import re
2
3 from .adobepass import AdobePassIE
4 from ..compat import compat_str
5 from ..utils import (
6 int_or_none,
7 determine_ext,
8 parse_age_limit,
9 remove_start,
10 remove_end,
11 try_get,
12 urlencode_postdata,
13 ExtractorError,
14 )
15
16
17 class GoIE(AdobePassIE):
18 _SITE_INFO = {
19 'abc': {
20 'brand': '001',
21 'requestor_id': 'ABC',
22 },
23 'freeform': {
24 'brand': '002',
25 'requestor_id': 'ABCFamily',
26 },
27 'watchdisneychannel': {
28 'brand': '004',
29 'resource_id': 'Disney',
30 },
31 'watchdisneyjunior': {
32 'brand': '008',
33 'resource_id': 'DisneyJunior',
34 },
35 'watchdisneyxd': {
36 'brand': '009',
37 'resource_id': 'DisneyXD',
38 },
39 'disneynow': {
40 'brand': '011',
41 'resource_id': 'Disney',
42 },
43 'fxnow.fxnetworks': {
44 'brand': '025',
45 'requestor_id': 'dtci',
46 },
47 }
48 _VALID_URL = r'''(?x)
49 https?://
50 (?P<sub_domain>
51 (?:%s\.)?go|fxnow\.fxnetworks|
52 (?:www\.)?(?:abc|freeform|disneynow)
53 )\.com/
54 (?:
55 (?:[^/]+/)*(?P<id>[Vv][Dd][Kk][Aa]\w+)|
56 (?:[^/]+/)*(?P<display_id>[^/?\#]+)
57 )
58 ''' % r'\.|'.join(list(_SITE_INFO.keys()))
59 _TESTS = [{
60 'url': 'http://abc.go.com/shows/designated-survivor/video/most-recent/VDKA3807643',
61 'info_dict': {
62 'id': 'VDKA3807643',
63 'ext': 'mp4',
64 'title': 'The Traitor in the White House',
65 'description': 'md5:05b009d2d145a1e85d25111bd37222e8',
66 },
67 'params': {
68 # m3u8 download
69 'skip_download': True,
70 },
71 'skip': 'This content is no longer available.',
72 }, {
73 'url': 'http://watchdisneyxd.go.com/doraemon',
74 'info_dict': {
75 'title': 'Doraemon',
76 'id': 'SH55574025',
77 },
78 'playlist_mincount': 51,
79 }, {
80 'url': 'http://freeform.go.com/shows/shadowhunters/episodes/season-2/1-this-guilty-blood',
81 'info_dict': {
82 'id': 'VDKA3609139',
83 'ext': 'mp4',
84 'title': 'This Guilty Blood',
85 'description': 'md5:f18e79ad1c613798d95fdabfe96cd292',
86 'age_limit': 14,
87 },
88 'params': {
89 'geo_bypass_ip_block': '3.244.239.0/24',
90 # m3u8 download
91 'skip_download': True,
92 },
93 }, {
94 'url': 'https://abc.com/shows/the-rookie/episode-guide/season-02/03-the-bet',
95 'info_dict': {
96 'id': 'VDKA13435179',
97 'ext': 'mp4',
98 'title': 'The Bet',
99 'description': 'md5:c66de8ba2e92c6c5c113c3ade84ab404',
100 'age_limit': 14,
101 },
102 'params': {
103 'geo_bypass_ip_block': '3.244.239.0/24',
104 # m3u8 download
105 'skip_download': True,
106 },
107 }, {
108 'url': 'https://fxnow.fxnetworks.com/shows/better-things/video/vdka12782841',
109 'info_dict': {
110 'id': 'VDKA12782841',
111 'ext': 'mp4',
112 'title': 'First Look: Better Things - Season 2',
113 'description': 'md5:fa73584a95761c605d9d54904e35b407',
114 },
115 'params': {
116 'geo_bypass_ip_block': '3.244.239.0/24',
117 # m3u8 download
118 'skip_download': True,
119 },
120 }, {
121 'url': 'https://abc.com/shows/modern-family/episode-guide/season-01/101-pilot',
122 'info_dict': {
123 'id': 'VDKA22600213',
124 'ext': 'mp4',
125 'title': 'Pilot',
126 'description': 'md5:74306df917cfc199d76d061d66bebdb4',
127 },
128 'params': {
129 # m3u8 download
130 'skip_download': True,
131 },
132 }, {
133 'url': 'http://abc.go.com/shows/the-catch/episode-guide/season-01/10-the-wedding',
134 'only_matching': True,
135 }, {
136 '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',
137 'only_matching': True,
138 }, {
139 # brand 004
140 'url': 'http://disneynow.go.com/shows/big-hero-6-the-series/season-01/episode-10-mr-sparkles-loses-his-sparkle/vdka4637915',
141 'only_matching': True,
142 }, {
143 # brand 008
144 'url': 'http://disneynow.go.com/shows/minnies-bow-toons/video/happy-campers/vdka4872013',
145 'only_matching': True,
146 }, {
147 'url': 'https://disneynow.com/shows/minnies-bow-toons/video/happy-campers/vdka4872013',
148 'only_matching': True,
149 }, {
150 'url': 'https://www.freeform.com/shows/cruel-summer/episode-guide/season-01/01-happy-birthday-jeanette-turner',
151 'only_matching': True,
152 }]
153
154 def _extract_videos(self, brand, video_id='-1', show_id='-1'):
155 display_id = video_id if video_id != '-1' else show_id
156 return self._download_json(
157 '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),
158 display_id)['video']
159
160 def _real_extract(self, url):
161 mobj = self._match_valid_url(url)
162 sub_domain = remove_start(remove_end(mobj.group('sub_domain') or '', '.go'), 'www.')
163 video_id, display_id = mobj.group('id', 'display_id')
164 site_info = self._SITE_INFO.get(sub_domain, {})
165 brand = site_info.get('brand')
166 if not video_id or not site_info:
167 webpage = self._download_webpage(url, display_id or video_id)
168 data = self._parse_json(
169 self._search_regex(
170 r'["\']__abc_com__["\']\s*\]\s*=\s*({.+?})\s*;', webpage,
171 'data', default='{}'),
172 display_id or video_id, fatal=False)
173 # https://abc.com/shows/modern-family/episode-guide/season-01/101-pilot
174 layout = try_get(data, lambda x: x['page']['content']['video']['layout'], dict)
175 video_id = None
176 if layout:
177 video_id = try_get(
178 layout,
179 (lambda x: x['videoid'], lambda x: x['video']['id']),
180 compat_str)
181 if not video_id:
182 video_id = self._search_regex(
183 (
184 # There may be inner quotes, e.g. data-video-id="'VDKA3609139'"
185 # from http://freeform.go.com/shows/shadowhunters/episodes/season-2/1-this-guilty-blood
186 r'data-video-id=["\']*(VDKA\w+)',
187 # page.analytics.videoIdCode
188 r'\bvideoIdCode["\']\s*:\s*["\']((?:vdka|VDKA)\w+)',
189 # https://abc.com/shows/the-rookie/episode-guide/season-02/03-the-bet
190 r'\b(?:video)?id["\']\s*:\s*["\'](VDKA\w+)'
191 ), webpage, 'video id', default=video_id)
192 if not site_info:
193 brand = self._search_regex(
194 (r'data-brand=\s*["\']\s*(\d+)',
195 r'data-page-brand=\s*["\']\s*(\d+)'), webpage, 'brand',
196 default='004')
197 site_info = next(
198 si for _, si in self._SITE_INFO.items()
199 if si.get('brand') == brand)
200 if not video_id:
201 # show extraction works for Disney, DisneyJunior and DisneyXD
202 # ABC and Freeform has different layout
203 show_id = self._search_regex(r'data-show-id=["\']*(SH\d+)', webpage, 'show id')
204 videos = self._extract_videos(brand, show_id=show_id)
205 show_title = self._search_regex(r'data-show-title="([^"]+)"', webpage, 'show title', fatal=False)
206 entries = []
207 for video in videos:
208 entries.append(self.url_result(
209 video['url'], 'Go', video.get('id'), video.get('title')))
210 entries.reverse()
211 return self.playlist_result(entries, show_id, show_title)
212 video_data = self._extract_videos(brand, video_id)[0]
213 video_id = video_data['id']
214 title = video_data['title']
215
216 formats = []
217 subtitles = {}
218 for asset in video_data.get('assets', {}).get('asset', []):
219 asset_url = asset.get('value')
220 if not asset_url:
221 continue
222 format_id = asset.get('format')
223 ext = determine_ext(asset_url)
224 if ext == 'm3u8':
225 video_type = video_data.get('type')
226 data = {
227 'video_id': video_data['id'],
228 'video_type': video_type,
229 'brand': brand,
230 'device': '001',
231 }
232 if video_data.get('accesslevel') == '1':
233 requestor_id = site_info.get('requestor_id', 'DisneyChannels')
234 resource = site_info.get('resource_id') or self._get_mvpd_resource(
235 requestor_id, title, video_id, None)
236 auth = self._extract_mvpd_auth(
237 url, video_id, requestor_id, resource)
238 data.update({
239 'token': auth,
240 'token_type': 'ap',
241 'adobe_requestor_id': requestor_id,
242 })
243 else:
244 self._initialize_geo_bypass({'countries': ['US']})
245 entitlement = self._download_json(
246 'https://api.entitlement.watchabc.go.com/vp2/ws-secure/entitlement/2020/authorize.json',
247 video_id, data=urlencode_postdata(data))
248 errors = entitlement.get('errors', {}).get('errors', [])
249 if errors:
250 for error in errors:
251 if error.get('code') == 1002:
252 self.raise_geo_restricted(
253 error['message'], countries=['US'])
254 error_message = ', '.join([error['message'] for error in errors])
255 raise ExtractorError('%s said: %s' % (self.IE_NAME, error_message), expected=True)
256 asset_url += '?' + entitlement['uplynkData']['sessionKey']
257 fmts, subs = self._extract_m3u8_formats_and_subtitles(
258 asset_url, video_id, 'mp4', m3u8_id=format_id or 'hls', fatal=False)
259 formats.extend(fmts)
260 self._merge_subtitles(subs, target=subtitles)
261 else:
262 f = {
263 'format_id': format_id,
264 'url': asset_url,
265 'ext': ext,
266 }
267 if re.search(r'(?:/mp4/source/|_source\.mp4)', asset_url):
268 f.update({
269 'format_id': ('%s-' % format_id if format_id else '') + 'SOURCE',
270 'quality': 1,
271 })
272 else:
273 mobj = re.search(r'/(\d+)x(\d+)/', asset_url)
274 if mobj:
275 height = int(mobj.group(2))
276 f.update({
277 'format_id': ('%s-' % format_id if format_id else '') + '%dP' % height,
278 'width': int(mobj.group(1)),
279 'height': height,
280 })
281 formats.append(f)
282 self._sort_formats(formats)
283
284 for cc in video_data.get('closedcaption', {}).get('src', []):
285 cc_url = cc.get('value')
286 if not cc_url:
287 continue
288 ext = determine_ext(cc_url)
289 if ext == 'xml':
290 ext = 'ttml'
291 subtitles.setdefault(cc.get('lang'), []).append({
292 'url': cc_url,
293 'ext': ext,
294 })
295
296 thumbnails = []
297 for thumbnail in video_data.get('thumbnails', {}).get('thumbnail', []):
298 thumbnail_url = thumbnail.get('value')
299 if not thumbnail_url:
300 continue
301 thumbnails.append({
302 'url': thumbnail_url,
303 'width': int_or_none(thumbnail.get('width')),
304 'height': int_or_none(thumbnail.get('height')),
305 })
306
307 return {
308 'id': video_id,
309 'title': title,
310 'description': video_data.get('longdescription') or video_data.get('description'),
311 'duration': int_or_none(video_data.get('duration', {}).get('value'), 1000),
312 'age_limit': parse_age_limit(video_data.get('tvrating', {}).get('rating')),
313 'episode_number': int_or_none(video_data.get('episodenumber')),
314 'series': video_data.get('show', {}).get('title'),
315 'season_number': int_or_none(video_data.get('season', {}).get('num')),
316 'thumbnails': thumbnails,
317 'formats': formats,
318 'subtitles': subtitles,
319 }