1 from __future__
import unicode_literals
6 from .common
import InfoExtractor
21 class UstreamIE(InfoExtractor
):
22 _VALID_URL
= r
'https?://(?:www\.)?(?:ustream\.tv|video\.ibm\.com)/(?P<type>recorded|embed|embed/recorded)/(?P<id>\d+)'
25 'url': 'http://www.ustream.tv/recorded/20274954',
26 'md5': '088f151799e8f572f84eb62f17d73e5c',
30 'title': 'Young Americans for Liberty February 7, 2012 2:28 AM',
31 'description': 'Young Americans for Liberty February 7, 2012 2:28 AM',
32 'timestamp': 1328577035,
33 'upload_date': '20120207',
34 'uploader': 'yaliberty',
35 'uploader_id': '6780869',
38 # From http://sportscanada.tv/canadagames/index.php/week2/figure-skating/444
39 # Title and uploader available only from params JSON
40 'url': 'http://www.ustream.tv/embed/recorded/59307601?ub=ff0000&lc=ff0000&oc=ffffff&uc=ffffff&v=3&wmode=direct',
41 'md5': '5a2abf40babeac9812ed20ae12d34e10',
45 'title': '-CG11- Canada Games Figure Skating',
46 'uploader': 'sportscanadatv',
48 'skip': 'This Pro Broadcaster has chosen to remove this video from the ustream.tv site.',
50 'url': 'http://www.ustream.tv/embed/10299409',
56 'url': 'http://www.ustream.tv/recorded/91343263',
60 'title': 'GitHub Universe - General Session - Day 1',
61 'upload_date': '20160914',
62 'description': 'GitHub Universe - General Session - Day 1',
63 'timestamp': 1473872730,
64 'uploader': 'wa0dnskeqkr',
65 'uploader_id': '38977840',
68 'skip_download': True, # m3u8 download
71 'url': 'https://video.ibm.com/embed/recorded/128240221?&autoplay=true&controls=true&volume=100',
72 'only_matching': True,
76 def _extract_url(webpage
):
78 r
'<iframe[^>]+?src=(["\'])(?P
<url
>https?
://(?
:www\
.)?
(?
:ustream\
.tv|video\
.ibm\
.com
)/embed
/.+?
)\
1', webpage)
80 return mobj.group('url
')
82 def _get_stream_info(self, url, video_id, app_id_ver, extra_note=None):
86 rnd = random.randrange
91 conn_info = self._download_json(
92 'http
://r
%d-1-%s-recorded-lp
-live
.ums
.ustream
.tv
/1/ustream
' % (rnd(1e8), video_id),
93 video_id, note='Downloading connection info
' + extra_note,
96 'appId
': app_id_ver[0],
97 'appVersion
': app_id_ver[1],
98 'rsid
': '%s:%s' % (num_to_hex(rnd(1e8)), num_to_hex(rnd(1e8))),
99 'rpin
': '_rpin
.%d' % rnd(1e15),
102 'application
': 'recorded
',
104 host = conn_info[0]['args
'][0]['host
']
105 connection_id = conn_info[0]['args
'][0]['connectionId
']
107 return self._download_json(
108 'http
://%s/1/ustream?connectionId
=%s' % (host, connection_id),
109 video_id, note='Downloading stream info
' + extra_note)
111 def _get_streams(self, url, video_id, app_id_ver):
112 # Sometimes the return dict does not have 'stream
'
113 for trial_count in range(3):
114 stream_info = self._get_stream_info(
115 url, video_id, app_id_ver,
116 extra_note=' (try %d)' % (trial_count + 1) if trial_count > 0 else '')
117 if 'stream
' in stream_info[0]['args
'][0]:
118 return stream_info[0]['args
'][0]['stream
']
121 def _parse_segmented_mp4(self, dash_stream_info):
122 def resolve_dash_template(template, idx, chunk_hash):
123 return template.replace('%', compat_str(idx), 1).replace('%', chunk_hash)
126 for stream in dash_stream_info['streams
']:
127 # Use only one provider to avoid too many formats
128 provider = dash_stream_info['providers
'][0]
130 'url
': resolve_dash_template(
131 provider['url
'] + stream['initUrl
'], 0, dash_stream_info['hashes
']['0'])
133 for idx in range(dash_stream_info['videoLength
'] // dash_stream_info['chunkTime
']):
135 'url
': resolve_dash_template(
136 provider['url
'] + stream['segmentUrl
'], idx,
137 dash_stream_info['hashes
'][compat_str(idx // 10 * 10)])
139 content_type = stream['contentType
']
140 kind = content_type.split('/')[0]
142 'format_id
': '-'.join(filter(None, [
143 'dash
', kind, str_or_none(stream.get('bitrate
'))])),
144 'protocol
': 'http_dash_segments
',
145 # TODO: generate a MPD doc for external players?
146 'url
': encode_data_uri(b'<MPD
/>', 'text
/xml
'),
147 'ext
': mimetype2ext(content_type),
148 'height
': stream.get('height
'),
149 'width
': stream.get('width
'),
150 'fragments
': fragments,
154 'vcodec
': stream.get('codec
'),
156 'vbr
': stream.get('bitrate
'),
161 'acodec
': stream.get('codec
'),
162 'abr
': stream.get('bitrate
'),
167 def _real_extract(self, url):
168 m = re.match(self._VALID_URL, url)
169 video_id = m.group('id')
171 # some sites use this embed format (see: https://github.com/ytdl-org/youtube-dl/issues/2990)
172 if m.group('type') == 'embed
/recorded
':
173 video_id = m.group('id')
174 desktop_url = 'http
://www
.ustream
.tv
/recorded
/' + video_id
175 return self.url_result(desktop_url, 'Ustream
')
176 if m.group('type') == 'embed
':
177 video_id = m.group('id')
178 webpage = self._download_webpage(url, video_id)
179 content_video_ids = self._parse_json(self._search_regex(
180 r'ustream\
.vars\
.offAirContentVideoIds
=([^
;]+);', webpage,
181 'content video IDs
'), video_id)
182 return self.playlist_result(
183 map(lambda u: self.url_result('http
://www
.ustream
.tv
/recorded
/' + u, 'Ustream
'), content_video_ids),
186 params = self._download_json(
187 'https
://api
.ustream
.tv
/videos
/%s.json
' % video_id, video_id)
189 error = params.get('error
')
191 raise ExtractorError(
192 '%s returned error
: %s' % (self.IE_NAME, error), expected=True)
194 video = params['video
']
196 title = video['title
']
197 filesize = float_or_none(video.get('file_size
'))
203 'filesize
': filesize,
204 } for format_id, video_url in video['media_urls
'].items() if video_url]
207 hls_streams = self._get_streams(url, video_id, app_id_ver=(11, 2))
209 # m3u8_native leads to intermittent ContentTooShortError
210 formats.extend(self._extract_m3u8_formats(
211 hls_streams[0]['url
'], video_id, ext='mp4
', m3u8_id='hls
'))
214 # DASH streams handling is incomplete as 'url
' is missing
215 dash_streams = self._get_streams(url, video_id, app_id_ver=(3, 1))
217 formats.extend(self._parse_segmented_mp4(dash_streams))
220 self._sort_formats(formats)
222 description = video.get('description
')
223 timestamp = int_or_none(video.get('created_at
'))
224 duration = float_or_none(video.get('length
'))
225 view_count = int_or_none(video.get('views
'))
227 uploader = video.get('owner
', {}).get('username
')
228 uploader_id = video.get('owner
', {}).get('id')
232 'url
': thumbnail_url,
233 } for thumbnail_id, thumbnail_url in video.get('thumbnail
', {}).items()]
238 'description
': description,
239 'thumbnails
': thumbnails,
240 'timestamp
': timestamp,
241 'duration
': duration,
242 'view_count
': view_count,
243 'uploader
': uploader,
244 'uploader_id
': uploader_id,
249 class UstreamChannelIE(InfoExtractor):
250 _VALID_URL = r'https?
://(?
:www\
.)?ustream\
.tv
/channel
/(?P
<slug
>.+)'
251 IE_NAME = 'ustream
:channel
'
253 'url
': 'http
://www
.ustream
.tv
/channel
/channeljapan
',
257 'playlist_mincount
': 17,
260 def _real_extract(self, url):
261 m = re.match(self._VALID_URL, url)
262 display_id = m.group('slug
')
263 webpage = self._download_webpage(url, display_id)
264 channel_id = self._html_search_meta('ustream
:channel_id
', webpage)
266 BASE = 'http
://www
.ustream
.tv
'
267 next_url = '/ajax
/socialstream
/videos
/%s/1.json
' % channel_id
270 reply = self._download_json(
271 compat_urlparse.urljoin(BASE, next_url), display_id,
272 note='Downloading video
information (next
: %d)' % (len(video_ids) + 1))
273 video_ids.extend(re.findall(r'data
-content
-id="(\d.*)"', reply['data
']))
274 next_url = reply['nextUrl
']
277 self.url_result('http
://www
.ustream
.tv
/recorded
/' + vid, 'Ustream
')
278 for vid in video_ids]
282 'display_id
': display_id,