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