]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/livestream.py
5247c6f58500e301dab50ed48039df0c070b493a
[youtubedl] / youtube_dl / extractor / livestream.py
1 from __future__ import unicode_literals
2
3 import re
4 import json
5
6 from .common import InfoExtractor
7 from ..compat import (
8 compat_str,
9 compat_urllib_parse_urlparse,
10 compat_urlparse,
11 )
12 from ..utils import (
13 ExtractorError,
14 find_xpath_attr,
15 int_or_none,
16 orderedSet,
17 xpath_with_ns,
18 )
19
20
21 class LivestreamIE(InfoExtractor):
22 IE_NAME = 'livestream'
23 _VALID_URL = r'https?://new\.livestream\.com/.*?/(?P<event_name>.*?)(/videos/(?P<id>[0-9]+)(?:/player)?)?/?(?:$|[?#])'
24 _TESTS = [{
25 'url': 'http://new.livestream.com/CoheedandCambria/WebsterHall/videos/4719370',
26 'md5': '53274c76ba7754fb0e8d072716f2292b',
27 'info_dict': {
28 'id': '4719370',
29 'ext': 'mp4',
30 'title': 'Live from Webster Hall NYC',
31 'upload_date': '20121012',
32 'like_count': int,
33 'view_count': int,
34 'thumbnail': 're:^http://.*\.jpg$'
35 }
36 }, {
37 'url': 'http://new.livestream.com/tedx/cityenglish',
38 'info_dict': {
39 'title': 'TEDCity2.0 (English)',
40 },
41 'playlist_mincount': 4,
42 }, {
43 'url': 'https://new.livestream.com/accounts/362/events/3557232/videos/67864563/player?autoPlay=false&height=360&mute=false&width=640',
44 'only_matching': True,
45 }]
46
47 def _parse_smil(self, video_id, smil_url):
48 formats = []
49 _SWITCH_XPATH = (
50 './/{http://www.w3.org/2001/SMIL20/Language}body/'
51 '{http://www.w3.org/2001/SMIL20/Language}switch')
52 smil_doc = self._download_xml(
53 smil_url, video_id,
54 note='Downloading SMIL information',
55 errnote='Unable to download SMIL information',
56 fatal=False)
57 if smil_doc is False: # Download failed
58 return formats
59 title_node = find_xpath_attr(
60 smil_doc, './/{http://www.w3.org/2001/SMIL20/Language}meta',
61 'name', 'title')
62 if title_node is None:
63 self.report_warning('Cannot find SMIL id')
64 switch_node = smil_doc.find(_SWITCH_XPATH)
65 else:
66 title_id = title_node.attrib['content']
67 switch_node = find_xpath_attr(
68 smil_doc, _SWITCH_XPATH, 'id', title_id)
69 if switch_node is None:
70 raise ExtractorError('Cannot find switch node')
71 video_nodes = switch_node.findall(
72 '{http://www.w3.org/2001/SMIL20/Language}video')
73
74 for vn in video_nodes:
75 tbr = int_or_none(vn.attrib.get('system-bitrate'))
76 furl = (
77 'http://livestream-f.akamaihd.net/%s?v=3.0.3&fp=WIN%%2014,0,0,145' %
78 (vn.attrib['src']))
79 if 'clipBegin' in vn.attrib:
80 furl += '&ssek=' + vn.attrib['clipBegin']
81 formats.append({
82 'url': furl,
83 'format_id': 'smil_%d' % tbr,
84 'ext': 'flv',
85 'tbr': tbr,
86 'preference': -1000,
87 })
88 return formats
89
90 def _extract_video_info(self, video_data):
91 video_id = compat_str(video_data['id'])
92
93 FORMAT_KEYS = (
94 ('sd', 'progressive_url'),
95 ('hd', 'progressive_url_hd'),
96 )
97 formats = [{
98 'format_id': format_id,
99 'url': video_data[key],
100 'quality': i + 1,
101 } for i, (format_id, key) in enumerate(FORMAT_KEYS)
102 if video_data.get(key)]
103
104 smil_url = video_data.get('smil_url')
105 if smil_url:
106 formats.extend(self._parse_smil(video_id, smil_url))
107 self._sort_formats(formats)
108
109 return {
110 'id': video_id,
111 'formats': formats,
112 'title': video_data['caption'],
113 'thumbnail': video_data.get('thumbnail_url'),
114 'upload_date': video_data['updated_at'].replace('-', '')[:8],
115 'like_count': video_data.get('likes', {}).get('total'),
116 'view_count': video_data.get('views'),
117 }
118
119 def _real_extract(self, url):
120 mobj = re.match(self._VALID_URL, url)
121 video_id = mobj.group('id')
122 event_name = mobj.group('event_name')
123 webpage = self._download_webpage(url, video_id or event_name)
124
125 og_video = self._og_search_video_url(
126 webpage, 'player url', fatal=False, default=None)
127 if og_video is not None:
128 query_str = compat_urllib_parse_urlparse(og_video).query
129 query = compat_urlparse.parse_qs(query_str)
130 if 'play_url' in query:
131 api_url = query['play_url'][0].replace('.smil', '')
132 info = json.loads(self._download_webpage(
133 api_url, video_id, 'Downloading video info'))
134 return self._extract_video_info(info)
135
136 config_json = self._search_regex(
137 r'window.config = ({.*?});', webpage, 'window config')
138 info = json.loads(config_json)['event']
139
140 def is_relevant(vdata, vid):
141 result = vdata['type'] == 'video'
142 if video_id is not None:
143 result = result and compat_str(vdata['data']['id']) == vid
144 return result
145
146 videos = [self._extract_video_info(video_data['data'])
147 for video_data in info['feed']['data']
148 if is_relevant(video_data, video_id)]
149 if video_id is None:
150 # This is an event page:
151 return self.playlist_result(videos, info['id'], info['full_name'])
152 else:
153 if not videos:
154 raise ExtractorError('Cannot find video %s' % video_id)
155 return videos[0]
156
157
158 # The original version of Livestream uses a different system
159 class LivestreamOriginalIE(InfoExtractor):
160 IE_NAME = 'livestream:original'
161 _VALID_URL = r'''(?x)https?://www\.livestream\.com/
162 (?P<user>[^/]+)/(?P<type>video|folder)
163 (?:\?.*?Id=|/)(?P<id>.*?)(&|$)
164 '''
165 _TESTS = [{
166 'url': 'http://www.livestream.com/dealbook/video?clipId=pla_8aa4a3f1-ba15-46a4-893b-902210e138fb',
167 'info_dict': {
168 'id': 'pla_8aa4a3f1-ba15-46a4-893b-902210e138fb',
169 'ext': 'flv',
170 'title': 'Spark 1 (BitCoin) with Cameron Winklevoss & Tyler Winklevoss of Winklevoss Capital',
171 },
172 'params': {
173 # rtmp
174 'skip_download': True,
175 },
176 }, {
177 'url': 'https://www.livestream.com/newplay/folder?dirId=a07bf706-d0e4-4e75-a747-b021d84f2fd3',
178 'info_dict': {
179 'id': 'a07bf706-d0e4-4e75-a747-b021d84f2fd3',
180 },
181 'playlist_mincount': 4,
182 }]
183
184 def _extract_video(self, user, video_id):
185 api_url = 'http://x{0}x.api.channel.livestream.com/2.0/clipdetails?extendedInfo=true&id={1}'.format(user, video_id)
186
187 info = self._download_xml(api_url, video_id)
188 item = info.find('channel').find('item')
189 ns = {'media': 'http://search.yahoo.com/mrss'}
190 thumbnail_url = item.find(xpath_with_ns('media:thumbnail', ns)).attrib['url']
191 # Remove the extension and number from the path (like 1.jpg)
192 path = self._search_regex(r'(user-files/.+)_.*?\.jpg$', thumbnail_url, 'path')
193
194 return {
195 'id': video_id,
196 'title': item.find('title').text,
197 'url': 'rtmp://extondemand.livestream.com/ondemand',
198 'play_path': 'trans/dv15/mogulus-{0}'.format(path),
199 'player_url': 'http://static.livestream.com/chromelessPlayer/v21/playerapi.swf?hash=5uetk&v=0803&classid=D27CDB6E-AE6D-11cf-96B8-444553540000&jsEnabled=false&wmode=opaque',
200 'ext': 'flv',
201 'thumbnail': thumbnail_url,
202 }
203
204 def _extract_folder(self, url, folder_id):
205 webpage = self._download_webpage(url, folder_id)
206 paths = orderedSet(re.findall(
207 r'''(?x)(?:
208 <li\s+class="folder">\s*<a\s+href="|
209 <a\s+href="(?=https?://livestre\.am/)
210 )([^"]+)"''', webpage))
211
212 return {
213 '_type': 'playlist',
214 'id': folder_id,
215 'entries': [{
216 '_type': 'url',
217 'url': compat_urlparse.urljoin(url, p),
218 } for p in paths],
219 }
220
221 def _real_extract(self, url):
222 mobj = re.match(self._VALID_URL, url)
223 id = mobj.group('id')
224 user = mobj.group('user')
225 url_type = mobj.group('type')
226 if url_type == 'folder':
227 return self._extract_folder(url, id)
228 else:
229 return self._extract_video(user, id)
230
231
232 # The server doesn't support HEAD request, the generic extractor can't detect
233 # the redirection
234 class LivestreamShortenerIE(InfoExtractor):
235 IE_NAME = 'livestream:shortener'
236 IE_DESC = False # Do not list
237 _VALID_URL = r'https?://livestre\.am/(?P<id>.+)'
238
239 def _real_extract(self, url):
240 mobj = re.match(self._VALID_URL, url)
241 id = mobj.group('id')
242 webpage = self._download_webpage(url, id)
243
244 return {
245 '_type': 'url',
246 'url': self._og_search_url(webpage),
247 }