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