]>
Commit | Line | Data |
---|---|---|
1 | import re | |
2 | import itertools | |
3 | ||
4 | from .common import InfoExtractor | |
5 | from ..compat import ( | |
6 | compat_str, | |
7 | compat_urlparse, | |
8 | ) | |
9 | from ..utils import ( | |
10 | find_xpath_attr, | |
11 | xpath_attr, | |
12 | xpath_with_ns, | |
13 | xpath_text, | |
14 | orderedSet, | |
15 | update_url_query, | |
16 | int_or_none, | |
17 | float_or_none, | |
18 | parse_iso8601, | |
19 | determine_ext, | |
20 | ) | |
21 | ||
22 | ||
23 | class LivestreamIE(InfoExtractor): | |
24 | IE_NAME = 'livestream' | |
25 | _VALID_URL = r'https?://(?:new\.)?livestream\.com/(?:accounts/(?P<account_id>\d+)|(?P<account_name>[^/]+))/(?:events/(?P<event_id>\d+)|(?P<event_name>[^/]+))(?:/videos/(?P<id>\d+))?' | |
26 | _TESTS = [{ | |
27 | 'url': 'http://new.livestream.com/CoheedandCambria/WebsterHall/videos/4719370', | |
28 | 'md5': '53274c76ba7754fb0e8d072716f2292b', | |
29 | 'info_dict': { | |
30 | 'id': '4719370', | |
31 | 'ext': 'mp4', | |
32 | 'title': 'Live from Webster Hall NYC', | |
33 | 'timestamp': 1350008072, | |
34 | 'upload_date': '20121012', | |
35 | 'duration': 5968.0, | |
36 | 'like_count': int, | |
37 | 'view_count': int, | |
38 | 'thumbnail': r're:^http://.*\.jpg$' | |
39 | } | |
40 | }, { | |
41 | 'url': 'http://new.livestream.com/tedx/cityenglish', | |
42 | 'info_dict': { | |
43 | 'title': 'TEDCity2.0 (English)', | |
44 | 'id': '2245590', | |
45 | }, | |
46 | 'playlist_mincount': 4, | |
47 | }, { | |
48 | 'url': 'http://new.livestream.com/chess24/tatasteelchess', | |
49 | 'info_dict': { | |
50 | 'title': 'Tata Steel Chess', | |
51 | 'id': '3705884', | |
52 | }, | |
53 | 'playlist_mincount': 60, | |
54 | }, { | |
55 | 'url': 'https://new.livestream.com/accounts/362/events/3557232/videos/67864563/player?autoPlay=false&height=360&mute=false&width=640', | |
56 | 'only_matching': True, | |
57 | }, { | |
58 | 'url': 'http://livestream.com/bsww/concacafbeachsoccercampeonato2015', | |
59 | 'only_matching': True, | |
60 | }] | |
61 | _API_URL_TEMPLATE = 'http://livestream.com/api/accounts/%s/events/%s' | |
62 | ||
63 | def _parse_smil_formats(self, smil, smil_url, video_id, namespace=None, f4m_params=None, transform_rtmp_url=None): | |
64 | base_ele = find_xpath_attr( | |
65 | smil, self._xpath_ns('.//meta', namespace), 'name', 'httpBase') | |
66 | base = base_ele.get('content') if base_ele is not None else 'http://livestreamvod-f.akamaihd.net/' | |
67 | ||
68 | formats = [] | |
69 | video_nodes = smil.findall(self._xpath_ns('.//video', namespace)) | |
70 | ||
71 | for vn in video_nodes: | |
72 | tbr = int_or_none(vn.attrib.get('system-bitrate'), 1000) | |
73 | furl = ( | |
74 | update_url_query(compat_urlparse.urljoin(base, vn.attrib['src']), { | |
75 | 'v': '3.0.3', | |
76 | 'fp': 'WIN% 14,0,0,145', | |
77 | })) | |
78 | if 'clipBegin' in vn.attrib: | |
79 | furl += '&ssek=' + vn.attrib['clipBegin'] | |
80 | formats.append({ | |
81 | 'url': furl, | |
82 | 'format_id': 'smil_%d' % tbr, | |
83 | 'ext': 'flv', | |
84 | 'tbr': tbr, | |
85 | 'preference': -1000, # Strictly inferior than all other formats? | |
86 | }) | |
87 | return formats | |
88 | ||
89 | def _extract_video_info(self, video_data): | |
90 | video_id = compat_str(video_data['id']) | |
91 | ||
92 | FORMAT_KEYS = ( | |
93 | ('sd', 'progressive_url'), | |
94 | ('hd', 'progressive_url_hd'), | |
95 | ) | |
96 | ||
97 | formats = [] | |
98 | for format_id, key in FORMAT_KEYS: | |
99 | video_url = video_data.get(key) | |
100 | if video_url: | |
101 | ext = determine_ext(video_url) | |
102 | if ext == 'm3u8': | |
103 | continue | |
104 | bitrate = int_or_none(self._search_regex( | |
105 | r'(\d+)\.%s' % ext, video_url, 'bitrate', default=None)) | |
106 | formats.append({ | |
107 | 'url': video_url, | |
108 | 'format_id': format_id, | |
109 | 'tbr': bitrate, | |
110 | 'ext': ext, | |
111 | }) | |
112 | ||
113 | smil_url = video_data.get('smil_url') | |
114 | if smil_url: | |
115 | formats.extend(self._extract_smil_formats(smil_url, video_id, fatal=False)) | |
116 | ||
117 | m3u8_url = video_data.get('m3u8_url') | |
118 | if m3u8_url: | |
119 | formats.extend(self._extract_m3u8_formats( | |
120 | m3u8_url, video_id, 'mp4', 'm3u8_native', | |
121 | m3u8_id='hls', fatal=False)) | |
122 | ||
123 | f4m_url = video_data.get('f4m_url') | |
124 | if f4m_url: | |
125 | formats.extend(self._extract_f4m_formats( | |
126 | f4m_url, video_id, f4m_id='hds', fatal=False)) | |
127 | self._sort_formats(formats) | |
128 | ||
129 | comments = [{ | |
130 | 'author_id': comment.get('author_id'), | |
131 | 'author': comment.get('author', {}).get('full_name'), | |
132 | 'id': comment.get('id'), | |
133 | 'text': comment['text'], | |
134 | 'timestamp': parse_iso8601(comment.get('created_at')), | |
135 | } for comment in video_data.get('comments', {}).get('data', [])] | |
136 | ||
137 | return { | |
138 | 'id': video_id, | |
139 | 'formats': formats, | |
140 | 'title': video_data['caption'], | |
141 | 'description': video_data.get('description'), | |
142 | 'thumbnail': video_data.get('thumbnail_url'), | |
143 | 'duration': float_or_none(video_data.get('duration'), 1000), | |
144 | 'timestamp': parse_iso8601(video_data.get('publish_at')), | |
145 | 'like_count': video_data.get('likes', {}).get('total'), | |
146 | 'comment_count': video_data.get('comments', {}).get('total'), | |
147 | 'view_count': video_data.get('views'), | |
148 | 'comments': comments, | |
149 | } | |
150 | ||
151 | def _extract_stream_info(self, stream_info): | |
152 | broadcast_id = compat_str(stream_info['broadcast_id']) | |
153 | is_live = stream_info.get('is_live') | |
154 | ||
155 | formats = [] | |
156 | smil_url = stream_info.get('play_url') | |
157 | if smil_url: | |
158 | formats.extend(self._extract_smil_formats(smil_url, broadcast_id)) | |
159 | ||
160 | m3u8_url = stream_info.get('m3u8_url') | |
161 | if m3u8_url: | |
162 | formats.extend(self._extract_m3u8_formats( | |
163 | m3u8_url, broadcast_id, 'mp4', 'm3u8_native', | |
164 | m3u8_id='hls', fatal=False)) | |
165 | ||
166 | rtsp_url = stream_info.get('rtsp_url') | |
167 | if rtsp_url: | |
168 | formats.append({ | |
169 | 'url': rtsp_url, | |
170 | 'format_id': 'rtsp', | |
171 | }) | |
172 | self._sort_formats(formats) | |
173 | ||
174 | return { | |
175 | 'id': broadcast_id, | |
176 | 'formats': formats, | |
177 | 'title': stream_info['stream_title'], | |
178 | 'thumbnail': stream_info.get('thumbnail_url'), | |
179 | 'is_live': is_live, | |
180 | } | |
181 | ||
182 | def _extract_event(self, event_data): | |
183 | event_id = compat_str(event_data['id']) | |
184 | account_id = compat_str(event_data['owner_account_id']) | |
185 | feed_root_url = self._API_URL_TEMPLATE % (account_id, event_id) + '/feed.json' | |
186 | ||
187 | stream_info = event_data.get('stream_info') | |
188 | if stream_info: | |
189 | return self._extract_stream_info(stream_info) | |
190 | ||
191 | last_video = None | |
192 | entries = [] | |
193 | for i in itertools.count(1): | |
194 | if last_video is None: | |
195 | info_url = feed_root_url | |
196 | else: | |
197 | info_url = '{root}?&id={id}&newer=-1&type=video'.format( | |
198 | root=feed_root_url, id=last_video) | |
199 | videos_info = self._download_json( | |
200 | info_url, event_id, 'Downloading page {0}'.format(i))['data'] | |
201 | videos_info = [v['data'] for v in videos_info if v['type'] == 'video'] | |
202 | if not videos_info: | |
203 | break | |
204 | for v in videos_info: | |
205 | v_id = compat_str(v['id']) | |
206 | entries.append(self.url_result( | |
207 | 'http://livestream.com/accounts/%s/events/%s/videos/%s' % (account_id, event_id, v_id), | |
208 | 'Livestream', v_id, v.get('caption'))) | |
209 | last_video = videos_info[-1]['id'] | |
210 | return self.playlist_result(entries, event_id, event_data['full_name']) | |
211 | ||
212 | def _real_extract(self, url): | |
213 | mobj = self._match_valid_url(url) | |
214 | video_id = mobj.group('id') | |
215 | event = mobj.group('event_id') or mobj.group('event_name') | |
216 | account = mobj.group('account_id') or mobj.group('account_name') | |
217 | api_url = self._API_URL_TEMPLATE % (account, event) | |
218 | if video_id: | |
219 | video_data = self._download_json( | |
220 | api_url + '/videos/%s' % video_id, video_id) | |
221 | return self._extract_video_info(video_data) | |
222 | else: | |
223 | event_data = self._download_json(api_url, video_id) | |
224 | return self._extract_event(event_data) | |
225 | ||
226 | ||
227 | # The original version of Livestream uses a different system | |
228 | class LivestreamOriginalIE(InfoExtractor): | |
229 | IE_NAME = 'livestream:original' | |
230 | _VALID_URL = r'''(?x)https?://original\.livestream\.com/ | |
231 | (?P<user>[^/\?#]+)(?:/(?P<type>video|folder) | |
232 | (?:(?:\?.*?Id=|/)(?P<id>.*?)(&|$))?)? | |
233 | ''' | |
234 | _TESTS = [{ | |
235 | 'url': 'http://original.livestream.com/dealbook/video?clipId=pla_8aa4a3f1-ba15-46a4-893b-902210e138fb', | |
236 | 'info_dict': { | |
237 | 'id': 'pla_8aa4a3f1-ba15-46a4-893b-902210e138fb', | |
238 | 'ext': 'mp4', | |
239 | 'title': 'Spark 1 (BitCoin) with Cameron Winklevoss & Tyler Winklevoss of Winklevoss Capital', | |
240 | 'duration': 771.301, | |
241 | 'view_count': int, | |
242 | }, | |
243 | }, { | |
244 | 'url': 'https://original.livestream.com/newplay/folder?dirId=a07bf706-d0e4-4e75-a747-b021d84f2fd3', | |
245 | 'info_dict': { | |
246 | 'id': 'a07bf706-d0e4-4e75-a747-b021d84f2fd3', | |
247 | }, | |
248 | 'playlist_mincount': 4, | |
249 | }, { | |
250 | # live stream | |
251 | 'url': 'http://original.livestream.com/znsbahamas', | |
252 | 'only_matching': True, | |
253 | }] | |
254 | ||
255 | def _extract_video_info(self, user, video_id): | |
256 | api_url = 'http://x%sx.api.channel.livestream.com/2.0/clipdetails?extendedInfo=true&id=%s' % (user, video_id) | |
257 | info = self._download_xml(api_url, video_id) | |
258 | ||
259 | item = info.find('channel').find('item') | |
260 | title = xpath_text(item, 'title') | |
261 | media_ns = {'media': 'http://search.yahoo.com/mrss'} | |
262 | thumbnail_url = xpath_attr( | |
263 | item, xpath_with_ns('media:thumbnail', media_ns), 'url') | |
264 | duration = float_or_none(xpath_attr( | |
265 | item, xpath_with_ns('media:content', media_ns), 'duration')) | |
266 | ls_ns = {'ls': 'http://api.channel.livestream.com/2.0'} | |
267 | view_count = int_or_none(xpath_text( | |
268 | item, xpath_with_ns('ls:viewsCount', ls_ns))) | |
269 | ||
270 | return { | |
271 | 'id': video_id, | |
272 | 'title': title, | |
273 | 'thumbnail': thumbnail_url, | |
274 | 'duration': duration, | |
275 | 'view_count': view_count, | |
276 | } | |
277 | ||
278 | def _extract_video_formats(self, video_data, video_id): | |
279 | formats = [] | |
280 | ||
281 | progressive_url = video_data.get('progressiveUrl') | |
282 | if progressive_url: | |
283 | formats.append({ | |
284 | 'url': progressive_url, | |
285 | 'format_id': 'http', | |
286 | }) | |
287 | ||
288 | m3u8_url = video_data.get('httpUrl') | |
289 | if m3u8_url: | |
290 | formats.extend(self._extract_m3u8_formats( | |
291 | m3u8_url, video_id, 'mp4', 'm3u8_native', | |
292 | m3u8_id='hls', fatal=False)) | |
293 | ||
294 | rtsp_url = video_data.get('rtspUrl') | |
295 | if rtsp_url: | |
296 | formats.append({ | |
297 | 'url': rtsp_url, | |
298 | 'format_id': 'rtsp', | |
299 | }) | |
300 | ||
301 | self._sort_formats(formats) | |
302 | return formats | |
303 | ||
304 | def _extract_folder(self, url, folder_id): | |
305 | webpage = self._download_webpage(url, folder_id) | |
306 | paths = orderedSet(re.findall( | |
307 | r'''(?x)(?: | |
308 | <li\s+class="folder">\s*<a\s+href="| | |
309 | <a\s+href="(?=https?://livestre\.am/) | |
310 | )([^"]+)"''', webpage)) | |
311 | ||
312 | entries = [{ | |
313 | '_type': 'url', | |
314 | 'url': compat_urlparse.urljoin(url, p), | |
315 | } for p in paths] | |
316 | ||
317 | return self.playlist_result(entries, folder_id) | |
318 | ||
319 | def _real_extract(self, url): | |
320 | mobj = self._match_valid_url(url) | |
321 | user = mobj.group('user') | |
322 | url_type = mobj.group('type') | |
323 | content_id = mobj.group('id') | |
324 | if url_type == 'folder': | |
325 | return self._extract_folder(url, content_id) | |
326 | else: | |
327 | # this url is used on mobile devices | |
328 | stream_url = 'http://x%sx.api.channel.livestream.com/3.0/getstream.json' % user | |
329 | info = {} | |
330 | if content_id: | |
331 | stream_url += '?id=%s' % content_id | |
332 | info = self._extract_video_info(user, content_id) | |
333 | else: | |
334 | content_id = user | |
335 | webpage = self._download_webpage(url, content_id) | |
336 | info = { | |
337 | 'title': self._og_search_title(webpage), | |
338 | 'description': self._og_search_description(webpage), | |
339 | 'thumbnail': self._search_regex(r'channelLogo\.src\s*=\s*"([^"]+)"', webpage, 'thumbnail', None), | |
340 | } | |
341 | video_data = self._download_json(stream_url, content_id) | |
342 | is_live = video_data.get('isLive') | |
343 | info.update({ | |
344 | 'id': content_id, | |
345 | 'title': info['title'], | |
346 | 'formats': self._extract_video_formats(video_data, content_id), | |
347 | 'is_live': is_live, | |
348 | }) | |
349 | return info | |
350 | ||
351 | ||
352 | # The server doesn't support HEAD request, the generic extractor can't detect | |
353 | # the redirection | |
354 | class LivestreamShortenerIE(InfoExtractor): | |
355 | IE_NAME = 'livestream:shortener' | |
356 | IE_DESC = False # Do not list | |
357 | _VALID_URL = r'https?://livestre\.am/(?P<id>.+)' | |
358 | ||
359 | def _real_extract(self, url): | |
360 | mobj = self._match_valid_url(url) | |
361 | id = mobj.group('id') | |
362 | webpage = self._download_webpage(url, id) | |
363 | ||
364 | return self.url_result(self._og_search_url(webpage)) |