]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/livestream.py
[livestream] Make clipBegin optional in SMIL
[yt-dlp.git] / youtube_dl / extractor / livestream.py
CommitLineData
c5469e04
S
1from __future__ import unicode_literals
2
b4444d5c
JMF
3import re
4import json
5
6from .common import InfoExtractor
b00ca882 7from ..utils import (
cbf915f3 8 compat_str,
b00ca882
JMF
9 compat_urllib_parse_urlparse,
10 compat_urlparse,
cbf915f3
PH
11 ExtractorError,
12 find_xpath_attr,
13 int_or_none,
78338f71 14 orderedSet,
cbf915f3 15 xpath_with_ns,
b00ca882 16)
b4444d5c
JMF
17
18
19class LivestreamIE(InfoExtractor):
c5469e04 20 IE_NAME = 'livestream'
c0ade33e 21 _VALID_URL = r'http://new\.livestream\.com/.*?/(?P<event_name>.*?)(/videos/(?P<id>\d+))?/?$'
b4444d5c 22 _TEST = {
c5469e04
S
23 'url': 'http://new.livestream.com/CoheedandCambria/WebsterHall/videos/4719370',
24 'md5': '53274c76ba7754fb0e8d072716f2292b',
25 'info_dict': {
26 'id': '4719370',
27 'ext': 'mp4',
28 'title': 'Live from Webster Hall NYC',
29 'upload_date': '20121012',
cbf915f3
PH
30 'like_count': int,
31 'view_count': int,
32 'thumbnail': 're:^http://.*\.jpg$'
b4444d5c
JMF
33 }
34 }
35
36 def _extract_video_info(self, video_data):
cbf915f3
PH
37 video_id = compat_str(video_data['id'])
38
39 FORMAT_KEYS = (
40 ('sd', 'progressive_url'),
41 ('hd', 'progressive_url_hd'),
72e785f3 42 )
cbf915f3
PH
43 formats = [{
44 'format_id': format_id,
45 'url': video_data[key],
46 'quality': i + 1,
47 } for i, (format_id, key) in enumerate(FORMAT_KEYS)
48 if video_data.get(key)]
49
50 smil_url = video_data.get('smil_url')
51 if smil_url:
52 _SWITCH_XPATH = (
53 './/{http://www.w3.org/2001/SMIL20/Language}body/'
54 '{http://www.w3.org/2001/SMIL20/Language}switch')
55 smil_doc = self._download_xml(
56 smil_url, video_id, note='Downloading SMIL information')
57
58 title_node = find_xpath_attr(
59 smil_doc, './/{http://www.w3.org/2001/SMIL20/Language}meta',
60 'name', 'title')
61 if title_node is None:
62 self.report_warning('Cannot find SMIL id')
63 switch_node = smil_doc.find(_SWITCH_XPATH)
64 else:
65 title_id = title_node.attrib['content']
66 switch_node = find_xpath_attr(
67 smil_doc, _SWITCH_XPATH, 'id', title_id)
68 if switch_node is None:
69 raise ExtractorError('Cannot find switch node')
70 video_nodes = switch_node.findall(
71 '{http://www.w3.org/2001/SMIL20/Language}video')
72
73 for vn in video_nodes:
74 tbr = int_or_none(vn.attrib.get('system-bitrate'))
75 furl = (
7fa547ab
PH
76 'http://livestream-f.akamaihd.net/%s?v=3.0.3&fp=WIN%%2014,0,0,145' %
77 (vn.attrib['src']))
78 if 'clipBegin' in vn.attrib:
79 furl += '&ssek=' + vn.attrib['clipBegin']
cbf915f3
PH
80 formats.append({
81 'url': furl,
82 'format_id': 'smil_%d' % tbr,
83 'ext': 'flv',
84 'tbr': tbr,
85 'preference': -1000,
86 })
87 self._sort_formats(formats)
88
c5469e04 89 return {
cbf915f3
PH
90 'id': video_id,
91 'formats': formats,
c5469e04 92 'title': video_data['caption'],
cbf915f3 93 'thumbnail': video_data.get('thumbnail_url'),
c5469e04 94 'upload_date': video_data['updated_at'].replace('-', '')[:8],
cbf915f3
PH
95 'like_count': video_data.get('likes', {}).get('total'),
96 'view_count': video_data.get('views'),
c5469e04 97 }
b4444d5c
JMF
98
99 def _real_extract(self, url):
100 mobj = re.match(self._VALID_URL, url)
101 video_id = mobj.group('id')
102 event_name = mobj.group('event_name')
103 webpage = self._download_webpage(url, video_id or event_name)
104
105 if video_id is None:
106 # This is an event page:
c5469e04
S
107 config_json = self._search_regex(
108 r'window.config = ({.*?});', webpage, 'window config')
5f1ea943 109 info = json.loads(config_json)['event']
b4444d5c 110 videos = [self._extract_video_info(video_data['data'])
72e785f3
PH
111 for video_data in info['feed']['data']
112 if video_data['type'] == 'video']
b4444d5c
JMF
113 return self.playlist_result(videos, info['id'], info['full_name'])
114 else:
c5469e04 115 og_video = self._og_search_video_url(webpage, 'player url')
b4444d5c
JMF
116 query_str = compat_urllib_parse_urlparse(og_video).query
117 query = compat_urlparse.parse_qs(query_str)
118 api_url = query['play_url'][0].replace('.smil', '')
c5469e04
S
119 info = json.loads(self._download_webpage(
120 api_url, video_id, 'Downloading video info'))
b4444d5c 121 return self._extract_video_info(info)
c66d2baa
JMF
122
123
124# The original version of Livestream uses a different system
125class LivestreamOriginalIE(InfoExtractor):
c5469e04 126 IE_NAME = 'livestream:original'
78338f71
JMF
127 _VALID_URL = r'''(?x)https?://www\.livestream\.com/
128 (?P<user>[^/]+)/(?P<type>video|folder)
129 (?:\?.*?Id=|/)(?P<id>.*?)(&|$)
130 '''
c66d2baa 131 _TEST = {
c5469e04
S
132 'url': 'http://www.livestream.com/dealbook/video?clipId=pla_8aa4a3f1-ba15-46a4-893b-902210e138fb',
133 'info_dict': {
134 'id': 'pla_8aa4a3f1-ba15-46a4-893b-902210e138fb',
135 'ext': 'flv',
136 'title': 'Spark 1 (BitCoin) with Cameron Winklevoss & Tyler Winklevoss of Winklevoss Capital',
c66d2baa 137 },
c5469e04 138 'params': {
c66d2baa 139 # rtmp
c5469e04 140 'skip_download': True,
c66d2baa
JMF
141 },
142 }
143
78338f71 144 def _extract_video(self, user, video_id):
c66d2baa
JMF
145 api_url = 'http://x{0}x.api.channel.livestream.com/2.0/clipdetails?extendedInfo=true&id={1}'.format(user, video_id)
146
e26f8712 147 info = self._download_xml(api_url, video_id)
c66d2baa
JMF
148 item = info.find('channel').find('item')
149 ns = {'media': 'http://search.yahoo.com/mrss'}
150 thumbnail_url = item.find(xpath_with_ns('media:thumbnail', ns)).attrib['url']
151 # Remove the extension and number from the path (like 1.jpg)
c5469e04 152 path = self._search_regex(r'(user-files/.+)_.*?\.jpg$', thumbnail_url, 'path')
c66d2baa
JMF
153
154 return {
155 'id': video_id,
156 'title': item.find('title').text,
157 'url': 'rtmp://extondemand.livestream.com/ondemand',
158 'play_path': 'mp4:trans/dv15/mogulus-{0}.mp4'.format(path),
159 'ext': 'flv',
160 'thumbnail': thumbnail_url,
161 }
78338f71
JMF
162
163 def _extract_folder(self, url, folder_id):
164 webpage = self._download_webpage(url, folder_id)
165 urls = orderedSet(re.findall(r'<a href="(https?://livestre\.am/.*?)"', webpage))
166
167 return {
168 '_type': 'playlist',
169 'id': folder_id,
170 'entries': [{
171 '_type': 'url',
172 'url': video_url,
173 } for video_url in urls],
174 }
175
176 def _real_extract(self, url):
177 mobj = re.match(self._VALID_URL, url)
178 id = mobj.group('id')
179 user = mobj.group('user')
180 url_type = mobj.group('type')
181 if url_type == 'folder':
182 return self._extract_folder(url, id)
183 else:
184 return self._extract_video(user, id)
185
186
187# The server doesn't support HEAD request, the generic extractor can't detect
188# the redirection
189class LivestreamShortenerIE(InfoExtractor):
190 IE_NAME = 'livestream:shortener'
191 IE_DESC = False # Do not list
192 _VALID_URL = r'https?://livestre\.am/(?P<id>.+)'
193
194 def _real_extract(self, url):
195 mobj = re.match(self._VALID_URL, url)
196 id = mobj.group('id')
197 webpage = self._download_webpage(url, id)
198
199 return {
200 '_type': 'url',
201 'url': self._og_search_url(webpage),
202 }