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