]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/turner.py
Prepare to release
[youtubedl] / youtube_dl / extractor / turner.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .adobepass import AdobePassIE
7 from ..compat import compat_str
8 from ..utils import (
9 xpath_text,
10 int_or_none,
11 determine_ext,
12 parse_duration,
13 xpath_attr,
14 update_url_query,
15 ExtractorError,
16 strip_or_none,
17 )
18
19
20 class TurnerBaseIE(AdobePassIE):
21 def _extract_timestamp(self, video_data):
22 return int_or_none(xpath_attr(video_data, 'dateCreated', 'uts'))
23
24 def _extract_cvp_info(self, data_src, video_id, path_data={}, ap_data={}):
25 video_data = self._download_xml(data_src, video_id)
26 video_id = video_data.attrib['id']
27 title = xpath_text(video_data, 'headline', fatal=True)
28 content_id = xpath_text(video_data, 'contentId') or video_id
29 # rtmp_src = xpath_text(video_data, 'akamai/src')
30 # if rtmp_src:
31 # splited_rtmp_src = rtmp_src.split(',')
32 # if len(splited_rtmp_src) == 2:
33 # rtmp_src = splited_rtmp_src[1]
34 # aifp = xpath_text(video_data, 'akamai/aifp', default='')
35
36 tokens = {}
37 urls = []
38 formats = []
39 rex = re.compile(
40 r'(?P<width>[0-9]+)x(?P<height>[0-9]+)(?:_(?P<bitrate>[0-9]+))?')
41 # Possible formats locations: files/file, files/groupFiles/files
42 # and maybe others
43 for video_file in video_data.findall('.//file'):
44 video_url = video_file.text.strip()
45 if not video_url:
46 continue
47 ext = determine_ext(video_url)
48 if video_url.startswith('/mp4:protected/'):
49 continue
50 # TODO Correct extraction for these files
51 # protected_path_data = path_data.get('protected')
52 # if not protected_path_data or not rtmp_src:
53 # continue
54 # protected_path = self._search_regex(
55 # r'/mp4:(.+)\.[a-z0-9]', video_url, 'secure path')
56 # auth = self._download_webpage(
57 # protected_path_data['tokenizer_src'], query={
58 # 'path': protected_path,
59 # 'videoId': content_id,
60 # 'aifp': aifp,
61 # })
62 # token = xpath_text(auth, 'token')
63 # if not token:
64 # continue
65 # video_url = rtmp_src + video_url + '?' + token
66 elif video_url.startswith('/secure/'):
67 secure_path_data = path_data.get('secure')
68 if not secure_path_data:
69 continue
70 video_url = secure_path_data['media_src'] + video_url
71 secure_path = self._search_regex(r'https?://[^/]+(.+/)', video_url, 'secure path') + '*'
72 token = tokens.get(secure_path)
73 if not token:
74 query = {
75 'path': secure_path,
76 'videoId': content_id,
77 }
78 if ap_data.get('auth_required'):
79 query['accessToken'] = self._extract_mvpd_auth(ap_data['url'], video_id, ap_data['site_name'], ap_data['site_name'])
80 auth = self._download_xml(
81 secure_path_data['tokenizer_src'], video_id, query=query)
82 error_msg = xpath_text(auth, 'error/msg')
83 if error_msg:
84 raise ExtractorError(error_msg, expected=True)
85 token = xpath_text(auth, 'token')
86 if not token:
87 continue
88 tokens[secure_path] = token
89 video_url = video_url + '?hdnea=' + token
90 elif not re.match('https?://', video_url):
91 base_path_data = path_data.get(ext, path_data.get('default', {}))
92 media_src = base_path_data.get('media_src')
93 if not media_src:
94 continue
95 video_url = media_src + video_url
96 if video_url in urls:
97 continue
98 urls.append(video_url)
99 format_id = video_file.get('bitrate')
100 if ext == 'smil':
101 formats.extend(self._extract_smil_formats(
102 video_url, video_id, fatal=False))
103 elif ext == 'm3u8':
104 m3u8_formats = self._extract_m3u8_formats(
105 video_url, video_id, 'mp4',
106 m3u8_id=format_id or 'hls', fatal=False)
107 if '/secure/' in video_url and '?hdnea=' in video_url:
108 for f in m3u8_formats:
109 f['_seekable'] = False
110 formats.extend(m3u8_formats)
111 elif ext == 'f4m':
112 formats.extend(self._extract_f4m_formats(
113 update_url_query(video_url, {'hdcore': '3.7.0'}),
114 video_id, f4m_id=format_id or 'hds', fatal=False))
115 else:
116 f = {
117 'format_id': format_id,
118 'url': video_url,
119 'ext': ext,
120 }
121 mobj = rex.search(format_id + video_url)
122 if mobj:
123 f.update({
124 'width': int(mobj.group('width')),
125 'height': int(mobj.group('height')),
126 'tbr': int_or_none(mobj.group('bitrate')),
127 })
128 elif isinstance(format_id, compat_str):
129 if format_id.isdigit():
130 f['tbr'] = int(format_id)
131 else:
132 mobj = re.match(r'ios_(audio|[0-9]+)$', format_id)
133 if mobj:
134 if mobj.group(1) == 'audio':
135 f.update({
136 'vcodec': 'none',
137 'ext': 'm4a',
138 })
139 else:
140 f['tbr'] = int(mobj.group(1))
141 formats.append(f)
142 self._sort_formats(formats)
143
144 subtitles = {}
145 for source in video_data.findall('closedCaptions/source'):
146 for track in source.findall('track'):
147 track_url = track.get('url')
148 if not isinstance(track_url, compat_str) or track_url.endswith('/big'):
149 continue
150 lang = track.get('lang') or track.get('label') or 'en'
151 subtitles.setdefault(lang, []).append({
152 'url': track_url,
153 'ext': {
154 'scc': 'scc',
155 'webvtt': 'vtt',
156 'smptett': 'tt',
157 }.get(source.get('format'))
158 })
159
160 thumbnails = [{
161 'id': image.get('cut'),
162 'url': image.text,
163 'width': int_or_none(image.get('width')),
164 'height': int_or_none(image.get('height')),
165 } for image in video_data.findall('images/image')]
166
167 is_live = xpath_text(video_data, 'isLive') == 'true'
168
169 return {
170 'id': video_id,
171 'title': self._live_title(title) if is_live else title,
172 'formats': formats,
173 'subtitles': subtitles,
174 'thumbnails': thumbnails,
175 'thumbnail': xpath_text(video_data, 'poster'),
176 'description': strip_or_none(xpath_text(video_data, 'description')),
177 'duration': parse_duration(xpath_text(video_data, 'length') or xpath_text(video_data, 'trt')),
178 'timestamp': self._extract_timestamp(video_data),
179 'upload_date': xpath_attr(video_data, 'metas', 'version'),
180 'series': xpath_text(video_data, 'showTitle'),
181 'season_number': int_or_none(xpath_text(video_data, 'seasonNumber')),
182 'episode_number': int_or_none(xpath_text(video_data, 'episodeNumber')),
183 'is_live': is_live,
184 }