1 from __future__ import unicode_literals
6 from .common import InfoExtractor
24 class LivestreamIE(InfoExtractor):
25 IE_NAME = 'livestream'
26 _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+))?'
28 'url': 'http://new.livestream.com/CoheedandCambria/WebsterHall/videos/4719370',
29 'md5': '53274c76ba7754fb0e8d072716f2292b',
33 'title': 'Live from Webster Hall NYC',
34 'timestamp': 1350008072,
35 'upload_date': '20121012',
39 'thumbnail': 're:^http://.*\.jpg$'
42 'url': 'http://new.livestream.com/tedx/cityenglish',
44 'title': 'TEDCity2.0 (English)',
47 'playlist_mincount': 4,
49 'url': 'http://new.livestream.com/chess24/tatasteelchess',
51 'title': 'Tata Steel Chess',
54 'playlist_mincount': 60,
56 'url': 'https://new.livestream.com/accounts/362/events/3557232/videos/67864563/player?autoPlay=false&height=360&mute=false&width=640',
57 'only_matching': True,
59 'url': 'http://livestream.com/bsww/concacafbeachsoccercampeonato2015',
60 'only_matching': True,
62 _API_URL_TEMPLATE = 'http://livestream.com/api/accounts/%s/events/%s'
64 def _parse_smil_formats(self, smil, smil_url, video_id, namespace=None, f4m_params=None, transform_rtmp_url=None):
65 base_ele = find_xpath_attr(smil, self._xpath_ns('.//meta', namespace), 'name', 'httpBase')
66 base = base_ele.get('content') if base_ele else 'http://livestreamvod-f.akamaihd.net/'
69 video_nodes = smil.findall(self._xpath_ns('.//video', namespace))
71 for vn in video_nodes:
72 tbr = int_or_none(vn.attrib.get('system-bitrate'), 1000)
74 '%s%s?v=3.0.3&fp=WIN%%2014,0,0,145' % (base, vn.attrib['src']))
75 if 'clipBegin' in vn.attrib:
76 furl += '&ssek=' + vn.attrib['clipBegin']
79 'format_id': 'smil_%d' % tbr,
86 def _extract_video_info(self, video_data):
87 video_id = compat_str(video_data['id'])
90 ('sd', 'progressive_url'),
91 ('hd', 'progressive_url_hd'),
95 for format_id, key in FORMAT_KEYS:
96 video_url = video_data.get(key)
98 ext = determine_ext(video_url)
99 bitrate = int_or_none(self._search_regex(r'(\d+)\.%s' % ext, video_url, 'bitrate', default=None))
102 'format_id': format_id,
107 smil_url = video_data.get('smil_url')
109 smil_formats = self._extract_smil_formats(smil_url, video_id)
111 formats.extend(smil_formats)
113 m3u8_url = video_data.get('m3u8_url')
115 m3u8_formats = self._extract_m3u8_formats(
116 m3u8_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)
118 formats.extend(m3u8_formats)
120 f4m_url = video_data.get('f4m_url')
122 f4m_formats = self._extract_f4m_formats(f4m_url, video_id, f4m_id='hds', fatal=False)
124 formats.extend(f4m_formats)
125 self._sort_formats(formats)
128 'author_id': comment.get('author_id'),
129 'author': comment.get('author', {}).get('full_name'),
130 'id': comment.get('id'),
131 'text': comment['text'],
132 'timestamp': parse_iso8601(comment.get('created_at')),
133 } for comment in video_data.get('comments', {}).get('data', [])]
138 'title': video_data['caption'],
139 'description': video_data.get('description'),
140 'thumbnail': video_data.get('thumbnail_url'),
141 'duration': float_or_none(video_data.get('duration'), 1000),
142 'timestamp': parse_iso8601(video_data.get('publish_at')),
143 'like_count': video_data.get('likes', {}).get('total'),
144 'comment_count': video_data.get('comments', {}).get('total'),
145 'view_count': video_data.get('views'),
146 'comments': comments,
149 def _extract_stream_info(self, stream_info):
150 broadcast_id = stream_info['broadcast_id']
151 is_live = stream_info.get('is_live')
154 smil_url = stream_info.get('play_url')
156 smil_formats = self._extract_smil_formats(smil_url, broadcast_id)
158 formats.extend(smil_formats)
160 m3u8_url = stream_info.get('m3u8_url')
162 m3u8_formats = self._extract_m3u8_formats(
163 m3u8_url, broadcast_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)
165 formats.extend(m3u8_formats)
167 rtsp_url = stream_info.get('rtsp_url')
173 self._sort_formats(formats)
178 'title': self._live_title(stream_info['stream_title']) if is_live else stream_info['stream_title'],
179 'thumbnail': stream_info.get('thumbnail_url'),
183 def _extract_event(self, event_data):
184 event_id = compat_str(event_data['id'])
185 account_id = compat_str(event_data['owner_account_id'])
186 feed_root_url = self._API_URL_TEMPLATE % (account_id, event_id) + '/feed.json'
188 stream_info = event_data.get('stream_info')
190 return self._extract_stream_info(stream_info)
194 for i in itertools.count(1):
195 if last_video is None:
196 info_url = feed_root_url
198 info_url = '{root}?&id={id}&newer=-1&type=video'.format(
199 root=feed_root_url, id=last_video)
200 videos_info = self._download_json(info_url, event_id, 'Downloading page {0}'.format(i))['data']
201 videos_info = [v['data'] for v in videos_info if v['type'] == 'video']
204 for v in videos_info:
205 entries.append(self.url_result(
206 'http://livestream.com/accounts/%s/events/%s/videos/%s' % (account_id, event_id, v['id']),
207 'Livestream', v['id'], v['caption']))
208 last_video = videos_info[-1]['id']
209 return self.playlist_result(entries, event_id, event_data['full_name'])
211 def _real_extract(self, url):
212 mobj = re.match(self._VALID_URL, url)
213 video_id = mobj.group('id')
214 event = mobj.group('event_id') or mobj.group('event_name')
215 account = mobj.group('account_id') or mobj.group('account_name')
216 api_url = self._API_URL_TEMPLATE % (account, event)
218 video_data = self._download_json(api_url + '/videos/%s' % video_id, video_id)
219 return self._extract_video_info(video_data)
221 event_data = self._download_json(api_url, video_id)
222 return self._extract_event(event_data)
225 # The original version of Livestream uses a different system
226 class LivestreamOriginalIE(InfoExtractor):
227 IE_NAME = 'livestream:original'
228 _VALID_URL = r'''(?x)https?://original\.livestream\.com/
229 (?P<user>[^/]+)/(?P<type>video|folder)
230 (?:\?.*?Id=|/)(?P<id>.*?)(&|$)
233 'url': 'http://original.livestream.com/dealbook/video?clipId=pla_8aa4a3f1-ba15-46a4-893b-902210e138fb',
235 'id': 'pla_8aa4a3f1-ba15-46a4-893b-902210e138fb',
237 'title': 'Spark 1 (BitCoin) with Cameron Winklevoss & Tyler Winklevoss of Winklevoss Capital',
242 'url': 'https://original.livestream.com/newplay/folder?dirId=a07bf706-d0e4-4e75-a747-b021d84f2fd3',
244 'id': 'a07bf706-d0e4-4e75-a747-b021d84f2fd3',
246 'playlist_mincount': 4,
249 def _extract_video(self, user, video_id):
250 api_url = 'http://x{0}x.api.channel.livestream.com/2.0/clipdetails?extendedInfo=true&id={1}'.format(user, video_id)
252 info = self._download_xml(api_url, video_id)
253 # this url is used on mobile devices
254 stream_url = 'http://x{0}x.api.channel.livestream.com/3.0/getstream.json?id={1}'.format(user, video_id)
255 stream_info = self._download_json(stream_url, video_id)
256 is_live = stream_info.get('isLive')
257 item = info.find('channel').find('item')
258 media_ns = {'media': 'http://search.yahoo.com/mrss'}
259 thumbnail_url = xpath_attr(item, xpath_with_ns('media:thumbnail', media_ns), 'url')
260 duration = float_or_none(xpath_attr(item, xpath_with_ns('media:content', media_ns), 'duration'))
261 ls_ns = {'ls': 'http://api.channel.livestream.com/2.0'}
262 view_count = int_or_none(xpath_text(item, xpath_with_ns('ls:viewsCount', ls_ns)))
265 'url': stream_info['progressiveUrl'],
269 m3u8_url = stream_info.get('httpUrl')
271 m3u8_formats = self._extract_m3u8_formats(
272 m3u8_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)
274 formats.extend(m3u8_formats)
276 rtsp_url = stream_info.get('rtspUrl')
282 self._sort_formats(formats)
286 'title': self._live_title(xpath_text(item, 'title')) if is_live else xpath_text(item, 'title'),
288 'thumbnail': thumbnail_url,
289 'duration': duration,
290 'view_count': view_count,
294 def _extract_folder(self, url, folder_id):
295 webpage = self._download_webpage(url, folder_id)
296 paths = orderedSet(re.findall(
298 <li\s+class="folder">\s*<a\s+href="|
299 <a\s+href="(?=https?://livestre\.am/)
300 )([^"]+)"''', webpage))
304 'url': compat_urlparse.urljoin(url, p),
307 return self.playlist_result(entries, folder_id)
309 def _real_extract(self, url):
310 mobj = re.match(self._VALID_URL, url)
311 id = mobj.group('id')
312 user = mobj.group('user')
313 url_type = mobj.group('type')
314 if url_type == 'folder':
315 return self._extract_folder(url, id)
317 return self._extract_video(user, id)
320 # The server doesn't support HEAD request, the generic extractor can't detect
322 class LivestreamShortenerIE(InfoExtractor):
323 IE_NAME = 'livestream:shortener'
324 IE_DESC = False # Do not list
325 _VALID_URL = r'https?://livestre\.am/(?P<id>.+)'
327 def _real_extract(self, url):
328 mobj = re.match(self._VALID_URL, url)
329 id = mobj.group('id')
330 webpage = self._download_webpage(url, id)
334 'url': self._og_search_url(webpage),