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