| 
									
										
										
										
											2014-06-12 20:42:46 +07:00
										 |  |  | from __future__ import unicode_literals | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-13 23:58:04 +02:00
										 |  |  | import re | 
					
						
							| 
									
										
										
										
											2015-03-14 12:06:01 +01:00
										 |  |  | import itertools | 
					
						
							| 
									
										
										
										
											2013-07-13 23:58:04 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | from .common import InfoExtractor | 
					
						
							| 
									
										
										
										
											2014-12-13 12:24:42 +01:00
										 |  |  | from ..compat import ( | 
					
						
							| 
									
										
										
										
											2014-07-25 11:39:17 +02:00
										 |  |  |     compat_str, | 
					
						
							| 
									
										
										
										
											2013-09-21 13:50:52 +02:00
										 |  |  |     compat_urlparse, | 
					
						
							| 
									
										
										
										
											2014-12-13 12:24:42 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | from ..utils import ( | 
					
						
							| 
									
										
										
										
											2014-07-25 11:39:17 +02:00
										 |  |  |     find_xpath_attr, | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |     xpath_attr, | 
					
						
							| 
									
										
										
										
											2014-07-25 11:39:17 +02:00
										 |  |  |     xpath_with_ns, | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |     xpath_text, | 
					
						
							|  |  |  |     orderedSet, | 
					
						
							| 
									
										
										
										
											2016-03-09 23:11:09 +06:00
										 |  |  |     update_url_query, | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |     int_or_none, | 
					
						
							|  |  |  |     float_or_none, | 
					
						
							|  |  |  |     parse_iso8601, | 
					
						
							|  |  |  |     determine_ext, | 
					
						
							| 
									
										
										
										
											2013-09-21 13:50:52 +02:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2013-07-13 23:58:04 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class LivestreamIE(InfoExtractor): | 
					
						
							| 
									
										
										
										
											2014-06-12 20:42:46 +07:00
										 |  |  |     IE_NAME = 'livestream' | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |     _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+))?' | 
					
						
							| 
									
										
										
										
											2014-08-28 00:58:24 +02:00
										 |  |  |     _TESTS = [{ | 
					
						
							| 
									
										
										
										
											2014-06-12 20:42:46 +07:00
										 |  |  |         'url': 'http://new.livestream.com/CoheedandCambria/WebsterHall/videos/4719370', | 
					
						
							|  |  |  |         'md5': '53274c76ba7754fb0e8d072716f2292b', | 
					
						
							|  |  |  |         'info_dict': { | 
					
						
							|  |  |  |             'id': '4719370', | 
					
						
							|  |  |  |             'ext': 'mp4', | 
					
						
							|  |  |  |             'title': 'Live from Webster Hall NYC', | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |             'timestamp': 1350008072, | 
					
						
							| 
									
										
										
										
											2014-06-12 20:42:46 +07:00
										 |  |  |             'upload_date': '20121012', | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |             'duration': 5968.0, | 
					
						
							| 
									
										
										
										
											2014-07-25 11:39:17 +02:00
										 |  |  |             'like_count': int, | 
					
						
							|  |  |  |             'view_count': int, | 
					
						
							| 
									
										
										
										
											2017-01-02 20:08:07 +08:00
										 |  |  |             'thumbnail': r're:^http://.*\.jpg$' | 
					
						
							| 
									
										
										
										
											2013-07-13 23:58:04 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-08-28 00:58:24 +02:00
										 |  |  |     }, { | 
					
						
							|  |  |  |         'url': 'http://new.livestream.com/tedx/cityenglish', | 
					
						
							|  |  |  |         'info_dict': { | 
					
						
							|  |  |  |             'title': 'TEDCity2.0 (English)', | 
					
						
							| 
									
										
										
										
											2015-02-18 00:34:45 +01:00
										 |  |  |             'id': '2245590', | 
					
						
							| 
									
										
										
										
											2014-08-28 00:58:24 +02:00
										 |  |  |         }, | 
					
						
							|  |  |  |         'playlist_mincount': 4, | 
					
						
							| 
									
										
										
										
											2015-03-14 12:06:01 +01:00
										 |  |  |     }, { | 
					
						
							|  |  |  |         'url': 'http://new.livestream.com/chess24/tatasteelchess', | 
					
						
							|  |  |  |         'info_dict': { | 
					
						
							|  |  |  |             'title': 'Tata Steel Chess', | 
					
						
							|  |  |  |             'id': '3705884', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         'playlist_mincount': 60, | 
					
						
							| 
									
										
										
										
											2014-11-13 16:12:51 +01:00
										 |  |  |     }, { | 
					
						
							|  |  |  |         'url': 'https://new.livestream.com/accounts/362/events/3557232/videos/67864563/player?autoPlay=false&height=360&mute=false&width=640', | 
					
						
							|  |  |  |         'only_matching': True, | 
					
						
							| 
									
										
										
										
											2015-04-08 17:42:26 +08:00
										 |  |  |     }, { | 
					
						
							|  |  |  |         'url': 'http://livestream.com/bsww/concacafbeachsoccercampeonato2015', | 
					
						
							|  |  |  |         'only_matching': True, | 
					
						
							| 
									
										
										
										
											2014-08-28 00:58:24 +02:00
										 |  |  |     }] | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |     _API_URL_TEMPLATE = 'http://livestream.com/api/accounts/%s/events/%s' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _parse_smil_formats(self, smil, smil_url, video_id, namespace=None, f4m_params=None, transform_rtmp_url=None): | 
					
						
							| 
									
										
										
										
											2015-12-27 12:57:39 +01:00
										 |  |  |         base_ele = find_xpath_attr( | 
					
						
							|  |  |  |             smil, self._xpath_ns('.//meta', namespace), 'name', 'httpBase') | 
					
						
							| 
									
										
										
										
											2016-03-04 01:16:58 +06:00
										 |  |  |         base = base_ele.get('content') if base_ele is not None else 'http://livestreamvod-f.akamaihd.net/' | 
					
						
							| 
									
										
										
										
											2013-07-13 23:58:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-25 11:53:52 +02:00
										 |  |  |         formats = [] | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |         video_nodes = smil.findall(self._xpath_ns('.//video', namespace)) | 
					
						
							| 
									
										
										
										
											2014-07-25 11:53:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         for vn in video_nodes: | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |             tbr = int_or_none(vn.attrib.get('system-bitrate'), 1000) | 
					
						
							| 
									
										
										
										
											2014-07-25 11:53:52 +02:00
										 |  |  |             furl = ( | 
					
						
							| 
									
										
										
										
											2016-03-09 23:11:09 +06:00
										 |  |  |                 update_url_query(compat_urlparse.urljoin(base, vn.attrib['src']), { | 
					
						
							|  |  |  |                     'v': '3.0.3', | 
					
						
							|  |  |  |                     'fp': 'WIN% 14,0,0,145', | 
					
						
							|  |  |  |                 })) | 
					
						
							| 
									
										
										
										
											2014-07-25 11:53:52 +02:00
										 |  |  |             if 'clipBegin' in vn.attrib: | 
					
						
							|  |  |  |                 furl += '&ssek=' + vn.attrib['clipBegin'] | 
					
						
							|  |  |  |             formats.append({ | 
					
						
							|  |  |  |                 'url': furl, | 
					
						
							|  |  |  |                 'format_id': 'smil_%d' % tbr, | 
					
						
							|  |  |  |                 'ext': 'flv', | 
					
						
							|  |  |  |                 'tbr': tbr, | 
					
						
							|  |  |  |                 'preference': -1000, | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |         return formats | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-13 23:58:04 +02:00
										 |  |  |     def _extract_video_info(self, video_data): | 
					
						
							| 
									
										
										
										
											2014-07-25 11:39:17 +02:00
										 |  |  |         video_id = compat_str(video_data['id']) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         FORMAT_KEYS = ( | 
					
						
							|  |  |  |             ('sd', 'progressive_url'), | 
					
						
							|  |  |  |             ('hd', 'progressive_url_hd'), | 
					
						
							| 
									
										
										
										
											2014-07-20 23:34:20 +02:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         formats = [] | 
					
						
							|  |  |  |         for format_id, key in FORMAT_KEYS: | 
					
						
							|  |  |  |             video_url = video_data.get(key) | 
					
						
							|  |  |  |             if video_url: | 
					
						
							|  |  |  |                 ext = determine_ext(video_url) | 
					
						
							| 
									
										
										
										
											2015-12-27 14:58:57 +01:00
										 |  |  |                 if ext == 'm3u8': | 
					
						
							|  |  |  |                     continue | 
					
						
							| 
									
										
										
										
											2015-12-27 12:57:39 +01:00
										 |  |  |                 bitrate = int_or_none(self._search_regex( | 
					
						
							|  |  |  |                     r'(\d+)\.%s' % ext, video_url, 'bitrate', default=None)) | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |                 formats.append({ | 
					
						
							|  |  |  |                     'url': video_url, | 
					
						
							|  |  |  |                     'format_id': format_id, | 
					
						
							|  |  |  |                     'tbr': bitrate, | 
					
						
							|  |  |  |                     'ext': ext, | 
					
						
							|  |  |  |                 }) | 
					
						
							| 
									
										
										
										
											2014-07-25 11:39:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         smil_url = video_data.get('smil_url') | 
					
						
							|  |  |  |         if smil_url: | 
					
						
							| 
									
										
										
										
											2015-12-29 00:58:24 +06:00
										 |  |  |             formats.extend(self._extract_smil_formats(smil_url, video_id)) | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         m3u8_url = video_data.get('m3u8_url') | 
					
						
							|  |  |  |         if m3u8_url: | 
					
						
							| 
									
										
										
										
											2015-12-29 00:58:24 +06:00
										 |  |  |             formats.extend(self._extract_m3u8_formats( | 
					
						
							| 
									
										
										
										
											2017-03-25 19:37:54 +01:00
										 |  |  |                 m3u8_url, video_id, 'mp4', 'm3u8_native', | 
					
						
							|  |  |  |                 m3u8_id='hls', fatal=False)) | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         f4m_url = video_data.get('f4m_url') | 
					
						
							|  |  |  |         if f4m_url: | 
					
						
							| 
									
										
										
										
											2015-12-29 00:58:24 +06:00
										 |  |  |             formats.extend(self._extract_f4m_formats( | 
					
						
							|  |  |  |                 f4m_url, video_id, f4m_id='hds', fatal=False)) | 
					
						
							| 
									
										
										
										
											2014-07-25 11:39:17 +02:00
										 |  |  |         self._sort_formats(formats) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |         comments = [{ | 
					
						
							|  |  |  |             'author_id': comment.get('author_id'), | 
					
						
							|  |  |  |             'author': comment.get('author', {}).get('full_name'), | 
					
						
							|  |  |  |             'id': comment.get('id'), | 
					
						
							|  |  |  |             'text': comment['text'], | 
					
						
							|  |  |  |             'timestamp': parse_iso8601(comment.get('created_at')), | 
					
						
							|  |  |  |         } for comment in video_data.get('comments', {}).get('data', [])] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-12 20:42:46 +07:00
										 |  |  |         return { | 
					
						
							| 
									
										
										
										
											2014-07-25 11:39:17 +02:00
										 |  |  |             'id': video_id, | 
					
						
							|  |  |  |             'formats': formats, | 
					
						
							| 
									
										
										
										
											2014-06-12 20:42:46 +07:00
										 |  |  |             'title': video_data['caption'], | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |             'description': video_data.get('description'), | 
					
						
							| 
									
										
										
										
											2014-07-25 11:39:17 +02:00
										 |  |  |             'thumbnail': video_data.get('thumbnail_url'), | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |             'duration': float_or_none(video_data.get('duration'), 1000), | 
					
						
							|  |  |  |             'timestamp': parse_iso8601(video_data.get('publish_at')), | 
					
						
							| 
									
										
										
										
											2014-07-25 11:39:17 +02:00
										 |  |  |             'like_count': video_data.get('likes', {}).get('total'), | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |             'comment_count': video_data.get('comments', {}).get('total'), | 
					
						
							| 
									
										
										
										
											2014-07-25 11:39:17 +02:00
										 |  |  |             'view_count': video_data.get('views'), | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |             'comments': comments, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _extract_stream_info(self, stream_info): | 
					
						
							| 
									
										
										
										
											2016-05-23 01:38:00 +08:00
										 |  |  |         broadcast_id = compat_str(stream_info['broadcast_id']) | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |         is_live = stream_info.get('is_live') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         formats = [] | 
					
						
							|  |  |  |         smil_url = stream_info.get('play_url') | 
					
						
							|  |  |  |         if smil_url: | 
					
						
							| 
									
										
										
										
											2015-12-29 00:58:24 +06:00
										 |  |  |             formats.extend(self._extract_smil_formats(smil_url, broadcast_id)) | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         m3u8_url = stream_info.get('m3u8_url') | 
					
						
							|  |  |  |         if m3u8_url: | 
					
						
							| 
									
										
										
										
											2015-12-29 00:58:24 +06:00
										 |  |  |             formats.extend(self._extract_m3u8_formats( | 
					
						
							| 
									
										
										
										
											2017-03-25 19:37:54 +01:00
										 |  |  |                 m3u8_url, broadcast_id, 'mp4', 'm3u8_native', | 
					
						
							|  |  |  |                 m3u8_id='hls', fatal=False)) | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         rtsp_url = stream_info.get('rtsp_url') | 
					
						
							|  |  |  |         if rtsp_url: | 
					
						
							|  |  |  |             formats.append({ | 
					
						
							|  |  |  |                 'url': rtsp_url, | 
					
						
							|  |  |  |                 'format_id': 'rtsp', | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |         self._sort_formats(formats) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             'id': broadcast_id, | 
					
						
							|  |  |  |             'formats': formats, | 
					
						
							|  |  |  |             'title': self._live_title(stream_info['stream_title']) if is_live else stream_info['stream_title'], | 
					
						
							|  |  |  |             'thumbnail': stream_info.get('thumbnail_url'), | 
					
						
							|  |  |  |             'is_live': is_live, | 
					
						
							| 
									
										
										
										
											2014-06-12 20:42:46 +07:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-07-13 23:58:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |     def _extract_event(self, event_data): | 
					
						
							|  |  |  |         event_id = compat_str(event_data['id']) | 
					
						
							|  |  |  |         account_id = compat_str(event_data['owner_account_id']) | 
					
						
							|  |  |  |         feed_root_url = self._API_URL_TEMPLATE % (account_id, event_id) + '/feed.json' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         stream_info = event_data.get('stream_info') | 
					
						
							|  |  |  |         if stream_info: | 
					
						
							|  |  |  |             return self._extract_stream_info(stream_info) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         last_video = None | 
					
						
							|  |  |  |         entries = [] | 
					
						
							|  |  |  |         for i in itertools.count(1): | 
					
						
							|  |  |  |             if last_video is None: | 
					
						
							|  |  |  |                 info_url = feed_root_url | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 info_url = '{root}?&id={id}&newer=-1&type=video'.format( | 
					
						
							|  |  |  |                     root=feed_root_url, id=last_video) | 
					
						
							| 
									
										
										
										
											2015-12-27 12:57:39 +01:00
										 |  |  |             videos_info = self._download_json( | 
					
						
							|  |  |  |                 info_url, event_id, 'Downloading page {0}'.format(i))['data'] | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |             videos_info = [v['data'] for v in videos_info if v['type'] == 'video'] | 
					
						
							|  |  |  |             if not videos_info: | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |             for v in videos_info: | 
					
						
							| 
									
										
										
										
											2016-06-07 23:53:08 +07:00
										 |  |  |                 v_id = compat_str(v['id']) | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |                 entries.append(self.url_result( | 
					
						
							| 
									
										
										
										
											2016-06-07 23:53:08 +07:00
										 |  |  |                     'http://livestream.com/accounts/%s/events/%s/videos/%s' % (account_id, event_id, v_id), | 
					
						
							|  |  |  |                     'Livestream', v_id, v.get('caption'))) | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |             last_video = videos_info[-1]['id'] | 
					
						
							|  |  |  |         return self.playlist_result(entries, event_id, event_data['full_name']) | 
					
						
							| 
									
										
										
										
											2015-03-14 12:06:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-13 23:58:04 +02:00
										 |  |  |     def _real_extract(self, url): | 
					
						
							|  |  |  |         mobj = re.match(self._VALID_URL, url) | 
					
						
							|  |  |  |         video_id = mobj.group('id') | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |         event = mobj.group('event_id') or mobj.group('event_name') | 
					
						
							|  |  |  |         account = mobj.group('account_id') or mobj.group('account_name') | 
					
						
							|  |  |  |         api_url = self._API_URL_TEMPLATE % (account, event) | 
					
						
							|  |  |  |         if video_id: | 
					
						
							| 
									
										
										
										
											2015-12-27 12:57:39 +01:00
										 |  |  |             video_data = self._download_json( | 
					
						
							|  |  |  |                 api_url + '/videos/%s' % video_id, video_id) | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |             return self._extract_video_info(video_data) | 
					
						
							| 
									
										
										
										
											2014-08-28 00:58:24 +02:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |             event_data = self._download_json(api_url, video_id) | 
					
						
							|  |  |  |             return self._extract_event(event_data) | 
					
						
							| 
									
										
										
										
											2013-11-14 13:16:32 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # The original version of Livestream uses a different system | 
					
						
							|  |  |  | class LivestreamOriginalIE(InfoExtractor): | 
					
						
							| 
									
										
										
										
											2014-06-12 20:42:46 +07:00
										 |  |  |     IE_NAME = 'livestream:original' | 
					
						
							| 
									
										
										
										
											2015-05-04 16:54:01 +02:00
										 |  |  |     _VALID_URL = r'''(?x)https?://original\.livestream\.com/
 | 
					
						
							| 
									
										
										
										
											2015-12-27 12:57:39 +01:00
										 |  |  |         (?P<user>[^/\?#]+)(?:/(?P<type>video|folder) | 
					
						
							|  |  |  |         (?:(?:\?.*?Id=|/)(?P<id>.*?)(&|$))?)? | 
					
						
							| 
									
										
										
										
											2014-06-26 16:34:36 +02:00
										 |  |  |         '''
 | 
					
						
							| 
									
										
										
										
											2014-08-28 00:58:24 +02:00
										 |  |  |     _TESTS = [{ | 
					
						
							| 
									
										
										
										
											2015-05-04 16:54:01 +02:00
										 |  |  |         'url': 'http://original.livestream.com/dealbook/video?clipId=pla_8aa4a3f1-ba15-46a4-893b-902210e138fb', | 
					
						
							| 
									
										
										
										
											2014-06-12 20:42:46 +07:00
										 |  |  |         'info_dict': { | 
					
						
							|  |  |  |             'id': 'pla_8aa4a3f1-ba15-46a4-893b-902210e138fb', | 
					
						
							| 
									
										
										
										
											2015-05-04 16:50:22 +02:00
										 |  |  |             'ext': 'mp4', | 
					
						
							| 
									
										
										
										
											2014-06-12 20:42:46 +07:00
										 |  |  |             'title': 'Spark 1 (BitCoin) with Cameron Winklevoss & Tyler Winklevoss of Winklevoss Capital', | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |             'duration': 771.301, | 
					
						
							|  |  |  |             'view_count': int, | 
					
						
							| 
									
										
										
										
											2013-11-14 13:16:32 +01:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2014-08-28 00:58:24 +02:00
										 |  |  |     }, { | 
					
						
							| 
									
										
										
										
											2015-05-04 16:54:01 +02:00
										 |  |  |         'url': 'https://original.livestream.com/newplay/folder?dirId=a07bf706-d0e4-4e75-a747-b021d84f2fd3', | 
					
						
							| 
									
										
										
										
											2014-08-28 00:58:24 +02:00
										 |  |  |         'info_dict': { | 
					
						
							|  |  |  |             'id': 'a07bf706-d0e4-4e75-a747-b021d84f2fd3', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         'playlist_mincount': 4, | 
					
						
							| 
									
										
										
										
											2015-12-27 12:57:39 +01:00
										 |  |  |     }, { | 
					
						
							|  |  |  |         # live stream | 
					
						
							| 
									
										
										
										
											2015-12-27 17:27:20 +01:00
										 |  |  |         'url': 'http://original.livestream.com/znsbahamas', | 
					
						
							| 
									
										
										
										
											2015-12-27 12:57:39 +01:00
										 |  |  |         'only_matching': True, | 
					
						
							| 
									
										
										
										
											2014-08-28 00:58:24 +02:00
										 |  |  |     }] | 
					
						
							| 
									
										
										
										
											2013-11-14 13:16:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-27 12:57:39 +01:00
										 |  |  |     def _extract_video_info(self, user, video_id): | 
					
						
							|  |  |  |         api_url = 'http://x%sx.api.channel.livestream.com/2.0/clipdetails?extendedInfo=true&id=%s' % (user, video_id) | 
					
						
							| 
									
										
										
										
											2013-11-26 18:48:52 +01:00
										 |  |  |         info = self._download_xml(api_url, video_id) | 
					
						
							| 
									
										
										
										
											2015-12-27 12:57:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-14 13:16:32 +01:00
										 |  |  |         item = info.find('channel').find('item') | 
					
						
							| 
									
										
										
										
											2015-12-27 12:57:39 +01:00
										 |  |  |         title = xpath_text(item, 'title') | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |         media_ns = {'media': 'http://search.yahoo.com/mrss'} | 
					
						
							| 
									
										
										
										
											2015-12-27 12:57:39 +01:00
										 |  |  |         thumbnail_url = xpath_attr( | 
					
						
							|  |  |  |             item, xpath_with_ns('media:thumbnail', media_ns), 'url') | 
					
						
							|  |  |  |         duration = float_or_none(xpath_attr( | 
					
						
							|  |  |  |             item, xpath_with_ns('media:content', media_ns), 'duration')) | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |         ls_ns = {'ls': 'http://api.channel.livestream.com/2.0'} | 
					
						
							| 
									
										
										
										
											2015-12-27 12:57:39 +01:00
										 |  |  |         view_count = int_or_none(xpath_text( | 
					
						
							|  |  |  |             item, xpath_with_ns('ls:viewsCount', ls_ns))) | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-27 12:57:39 +01:00
										 |  |  |         return { | 
					
						
							|  |  |  |             'id': video_id, | 
					
						
							|  |  |  |             'title': title, | 
					
						
							|  |  |  |             'thumbnail': thumbnail_url, | 
					
						
							|  |  |  |             'duration': duration, | 
					
						
							|  |  |  |             'view_count': view_count, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-25 19:37:54 +01:00
										 |  |  |     def _extract_video_formats(self, video_data, video_id): | 
					
						
							| 
									
										
										
										
											2015-12-27 12:57:39 +01:00
										 |  |  |         formats = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         progressive_url = video_data.get('progressiveUrl') | 
					
						
							|  |  |  |         if progressive_url: | 
					
						
							|  |  |  |             formats.append({ | 
					
						
							|  |  |  |                 'url': progressive_url, | 
					
						
							|  |  |  |                 'format_id': 'http', | 
					
						
							|  |  |  |             }) | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-27 12:57:39 +01:00
										 |  |  |         m3u8_url = video_data.get('httpUrl') | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |         if m3u8_url: | 
					
						
							| 
									
										
										
										
											2015-12-29 00:58:24 +06:00
										 |  |  |             formats.extend(self._extract_m3u8_formats( | 
					
						
							| 
									
										
										
										
											2017-03-25 19:37:54 +01:00
										 |  |  |                 m3u8_url, video_id, 'mp4', 'm3u8_native', | 
					
						
							|  |  |  |                 m3u8_id='hls', fatal=False)) | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-27 12:57:39 +01:00
										 |  |  |         rtsp_url = video_data.get('rtspUrl') | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |         if rtsp_url: | 
					
						
							|  |  |  |             formats.append({ | 
					
						
							|  |  |  |                 'url': rtsp_url, | 
					
						
							|  |  |  |                 'format_id': 'rtsp', | 
					
						
							|  |  |  |             }) | 
					
						
							| 
									
										
										
										
											2013-11-14 13:16:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-27 12:57:39 +01:00
										 |  |  |         self._sort_formats(formats) | 
					
						
							|  |  |  |         return formats | 
					
						
							| 
									
										
										
										
											2014-06-26 16:34:36 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _extract_folder(self, url, folder_id): | 
					
						
							|  |  |  |         webpage = self._download_webpage(url, folder_id) | 
					
						
							| 
									
										
										
										
											2014-08-28 00:58:24 +02:00
										 |  |  |         paths = orderedSet(re.findall( | 
					
						
							|  |  |  |             r'''(?x)(?:
 | 
					
						
							|  |  |  |                 <li\s+class="folder">\s*<a\s+href="| | 
					
						
							|  |  |  |                 <a\s+href="(?=https?://livestre\.am/) | 
					
						
							|  |  |  |             )([^"]+)"''', webpage))
 | 
					
						
							| 
									
										
										
										
											2014-06-26 16:34:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-17 20:56:54 +01:00
										 |  |  |         entries = [{ | 
					
						
							|  |  |  |             '_type': 'url', | 
					
						
							|  |  |  |             'url': compat_urlparse.urljoin(url, p), | 
					
						
							|  |  |  |         } for p in paths] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return self.playlist_result(entries, folder_id) | 
					
						
							| 
									
										
										
										
											2014-06-26 16:34:36 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _real_extract(self, url): | 
					
						
							|  |  |  |         mobj = re.match(self._VALID_URL, url) | 
					
						
							|  |  |  |         user = mobj.group('user') | 
					
						
							|  |  |  |         url_type = mobj.group('type') | 
					
						
							| 
									
										
										
										
											2015-12-27 12:57:39 +01:00
										 |  |  |         content_id = mobj.group('id') | 
					
						
							| 
									
										
										
										
											2014-06-26 16:34:36 +02:00
										 |  |  |         if url_type == 'folder': | 
					
						
							| 
									
										
										
										
											2015-12-27 12:57:39 +01:00
										 |  |  |             return self._extract_folder(url, content_id) | 
					
						
							| 
									
										
										
										
											2014-06-26 16:34:36 +02:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2015-12-27 12:57:39 +01:00
										 |  |  |             # this url is used on mobile devices | 
					
						
							|  |  |  |             stream_url = 'http://x%sx.api.channel.livestream.com/3.0/getstream.json' % user | 
					
						
							|  |  |  |             info = {} | 
					
						
							|  |  |  |             if content_id: | 
					
						
							|  |  |  |                 stream_url += '?id=%s' % content_id | 
					
						
							|  |  |  |                 info = self._extract_video_info(user, content_id) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 content_id = user | 
					
						
							|  |  |  |                 webpage = self._download_webpage(url, content_id) | 
					
						
							|  |  |  |                 info = { | 
					
						
							|  |  |  |                     'title': self._og_search_title(webpage), | 
					
						
							|  |  |  |                     'description': self._og_search_description(webpage), | 
					
						
							|  |  |  |                     'thumbnail': self._search_regex(r'channelLogo.src\s*=\s*"([^"]+)"', webpage, 'thumbnail', None), | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             video_data = self._download_json(stream_url, content_id) | 
					
						
							|  |  |  |             is_live = video_data.get('isLive') | 
					
						
							|  |  |  |             info.update({ | 
					
						
							|  |  |  |                 'id': content_id, | 
					
						
							|  |  |  |                 'title': self._live_title(info['title']) if is_live else info['title'], | 
					
						
							| 
									
										
										
										
											2017-03-25 19:37:54 +01:00
										 |  |  |                 'formats': self._extract_video_formats(video_data, content_id), | 
					
						
							| 
									
										
										
										
											2015-12-27 12:57:39 +01:00
										 |  |  |                 'is_live': is_live, | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |             return info | 
					
						
							| 
									
										
										
										
											2014-06-26 16:34:36 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # The server doesn't support HEAD request, the generic extractor can't detect | 
					
						
							|  |  |  | # the redirection | 
					
						
							|  |  |  | class LivestreamShortenerIE(InfoExtractor): | 
					
						
							|  |  |  |     IE_NAME = 'livestream:shortener' | 
					
						
							|  |  |  |     IE_DESC = False  # Do not list | 
					
						
							|  |  |  |     _VALID_URL = r'https?://livestre\.am/(?P<id>.+)' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _real_extract(self, url): | 
					
						
							|  |  |  |         mobj = re.match(self._VALID_URL, url) | 
					
						
							|  |  |  |         id = mobj.group('id') | 
					
						
							|  |  |  |         webpage = self._download_webpage(url, id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             '_type': 'url', | 
					
						
							|  |  |  |             'url': self._og_search_url(webpage), | 
					
						
							|  |  |  |         } |