]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/ustream.py
[ustream] Fix typo
[yt-dlp.git] / youtube_dl / extractor / ustream.py
CommitLineData
0eb799ba
JMF
1from __future__ import unicode_literals
2
78af8eb1
PH
3import re
4
5from .common import InfoExtractor
1cc79574 6from ..compat import (
74ac9bdd 7 compat_urlparse,
74ac9bdd 8)
5820c4a2
S
9from ..utils import (
10 ExtractorError,
11 int_or_none,
12 float_or_none,
13)
78af8eb1
PH
14
15
16class UstreamIE(InfoExtractor):
4853eb63 17 _VALID_URL = r'https?://www\.ustream\.tv/(?P<type>recorded|embed|embed/recorded)/(?P<id>\d+)'
0eb799ba 18 IE_NAME = 'ustream'
cd9fdccd 19 _TESTS = [{
0eb799ba 20 'url': 'http://www.ustream.tv/recorded/20274954',
0eb799ba
JMF
21 'md5': '088f151799e8f572f84eb62f17d73e5c',
22 'info_dict': {
9e875391
S
23 'id': '20274954',
24 'ext': 'flv',
25 'uploader': 'Young Americans for Liberty',
26 'title': 'Young Americans for Liberty February 7, 2012 2:28 AM',
0eb799ba 27 },
cd9fdccd
YCH
28 }, {
29 # From http://sportscanada.tv/canadagames/index.php/week2/figure-skating/444
30 # Title and uploader available only from params JSON
31 'url': 'http://www.ustream.tv/embed/recorded/59307601?ub=ff0000&lc=ff0000&oc=ffffff&uc=ffffff&v=3&wmode=direct',
32 'md5': '5a2abf40babeac9812ed20ae12d34e10',
33 'info_dict': {
34 'id': '59307601',
35 'ext': 'flv',
36 'title': '-CG11- Canada Games Figure Skating',
37 'uploader': 'sportscanadatv',
38 }
39 }]
78af8eb1
PH
40
41 def _real_extract(self, url):
42 m = re.match(self._VALID_URL, url)
4853eb63 43 video_id = m.group('id')
b702eceb 44
9e875391
S
45 # some sites use this embed format (see: http://github.com/rg3/youtube-dl/issues/2990)
46 if m.group('type') == 'embed/recorded':
4853eb63 47 video_id = m.group('id')
b702eceb 48 desktop_url = 'http://www.ustream.tv/recorded/' + video_id
49 return self.url_result(desktop_url, 'Ustream')
5c386252 50 if m.group('type') == 'embed':
4853eb63 51 video_id = m.group('id')
5c386252 52 webpage = self._download_webpage(url, video_id)
9e875391
S
53 desktop_video_id = self._html_search_regex(
54 r'ContentVideoIds=\["([^"]*?)"\]', webpage, 'desktop_video_id')
5c386252 55 desktop_url = 'http://www.ustream.tv/recorded/' + desktop_video_id
56 return self.url_result(desktop_url, 'Ustream')
57
4853eb63
S
58 params = self._download_json(
59 'https://api.ustream.tv/videos/%s.json' % video_id, video_id)
f8610ba1 60
5820c4a2
S
61 error = params.get('error')
62 if error:
63 raise ExtractorError(
64 '%s returned error: %s' % (self.IE_NAME, error), expected=True)
78af8eb1 65
5820c4a2 66 video = params['video']
2a813727 67
41db7333
S
68 title = video['title']
69 filesize = float_or_none(video.get('file_size'))
70
5820c4a2 71 formats = [{
dc5756fd 72 'id': video_id,
5820c4a2
S
73 'url': video_url,
74 'ext': format_id,
41db7333 75 'filesize': filesize,
5820c4a2
S
76 } for format_id, video_url in video['media_urls'].items()]
77 self._sort_formats(formats)
78af8eb1 78
5820c4a2
S
79 description = video.get('description')
80 timestamp = int_or_none(video.get('created_at'))
81 duration = float_or_none(video.get('length'))
5820c4a2 82 view_count = int_or_none(video.get('views'))
cd9fdccd 83
5820c4a2
S
84 uploader = video.get('owner', {}).get('username')
85 uploader_id = video.get('owner', {}).get('id')
78af8eb1 86
5820c4a2
S
87 thumbnails = [{
88 'id': thumbnail_id,
89 'url': thumbnail_url,
90 } for thumbnail_id, thumbnail_url in video.get('thumbnail', {}).items()]
0eb799ba
JMF
91
92 return {
93 'id': video_id,
5820c4a2
S
94 'title': title,
95 'description': description,
96 'thumbnails': thumbnails,
97 'timestamp': timestamp,
98 'duration': duration,
5820c4a2 99 'view_count': view_count,
0eb799ba 100 'uploader': uploader,
5820c4a2
S
101 'uploader_id': uploader_id,
102 'formats': formats,
0eb799ba
JMF
103 }
104
bfd5c93a 105
bfd5c93a 106class UstreamChannelIE(InfoExtractor):
107 _VALID_URL = r'https?://www\.ustream\.tv/channel/(?P<slug>.+)'
0eb799ba 108 IE_NAME = 'ustream:channel'
22a6f150
PH
109 _TEST = {
110 'url': 'http://www.ustream.tv/channel/channeljapan',
111 'info_dict': {
112 'id': '10874166',
113 },
446a03bd 114 'playlist_mincount': 17,
22a6f150 115 }
bfd5c93a 116
117 def _real_extract(self, url):
118 m = re.match(self._VALID_URL, url)
22a6f150
PH
119 display_id = m.group('slug')
120 webpage = self._download_webpage(url, display_id)
44789f24 121 channel_id = self._html_search_meta('ustream:channel_id', webpage)
bfd5c93a 122
bfd5c93a 123 BASE = 'http://www.ustream.tv'
124 next_url = '/ajax/socialstream/videos/%s/1.json' % channel_id
a921f407 125 video_ids = []
bfd5c93a 126 while next_url:
22a6f150
PH
127 reply = self._download_json(
128 compat_urlparse.urljoin(BASE, next_url), display_id,
129 note='Downloading video information (next: %d)' % (len(video_ids) + 1))
a921f407 130 video_ids.extend(re.findall(r'data-content-id="(\d.*)"', reply['data']))
bfd5c93a 131 next_url = reply['nextUrl']
bfd5c93a 132
22a6f150
PH
133 entries = [
134 self.url_result('http://www.ustream.tv/recorded/' + vid, 'Ustream')
135 for vid in video_ids]
136 return {
137 '_type': 'playlist',
138 'id': channel_id,
139 'display_id': display_id,
140 'entries': entries,
141 }