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