]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/ustream.py
[viafree] Fix test
[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',
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.',
67d46a3f
YCH
44 }, {
45 'url': 'http://www.ustream.tv/embed/10299409',
46 'info_dict': {
47 'id': '10299409',
48 },
49 'playlist_count': 3,
cd9fdccd 50 }]
78af8eb1
PH
51
52 def _real_extract(self, url):
53 m = re.match(self._VALID_URL, url)
4853eb63 54 video_id = m.group('id')
b702eceb 55
e5a66240 56 # some sites use this embed format (see: https://github.com/rg3/youtube-dl/issues/2990)
9e875391 57 if m.group('type') == 'embed/recorded':
4853eb63 58 video_id = m.group('id')
b702eceb 59 desktop_url = 'http://www.ustream.tv/recorded/' + video_id
60 return self.url_result(desktop_url, 'Ustream')
5c386252 61 if m.group('type') == 'embed':
4853eb63 62 video_id = m.group('id')
5c386252 63 webpage = self._download_webpage(url, video_id)
67d46a3f
YCH
64 content_video_ids = self._parse_json(self._search_regex(
65 r'ustream\.vars\.offAirContentVideoIds=([^;]+);', webpage,
66 'content video IDs'), video_id)
67 return self.playlist_result(
68 map(lambda u: self.url_result('http://www.ustream.tv/recorded/' + u, 'Ustream'), content_video_ids),
69 video_id)
5c386252 70
4853eb63
S
71 params = self._download_json(
72 'https://api.ustream.tv/videos/%s.json' % video_id, video_id)
f8610ba1 73
5820c4a2
S
74 error = params.get('error')
75 if error:
76 raise ExtractorError(
77 '%s returned error: %s' % (self.IE_NAME, error), expected=True)
78af8eb1 78
5820c4a2 79 video = params['video']
2a813727 80
41db7333
S
81 title = video['title']
82 filesize = float_or_none(video.get('file_size'))
83
5820c4a2 84 formats = [{
dc5756fd 85 'id': video_id,
5820c4a2
S
86 'url': video_url,
87 'ext': format_id,
41db7333 88 'filesize': filesize,
5820c4a2
S
89 } for format_id, video_url in video['media_urls'].items()]
90 self._sort_formats(formats)
78af8eb1 91
5820c4a2
S
92 description = video.get('description')
93 timestamp = int_or_none(video.get('created_at'))
94 duration = float_or_none(video.get('length'))
5820c4a2 95 view_count = int_or_none(video.get('views'))
cd9fdccd 96
5820c4a2
S
97 uploader = video.get('owner', {}).get('username')
98 uploader_id = video.get('owner', {}).get('id')
78af8eb1 99
5820c4a2
S
100 thumbnails = [{
101 'id': thumbnail_id,
102 'url': thumbnail_url,
103 } for thumbnail_id, thumbnail_url in video.get('thumbnail', {}).items()]
0eb799ba
JMF
104
105 return {
106 'id': video_id,
5820c4a2
S
107 'title': title,
108 'description': description,
109 'thumbnails': thumbnails,
110 'timestamp': timestamp,
111 'duration': duration,
5820c4a2 112 'view_count': view_count,
0eb799ba 113 'uploader': uploader,
5820c4a2
S
114 'uploader_id': uploader_id,
115 'formats': formats,
0eb799ba
JMF
116 }
117
bfd5c93a 118
bfd5c93a 119class UstreamChannelIE(InfoExtractor):
120 _VALID_URL = r'https?://www\.ustream\.tv/channel/(?P<slug>.+)'
0eb799ba 121 IE_NAME = 'ustream:channel'
22a6f150
PH
122 _TEST = {
123 'url': 'http://www.ustream.tv/channel/channeljapan',
124 'info_dict': {
125 'id': '10874166',
126 },
446a03bd 127 'playlist_mincount': 17,
22a6f150 128 }
bfd5c93a 129
130 def _real_extract(self, url):
131 m = re.match(self._VALID_URL, url)
22a6f150
PH
132 display_id = m.group('slug')
133 webpage = self._download_webpage(url, display_id)
44789f24 134 channel_id = self._html_search_meta('ustream:channel_id', webpage)
bfd5c93a 135
bfd5c93a 136 BASE = 'http://www.ustream.tv'
137 next_url = '/ajax/socialstream/videos/%s/1.json' % channel_id
a921f407 138 video_ids = []
bfd5c93a 139 while next_url:
22a6f150
PH
140 reply = self._download_json(
141 compat_urlparse.urljoin(BASE, next_url), display_id,
142 note='Downloading video information (next: %d)' % (len(video_ids) + 1))
a921f407 143 video_ids.extend(re.findall(r'data-content-id="(\d.*)"', reply['data']))
bfd5c93a 144 next_url = reply['nextUrl']
bfd5c93a 145
22a6f150
PH
146 entries = [
147 self.url_result('http://www.ustream.tv/recorded/' + vid, 'Ustream')
148 for vid in video_ids]
149 return {
150 '_type': 'playlist',
151 'id': channel_id,
152 'display_id': display_id,
153 'entries': entries,
154 }