1 from HTMLParser import HTMLParser
4 from urlparse import urljoin
6 from .common import InfoExtractor
9 class UstreamIE(InfoExtractor):
10 _VALID_URL = r'https?://www\.ustream\.tv/recorded/(?P<videoID>\d+)'
13 u'url': u'http://www.ustream.tv/recorded/20274954',
14 u'file': u'20274954.flv',
15 u'md5': u'088f151799e8f572f84eb62f17d73e5c',
17 u"uploader": u"Young Americans for Liberty",
18 u"title": u"Young Americans for Liberty February 7, 2012 2:28 AM"
22 def _real_extract(self, url):
23 m = re.match(self._VALID_URL, url)
24 video_id = m.group('videoID')
26 video_url = u'http://tcdn.ustream.tv/video/%s' % video_id
27 webpage = self._download_webpage(url, video_id)
29 self.report_extraction(video_id)
31 video_title = self._html_search_regex(r'data-title="(?P<title>.+)"',
34 uploader = self._html_search_regex(r'data-content-type="channel".*?>(?P<uploader>.*?)</a>',
35 webpage, u'uploader', fatal=False, flags=re.DOTALL)
37 thumbnail = self._html_search_regex(r'<link rel="image_src" href="(?P<thumb>.*?)"',
38 webpage, u'thumbnail', fatal=False)
46 'thumbnail': thumbnail,
50 # More robust than regular expressions
52 class ChannelParser(HTMLParser):
54 <meta name="ustream:channel_id" content="1234">
58 def handle_starttag(self, tag, attrs):
62 if values.get('name') != 'ustream:channel_id':
64 value = values.get('content', '')
66 self.channel_id = value
68 class SocialstreamParser(HTMLParser):
70 <li class="content123 video" data-content-id="123" data-length="1452"
71 data-href="/recorded/123" data-og-url="/recorded/123">
74 HTMLParser.__init__(self)
77 def handle_starttag(self, tag, attrs):
80 for (attr, value) in attrs:
81 if attr == 'data-content-id' and value.isdigit():
82 self.content_ids.append(value)
84 class UstreamChannelIE(InfoExtractor):
85 _VALID_URL = r'https?://www\.ustream\.tv/channel/(?P<slug>.+)'
86 IE_NAME = u'ustream:channel'
88 def _real_extract(self, url):
89 m = re.match(self._VALID_URL, url)
90 slug = m.group('slug')
91 # Slugs can be non-ascii, but youtube-dl can't handle non-ascii command lines,
92 # so if we got this far it's probably percent encoded and we needn't worry.
95 p.feed(self._download_webpage(url, slug))
97 channel_id = p.channel_id
99 p = SocialstreamParser()
100 BASE = 'http://www.ustream.tv'
101 next_url = '/ajax/socialstream/videos/%s/1.json' % channel_id
103 reply = json.loads(self._download_webpage(urljoin(BASE, next_url), channel_id))
104 p.feed(reply['data'])
105 next_url = reply['nextUrl']
107 video_ids = p.content_ids
109 # From YoutubeChannelIE
111 self._downloader.to_screen(u'[ustream] Channel %s: Found %i videos' % (channel_id, len(video_ids)))
113 urls = ['http://www.ustream.tv/recorded/' + vid for vid in video_ids]
114 url_entries = [self.url_result(eurl, 'Ustream') for eurl in urls]
115 return [self.playlist_result(url_entries, channel_id)]