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