]> jfr.im git - yt-dlp.git/blob - youtube_dlc/extractor/go.py
Merge branch 'fixYTSearch' of https://github.com/xarantolus/youtube-dl into xarantolu...
[yt-dlp.git] / youtube_dlc / 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://github.com/ytdl-org/youtube-dl/pull/25216/files
141 # The following is based on the pull request on the line above. Changed the ABC.com URL to a show available now.
142 # https://abc.com/shows/the-rookie/episode-guide/season-02/19-the-q-word
143 r'\bvideoIdCode["\']\s*:\s*["\'](vdka\w+)',
144 # Deprecated fallback pattern
145 r'\b(?:video)?id["\']\s*:\s*["\'](VDKA\w+)'
146 ), webpage, 'video id', default=video_id)
147 if not site_info:
148 brand = self._search_regex(
149 (r'data-brand=\s*["\']\s*(\d+)',
150 r'data-page-brand=\s*["\']\s*(\d+)'), webpage, 'brand',
151 default='004')
152 site_info = next(
153 si for _, si in self._SITE_INFO.items()
154 if si.get('brand') == brand)
155 if not video_id:
156 # show extraction works for Disney, DisneyJunior and DisneyXD
157 # ABC and Freeform has different layout
158 show_id = self._search_regex(r'data-show-id=["\']*(SH\d+)', webpage, 'show id')
159 videos = self._extract_videos(brand, show_id=show_id)
160 show_title = self._search_regex(r'data-show-title="([^"]+)"', webpage, 'show title', fatal=False)
161 entries = []
162 for video in videos:
163 entries.append(self.url_result(
164 video['url'], 'Go', video.get('id'), video.get('title')))
165 entries.reverse()
166 return self.playlist_result(entries, show_id, show_title)
167 video_data = self._extract_videos(brand, video_id)[0]
168 video_id = video_data['id']
169 title = video_data['title']
170
171 formats = []
172 for asset in video_data.get('assets', {}).get('asset', []):
173 asset_url = asset.get('value')
174 if not asset_url:
175 continue
176 format_id = asset.get('format')
177 ext = determine_ext(asset_url)
178 if ext == 'm3u8':
179 video_type = video_data.get('type')
180 data = {
181 'video_id': video_data['id'],
182 'video_type': video_type,
183 'brand': brand,
184 'device': '001',
185 }
186 if video_data.get('accesslevel') == '1':
187 requestor_id = site_info.get('requestor_id', 'DisneyChannels')
188 resource = site_info.get('resource_id') or self._get_mvpd_resource(
189 requestor_id, title, video_id, None)
190 auth = self._extract_mvpd_auth(
191 url, video_id, requestor_id, resource)
192 data.update({
193 'token': auth,
194 'token_type': 'ap',
195 'adobe_requestor_id': requestor_id,
196 })
197 else:
198 self._initialize_geo_bypass({'countries': ['US']})
199 entitlement = self._download_json(
200 'https://api.entitlement.watchabc.go.com/vp2/ws-secure/entitlement/2020/authorize.json',
201 video_id, data=urlencode_postdata(data))
202 errors = entitlement.get('errors', {}).get('errors', [])
203 if errors:
204 for error in errors:
205 if error.get('code') == 1002:
206 self.raise_geo_restricted(
207 error['message'], countries=['US'])
208 error_message = ', '.join([error['message'] for error in errors])
209 raise ExtractorError('%s said: %s' % (self.IE_NAME, error_message), expected=True)
210 asset_url += '?' + entitlement['uplynkData']['sessionKey']
211 formats.extend(self._extract_m3u8_formats(
212 asset_url, video_id, 'mp4', m3u8_id=format_id or 'hls', fatal=False))
213 else:
214 f = {
215 'format_id': format_id,
216 'url': asset_url,
217 'ext': ext,
218 }
219 if re.search(r'(?:/mp4/source/|_source\.mp4)', asset_url):
220 f.update({
221 'format_id': ('%s-' % format_id if format_id else '') + 'SOURCE',
222 'preference': 1,
223 })
224 else:
225 mobj = re.search(r'/(\d+)x(\d+)/', asset_url)
226 if mobj:
227 height = int(mobj.group(2))
228 f.update({
229 'format_id': ('%s-' % format_id if format_id else '') + '%dP' % height,
230 'width': int(mobj.group(1)),
231 'height': height,
232 })
233 formats.append(f)
234 self._sort_formats(formats)
235
236 subtitles = {}
237 for cc in video_data.get('closedcaption', {}).get('src', []):
238 cc_url = cc.get('value')
239 if not cc_url:
240 continue
241 ext = determine_ext(cc_url)
242 if ext == 'xml':
243 ext = 'ttml'
244 subtitles.setdefault(cc.get('lang'), []).append({
245 'url': cc_url,
246 'ext': ext,
247 })
248
249 thumbnails = []
250 for thumbnail in video_data.get('thumbnails', {}).get('thumbnail', []):
251 thumbnail_url = thumbnail.get('value')
252 if not thumbnail_url:
253 continue
254 thumbnails.append({
255 'url': thumbnail_url,
256 'width': int_or_none(thumbnail.get('width')),
257 'height': int_or_none(thumbnail.get('height')),
258 })
259
260 return {
261 'id': video_id,
262 'title': title,
263 'description': video_data.get('longdescription') or video_data.get('description'),
264 'duration': int_or_none(video_data.get('duration', {}).get('value'), 1000),
265 'age_limit': parse_age_limit(video_data.get('tvrating', {}).get('rating')),
266 'episode_number': int_or_none(video_data.get('episodenumber')),
267 'series': video_data.get('show', {}).get('title'),
268 'season_number': int_or_none(video_data.get('season', {}).get('num')),
269 'thumbnails': thumbnails,
270 'formats': formats,
271 'subtitles': subtitles,
272 }