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