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