]>
Commit | Line | Data |
---|---|---|
1 | import random | |
2 | import re | |
3 | ||
4 | from .common import InfoExtractor | |
5 | from ..compat import ( | |
6 | compat_str, | |
7 | compat_urlparse, | |
8 | ) | |
9 | from ..utils import ( | |
10 | encode_data_uri, | |
11 | ExtractorError, | |
12 | int_or_none, | |
13 | float_or_none, | |
14 | join_nonempty, | |
15 | mimetype2ext, | |
16 | str_or_none, | |
17 | ) | |
18 | ||
19 | ||
20 | class UstreamIE(InfoExtractor): | |
21 | _VALID_URL = r'https?://(?:www\.)?(?:ustream\.tv|video\.ibm\.com)/(?P<type>recorded|embed|embed/recorded)/(?P<id>\d+)' | |
22 | IE_NAME = 'ustream' | |
23 | _EMBED_REGEX = [r'<iframe[^>]+?src=(["\'])(?P<url>https?://(?:www\.)?(?:ustream\.tv|video\.ibm\.com)/embed/.+?)\1'] | |
24 | _TESTS = [{ | |
25 | 'url': 'http://www.ustream.tv/recorded/20274954', | |
26 | 'md5': '088f151799e8f572f84eb62f17d73e5c', | |
27 | 'info_dict': { | |
28 | 'id': '20274954', | |
29 | 'ext': 'flv', | |
30 | 'title': 'Young Americans for Liberty February 7, 2012 2:28 AM', | |
31 | 'description': 'Young Americans for Liberty February 7, 2012 2:28 AM', | |
32 | 'timestamp': 1328577035, | |
33 | 'upload_date': '20120207', | |
34 | 'uploader': 'yaliberty', | |
35 | 'uploader_id': '6780869', | |
36 | }, | |
37 | }, { | |
38 | # From http://sportscanada.tv/canadagames/index.php/week2/figure-skating/444 | |
39 | # Title and uploader available only from params JSON | |
40 | 'url': 'http://www.ustream.tv/embed/recorded/59307601?ub=ff0000&lc=ff0000&oc=ffffff&uc=ffffff&v=3&wmode=direct', | |
41 | 'md5': '5a2abf40babeac9812ed20ae12d34e10', | |
42 | 'info_dict': { | |
43 | 'id': '59307601', | |
44 | 'ext': 'flv', | |
45 | 'title': '-CG11- Canada Games Figure Skating', | |
46 | 'uploader': 'sportscanadatv', | |
47 | }, | |
48 | 'skip': 'This Pro Broadcaster has chosen to remove this video from the ustream.tv site.', | |
49 | }, { | |
50 | 'url': 'http://www.ustream.tv/embed/10299409', | |
51 | 'info_dict': { | |
52 | 'id': '10299409', | |
53 | }, | |
54 | 'playlist_count': 3, | |
55 | }, { | |
56 | 'url': 'http://www.ustream.tv/recorded/91343263', | |
57 | 'info_dict': { | |
58 | 'id': '91343263', | |
59 | 'ext': 'mp4', | |
60 | 'title': 'GitHub Universe - General Session - Day 1', | |
61 | 'upload_date': '20160914', | |
62 | 'description': 'GitHub Universe - General Session - Day 1', | |
63 | 'timestamp': 1473872730, | |
64 | 'uploader': 'wa0dnskeqkr', | |
65 | 'uploader_id': '38977840', | |
66 | }, | |
67 | 'params': { | |
68 | 'skip_download': True, # m3u8 download | |
69 | }, | |
70 | }, { | |
71 | 'url': 'https://video.ibm.com/embed/recorded/128240221?&autoplay=true&controls=true&volume=100', | |
72 | 'only_matching': True, | |
73 | }] | |
74 | ||
75 | def _get_stream_info(self, url, video_id, app_id_ver, extra_note=None): | |
76 | def num_to_hex(n): | |
77 | return hex(n)[2:] | |
78 | ||
79 | rnd = random.randrange | |
80 | ||
81 | if not extra_note: | |
82 | extra_note = '' | |
83 | ||
84 | conn_info = self._download_json( | |
85 | 'http://r%d-1-%s-recorded-lp-live.ums.ustream.tv/1/ustream' % (rnd(1e8), video_id), | |
86 | video_id, note='Downloading connection info' + extra_note, | |
87 | query={ | |
88 | 'type': 'viewer', | |
89 | 'appId': app_id_ver[0], | |
90 | 'appVersion': app_id_ver[1], | |
91 | 'rsid': '%s:%s' % (num_to_hex(rnd(1e8)), num_to_hex(rnd(1e8))), | |
92 | 'rpin': '_rpin.%d' % rnd(1e15), | |
93 | 'referrer': url, | |
94 | 'media': video_id, | |
95 | 'application': 'recorded', | |
96 | }) | |
97 | host = conn_info[0]['args'][0]['host'] | |
98 | connection_id = conn_info[0]['args'][0]['connectionId'] | |
99 | ||
100 | return self._download_json( | |
101 | 'http://%s/1/ustream?connectionId=%s' % (host, connection_id), | |
102 | video_id, note='Downloading stream info' + extra_note) | |
103 | ||
104 | def _get_streams(self, url, video_id, app_id_ver): | |
105 | # Sometimes the return dict does not have 'stream' | |
106 | for trial_count in range(3): | |
107 | stream_info = self._get_stream_info( | |
108 | url, video_id, app_id_ver, | |
109 | extra_note=' (try %d)' % (trial_count + 1) if trial_count > 0 else '') | |
110 | if 'stream' in stream_info[0]['args'][0]: | |
111 | return stream_info[0]['args'][0]['stream'] | |
112 | return [] | |
113 | ||
114 | def _parse_segmented_mp4(self, dash_stream_info): | |
115 | def resolve_dash_template(template, idx, chunk_hash): | |
116 | return template.replace('%', compat_str(idx), 1).replace('%', chunk_hash) | |
117 | ||
118 | formats = [] | |
119 | for stream in dash_stream_info['streams']: | |
120 | # Use only one provider to avoid too many formats | |
121 | provider = dash_stream_info['providers'][0] | |
122 | fragments = [{ | |
123 | 'url': resolve_dash_template( | |
124 | provider['url'] + stream['initUrl'], 0, dash_stream_info['hashes']['0']) | |
125 | }] | |
126 | for idx in range(dash_stream_info['videoLength'] // dash_stream_info['chunkTime']): | |
127 | fragments.append({ | |
128 | 'url': resolve_dash_template( | |
129 | provider['url'] + stream['segmentUrl'], idx, | |
130 | dash_stream_info['hashes'][compat_str(idx // 10 * 10)]) | |
131 | }) | |
132 | content_type = stream['contentType'] | |
133 | kind = content_type.split('/')[0] | |
134 | f = { | |
135 | 'format_id': join_nonempty( | |
136 | 'dash', kind, str_or_none(stream.get('bitrate'))), | |
137 | 'protocol': 'http_dash_segments', | |
138 | # TODO: generate a MPD doc for external players? | |
139 | 'url': encode_data_uri(b'<MPD/>', 'text/xml'), | |
140 | 'ext': mimetype2ext(content_type), | |
141 | 'height': stream.get('height'), | |
142 | 'width': stream.get('width'), | |
143 | 'fragments': fragments, | |
144 | } | |
145 | if kind == 'video': | |
146 | f.update({ | |
147 | 'vcodec': stream.get('codec'), | |
148 | 'acodec': 'none', | |
149 | 'vbr': stream.get('bitrate'), | |
150 | }) | |
151 | else: | |
152 | f.update({ | |
153 | 'vcodec': 'none', | |
154 | 'acodec': stream.get('codec'), | |
155 | 'abr': stream.get('bitrate'), | |
156 | }) | |
157 | formats.append(f) | |
158 | return formats | |
159 | ||
160 | def _real_extract(self, url): | |
161 | m = self._match_valid_url(url) | |
162 | video_id = m.group('id') | |
163 | ||
164 | # some sites use this embed format (see: https://github.com/ytdl-org/youtube-dl/issues/2990) | |
165 | if m.group('type') == 'embed/recorded': | |
166 | video_id = m.group('id') | |
167 | desktop_url = 'http://www.ustream.tv/recorded/' + video_id | |
168 | return self.url_result(desktop_url, 'Ustream') | |
169 | if m.group('type') == 'embed': | |
170 | video_id = m.group('id') | |
171 | webpage = self._download_webpage(url, video_id) | |
172 | content_video_ids = self._parse_json(self._search_regex( | |
173 | r'ustream\.vars\.offAirContentVideoIds=([^;]+);', webpage, | |
174 | 'content video IDs'), video_id) | |
175 | return self.playlist_result( | |
176 | map(lambda u: self.url_result('http://www.ustream.tv/recorded/' + u, 'Ustream'), content_video_ids), | |
177 | video_id) | |
178 | ||
179 | params = self._download_json( | |
180 | 'https://api.ustream.tv/videos/%s.json' % video_id, video_id) | |
181 | ||
182 | error = params.get('error') | |
183 | if error: | |
184 | raise ExtractorError( | |
185 | '%s returned error: %s' % (self.IE_NAME, error), expected=True) | |
186 | ||
187 | video = params['video'] | |
188 | ||
189 | title = video['title'] | |
190 | filesize = float_or_none(video.get('file_size')) | |
191 | ||
192 | formats = [{ | |
193 | 'id': video_id, | |
194 | 'url': video_url, | |
195 | 'ext': format_id, | |
196 | 'filesize': filesize, | |
197 | } for format_id, video_url in video['media_urls'].items() if video_url] | |
198 | ||
199 | if not formats: | |
200 | hls_streams = self._get_streams(url, video_id, app_id_ver=(11, 2)) | |
201 | if hls_streams: | |
202 | # m3u8_native leads to intermittent ContentTooShortError | |
203 | formats.extend(self._extract_m3u8_formats( | |
204 | hls_streams[0]['url'], video_id, ext='mp4', m3u8_id='hls')) | |
205 | ||
206 | ''' | |
207 | # DASH streams handling is incomplete as 'url' is missing | |
208 | dash_streams = self._get_streams(url, video_id, app_id_ver=(3, 1)) | |
209 | if dash_streams: | |
210 | formats.extend(self._parse_segmented_mp4(dash_streams)) | |
211 | ''' | |
212 | ||
213 | description = video.get('description') | |
214 | timestamp = int_or_none(video.get('created_at')) | |
215 | duration = float_or_none(video.get('length')) | |
216 | view_count = int_or_none(video.get('views')) | |
217 | ||
218 | uploader = video.get('owner', {}).get('username') | |
219 | uploader_id = video.get('owner', {}).get('id') | |
220 | ||
221 | thumbnails = [{ | |
222 | 'id': thumbnail_id, | |
223 | 'url': thumbnail_url, | |
224 | } for thumbnail_id, thumbnail_url in video.get('thumbnail', {}).items()] | |
225 | ||
226 | return { | |
227 | 'id': video_id, | |
228 | 'title': title, | |
229 | 'description': description, | |
230 | 'thumbnails': thumbnails, | |
231 | 'timestamp': timestamp, | |
232 | 'duration': duration, | |
233 | 'view_count': view_count, | |
234 | 'uploader': uploader, | |
235 | 'uploader_id': uploader_id, | |
236 | 'formats': formats, | |
237 | } | |
238 | ||
239 | ||
240 | class UstreamChannelIE(InfoExtractor): | |
241 | _VALID_URL = r'https?://(?:www\.)?ustream\.tv/channel/(?P<slug>.+)' | |
242 | IE_NAME = 'ustream:channel' | |
243 | _TEST = { | |
244 | 'url': 'http://www.ustream.tv/channel/channeljapan', | |
245 | 'info_dict': { | |
246 | 'id': '10874166', | |
247 | }, | |
248 | 'playlist_mincount': 17, | |
249 | } | |
250 | ||
251 | def _real_extract(self, url): | |
252 | m = self._match_valid_url(url) | |
253 | display_id = m.group('slug') | |
254 | webpage = self._download_webpage(url, display_id) | |
255 | channel_id = self._html_search_meta('ustream:channel_id', webpage) | |
256 | ||
257 | BASE = 'http://www.ustream.tv' | |
258 | next_url = '/ajax/socialstream/videos/%s/1.json' % channel_id | |
259 | video_ids = [] | |
260 | while next_url: | |
261 | reply = self._download_json( | |
262 | compat_urlparse.urljoin(BASE, next_url), display_id, | |
263 | note='Downloading video information (next: %d)' % (len(video_ids) + 1)) | |
264 | video_ids.extend(re.findall(r'data-content-id="(\d.*)"', reply['data'])) | |
265 | next_url = reply['nextUrl'] | |
266 | ||
267 | entries = [ | |
268 | self.url_result('http://www.ustream.tv/recorded/' + vid, 'Ustream') | |
269 | for vid in video_ids] | |
270 | return { | |
271 | '_type': 'playlist', | |
272 | 'id': channel_id, | |
273 | 'display_id': display_id, | |
274 | 'entries': entries, | |
275 | } |