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