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