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