]>
Commit | Line | Data |
---|---|---|
0eb799ba JMF |
1 | from __future__ import unicode_literals |
2 | ||
e3d6bdc8 | 3 | import random |
78af8eb1 PH |
4 | import re |
5 | ||
6 | from .common import InfoExtractor | |
1cc79574 | 7 | from ..compat import ( |
e3d6bdc8 | 8 | compat_str, |
74ac9bdd | 9 | compat_urlparse, |
74ac9bdd | 10 | ) |
5820c4a2 | 11 | from ..utils import ( |
e3d6bdc8 | 12 | encode_data_uri, |
5820c4a2 S |
13 | ExtractorError, |
14 | int_or_none, | |
15 | float_or_none, | |
e3d6bdc8 YCH |
16 | mimetype2ext, |
17 | str_or_none, | |
5820c4a2 | 18 | ) |
78af8eb1 PH |
19 | |
20 | ||
21 | class UstreamIE(InfoExtractor): | |
92519402 | 22 | _VALID_URL = r'https?://(?:www\.)?ustream\.tv/(?P<type>recorded|embed|embed/recorded)/(?P<id>\d+)' |
0eb799ba | 23 | IE_NAME = 'ustream' |
cd9fdccd | 24 | _TESTS = [{ |
0eb799ba | 25 | 'url': 'http://www.ustream.tv/recorded/20274954', |
0eb799ba JMF |
26 | 'md5': '088f151799e8f572f84eb62f17d73e5c', |
27 | 'info_dict': { | |
9e875391 S |
28 | 'id': '20274954', |
29 | 'ext': 'flv', | |
9e875391 | 30 | 'title': 'Young Americans for Liberty February 7, 2012 2:28 AM', |
40fbb05e S |
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', | |
0eb799ba | 36 | }, |
cd9fdccd YCH |
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', | |
40fbb05e S |
47 | }, |
48 | 'skip': 'This Pro Broadcaster has chosen to remove this video from the ustream.tv site.', | |
67d46a3f YCH |
49 | }, { |
50 | 'url': 'http://www.ustream.tv/embed/10299409', | |
51 | 'info_dict': { | |
52 | 'id': '10299409', | |
53 | }, | |
54 | 'playlist_count': 3, | |
e3d6bdc8 YCH |
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 | }, | |
cd9fdccd | 70 | }] |
78af8eb1 | 71 | |
d77ac737 YCH |
72 | @staticmethod |
73 | def _extract_url(webpage): | |
74 | mobj = re.search( | |
75 | r'<iframe[^>]+?src=(["\'])(?P<url>http://www\.ustream\.tv/embed/.+?)\1', webpage) | |
76 | if mobj is not None: | |
77 | return mobj.group('url') | |
78 | ||
e3d6bdc8 YCH |
79 | def _get_stream_info(self, url, video_id, app_id_ver, extra_note=None): |
80 | def num_to_hex(n): | |
81 | return hex(n)[2:] | |
82 | ||
83 | rnd = random.randrange | |
84 | ||
85 | if not extra_note: | |
86 | extra_note = '' | |
87 | ||
88 | conn_info = self._download_json( | |
89 | 'http://r%d-1-%s-recorded-lp-live.ums.ustream.tv/1/ustream' % (rnd(1e8), video_id), | |
90 | video_id, note='Downloading connection info' + extra_note, | |
91 | query={ | |
92 | 'type': 'viewer', | |
93 | 'appId': app_id_ver[0], | |
94 | 'appVersion': app_id_ver[1], | |
95 | 'rsid': '%s:%s' % (num_to_hex(rnd(1e8)), num_to_hex(rnd(1e8))), | |
96 | 'rpin': '_rpin.%d' % rnd(1e15), | |
97 | 'referrer': url, | |
98 | 'media': video_id, | |
99 | 'application': 'recorded', | |
100 | }) | |
101 | host = conn_info[0]['args'][0]['host'] | |
102 | connection_id = conn_info[0]['args'][0]['connectionId'] | |
103 | ||
104 | return self._download_json( | |
105 | 'http://%s/1/ustream?connectionId=%s' % (host, connection_id), | |
106 | video_id, note='Downloading stream info' + extra_note) | |
107 | ||
108 | def _get_streams(self, url, video_id, app_id_ver): | |
109 | # Sometimes the return dict does not have 'stream' | |
110 | for trial_count in range(3): | |
111 | stream_info = self._get_stream_info( | |
112 | url, video_id, app_id_ver, | |
113 | extra_note=' (try %d)' % (trial_count + 1) if trial_count > 0 else '') | |
114 | if 'stream' in stream_info[0]['args'][0]: | |
115 | return stream_info[0]['args'][0]['stream'] | |
116 | return [] | |
117 | ||
118 | def _parse_segmented_mp4(self, dash_stream_info): | |
119 | def resolve_dash_template(template, idx, chunk_hash): | |
120 | return template.replace('%', compat_str(idx), 1).replace('%', chunk_hash) | |
121 | ||
122 | formats = [] | |
123 | for stream in dash_stream_info['streams']: | |
124 | # Use only one provider to avoid too many formats | |
125 | provider = dash_stream_info['providers'][0] | |
126 | fragments = [{ | |
127 | 'url': resolve_dash_template( | |
128 | provider['url'] + stream['initUrl'], 0, dash_stream_info['hashes']['0']) | |
129 | }] | |
130 | for idx in range(dash_stream_info['videoLength'] // dash_stream_info['chunkTime']): | |
131 | fragments.append({ | |
132 | 'url': resolve_dash_template( | |
133 | provider['url'] + stream['segmentUrl'], idx, | |
134 | dash_stream_info['hashes'][compat_str(idx // 10 * 10)]) | |
135 | }) | |
136 | content_type = stream['contentType'] | |
137 | kind = content_type.split('/')[0] | |
138 | f = { | |
139 | 'format_id': '-'.join(filter(None, [ | |
140 | 'dash', kind, str_or_none(stream.get('bitrate'))])), | |
141 | 'protocol': 'http_dash_segments', | |
142 | # TODO: generate a MPD doc for external players? | |
143 | 'url': encode_data_uri(b'<MPD/>', 'text/xml'), | |
144 | 'ext': mimetype2ext(content_type), | |
145 | 'height': stream.get('height'), | |
146 | 'width': stream.get('width'), | |
147 | 'fragments': fragments, | |
148 | } | |
149 | if kind == 'video': | |
150 | f.update({ | |
151 | 'vcodec': stream.get('codec'), | |
152 | 'acodec': 'none', | |
153 | 'vbr': stream.get('bitrate'), | |
154 | }) | |
155 | else: | |
156 | f.update({ | |
157 | 'vcodec': 'none', | |
158 | 'acodec': stream.get('codec'), | |
159 | 'abr': stream.get('bitrate'), | |
160 | }) | |
161 | formats.append(f) | |
162 | return formats | |
163 | ||
78af8eb1 PH |
164 | def _real_extract(self, url): |
165 | m = re.match(self._VALID_URL, url) | |
4853eb63 | 166 | video_id = m.group('id') |
b702eceb | 167 | |
e5a66240 | 168 | # some sites use this embed format (see: https://github.com/rg3/youtube-dl/issues/2990) |
9e875391 | 169 | if m.group('type') == 'embed/recorded': |
4853eb63 | 170 | video_id = m.group('id') |
b702eceb | 171 | desktop_url = 'http://www.ustream.tv/recorded/' + video_id |
172 | return self.url_result(desktop_url, 'Ustream') | |
5c386252 | 173 | if m.group('type') == 'embed': |
4853eb63 | 174 | video_id = m.group('id') |
5c386252 | 175 | webpage = self._download_webpage(url, video_id) |
67d46a3f YCH |
176 | content_video_ids = self._parse_json(self._search_regex( |
177 | r'ustream\.vars\.offAirContentVideoIds=([^;]+);', webpage, | |
178 | 'content video IDs'), video_id) | |
179 | return self.playlist_result( | |
180 | map(lambda u: self.url_result('http://www.ustream.tv/recorded/' + u, 'Ustream'), content_video_ids), | |
181 | video_id) | |
5c386252 | 182 | |
4853eb63 S |
183 | params = self._download_json( |
184 | 'https://api.ustream.tv/videos/%s.json' % video_id, video_id) | |
f8610ba1 | 185 | |
5820c4a2 S |
186 | error = params.get('error') |
187 | if error: | |
188 | raise ExtractorError( | |
189 | '%s returned error: %s' % (self.IE_NAME, error), expected=True) | |
78af8eb1 | 190 | |
5820c4a2 | 191 | video = params['video'] |
2a813727 | 192 | |
41db7333 S |
193 | title = video['title'] |
194 | filesize = float_or_none(video.get('file_size')) | |
195 | ||
5820c4a2 | 196 | formats = [{ |
dc5756fd | 197 | 'id': video_id, |
5820c4a2 S |
198 | 'url': video_url, |
199 | 'ext': format_id, | |
41db7333 | 200 | 'filesize': filesize, |
e3d6bdc8 YCH |
201 | } for format_id, video_url in video['media_urls'].items() if video_url] |
202 | ||
203 | if not formats: | |
204 | hls_streams = self._get_streams(url, video_id, app_id_ver=(11, 2)) | |
205 | if hls_streams: | |
206 | # m3u8_native leads to intermittent ContentTooShortError | |
207 | formats.extend(self._extract_m3u8_formats( | |
208 | hls_streams[0]['url'], video_id, ext='mp4', m3u8_id='hls')) | |
209 | ||
210 | ''' | |
211 | # DASH streams handling is incomplete as 'url' is missing | |
212 | dash_streams = self._get_streams(url, video_id, app_id_ver=(3, 1)) | |
213 | if dash_streams: | |
214 | formats.extend(self._parse_segmented_mp4(dash_streams)) | |
215 | ''' | |
216 | ||
5820c4a2 | 217 | self._sort_formats(formats) |
78af8eb1 | 218 | |
5820c4a2 S |
219 | description = video.get('description') |
220 | timestamp = int_or_none(video.get('created_at')) | |
221 | duration = float_or_none(video.get('length')) | |
5820c4a2 | 222 | view_count = int_or_none(video.get('views')) |
cd9fdccd | 223 | |
5820c4a2 S |
224 | uploader = video.get('owner', {}).get('username') |
225 | uploader_id = video.get('owner', {}).get('id') | |
78af8eb1 | 226 | |
5820c4a2 S |
227 | thumbnails = [{ |
228 | 'id': thumbnail_id, | |
229 | 'url': thumbnail_url, | |
230 | } for thumbnail_id, thumbnail_url in video.get('thumbnail', {}).items()] | |
0eb799ba JMF |
231 | |
232 | return { | |
233 | 'id': video_id, | |
5820c4a2 S |
234 | 'title': title, |
235 | 'description': description, | |
236 | 'thumbnails': thumbnails, | |
237 | 'timestamp': timestamp, | |
238 | 'duration': duration, | |
5820c4a2 | 239 | 'view_count': view_count, |
0eb799ba | 240 | 'uploader': uploader, |
5820c4a2 S |
241 | 'uploader_id': uploader_id, |
242 | 'formats': formats, | |
0eb799ba JMF |
243 | } |
244 | ||
bfd5c93a | 245 | |
bfd5c93a | 246 | class UstreamChannelIE(InfoExtractor): |
92519402 | 247 | _VALID_URL = r'https?://(?:www\.)?ustream\.tv/channel/(?P<slug>.+)' |
0eb799ba | 248 | IE_NAME = 'ustream:channel' |
22a6f150 PH |
249 | _TEST = { |
250 | 'url': 'http://www.ustream.tv/channel/channeljapan', | |
251 | 'info_dict': { | |
252 | 'id': '10874166', | |
253 | }, | |
446a03bd | 254 | 'playlist_mincount': 17, |
22a6f150 | 255 | } |
bfd5c93a | 256 | |
257 | def _real_extract(self, url): | |
258 | m = re.match(self._VALID_URL, url) | |
22a6f150 PH |
259 | display_id = m.group('slug') |
260 | webpage = self._download_webpage(url, display_id) | |
44789f24 | 261 | channel_id = self._html_search_meta('ustream:channel_id', webpage) |
bfd5c93a | 262 | |
bfd5c93a | 263 | BASE = 'http://www.ustream.tv' |
264 | next_url = '/ajax/socialstream/videos/%s/1.json' % channel_id | |
a921f407 | 265 | video_ids = [] |
bfd5c93a | 266 | while next_url: |
22a6f150 PH |
267 | reply = self._download_json( |
268 | compat_urlparse.urljoin(BASE, next_url), display_id, | |
269 | note='Downloading video information (next: %d)' % (len(video_ids) + 1)) | |
a921f407 | 270 | video_ids.extend(re.findall(r'data-content-id="(\d.*)"', reply['data'])) |
bfd5c93a | 271 | next_url = reply['nextUrl'] |
bfd5c93a | 272 | |
22a6f150 PH |
273 | entries = [ |
274 | self.url_result('http://www.ustream.tv/recorded/' + vid, 'Ustream') | |
275 | for vid in video_ids] | |
276 | return { | |
277 | '_type': 'playlist', | |
278 | 'id': channel_id, | |
279 | 'display_id': display_id, | |
280 | 'entries': entries, | |
281 | } |