Add Ustream channel support
[youtube-dl] / youtube_dl / extractor / ustream.py
1 from HTMLParser import HTMLParser
2 import json
3 import re
4 from urlparse import urljoin
5
6 from .common import InfoExtractor
7
8
9 class UstreamIE(InfoExtractor):
10     _VALID_URL = r'https?://www\.ustream\.tv/recorded/(?P<videoID>\d+)'
11     IE_NAME = u'ustream'
12     _TEST = {
13         u'url': u'http://www.ustream.tv/recorded/20274954',
14         u'file': u'20274954.flv',
15         u'md5': u'088f151799e8f572f84eb62f17d73e5c',
16         u'info_dict': {
17             u"uploader": u"Young Americans for Liberty", 
18             u"title": u"Young Americans for Liberty February 7, 2012 2:28 AM"
19         }
20     }
21
22     def _real_extract(self, url):
23         m = re.match(self._VALID_URL, url)
24         video_id = m.group('videoID')
25
26         video_url = u'http://tcdn.ustream.tv/video/%s' % video_id
27         webpage = self._download_webpage(url, video_id)
28
29         self.report_extraction(video_id)
30
31         video_title = self._html_search_regex(r'data-title="(?P<title>.+)"',
32             webpage, u'title')
33
34         uploader = self._html_search_regex(r'data-content-type="channel".*?>(?P<uploader>.*?)</a>',
35             webpage, u'uploader', fatal=False, flags=re.DOTALL)
36
37         thumbnail = self._html_search_regex(r'<link rel="image_src" href="(?P<thumb>.*?)"',
38             webpage, u'thumbnail', fatal=False)
39
40         info = {
41                 'id': video_id,
42                 'url': video_url,
43                 'ext': 'flv',
44                 'title': video_title,
45                 'uploader': uploader,
46                 'thumbnail': thumbnail,
47                }
48         return info
49
50 # More robust than regular expressions
51
52 class ChannelParser(HTMLParser):
53     """
54     <meta name="ustream:channel_id" content="1234">
55     """
56     channel_id = None
57
58     def handle_starttag(self, tag, attrs):
59         if tag != 'meta':
60             return
61         values = dict(attrs)
62         if values.get('name') != 'ustream:channel_id':
63             return
64         value = values.get('content', '')
65         if value.isdigit():
66             self.channel_id = value
67
68 class SocialstreamParser(HTMLParser):
69     """
70     <li class="content123 video" data-content-id="123" data-length="1452"
71         data-href="/recorded/123" data-og-url="/recorded/123">
72     """
73     def __init__(self):
74         HTMLParser.__init__(self)
75         self.content_ids = []
76
77     def handle_starttag(self, tag, attrs):
78         if tag != 'li':
79             return
80         for (attr, value) in attrs:
81             if attr == 'data-content-id' and value.isdigit():
82                 self.content_ids.append(value)
83
84 class UstreamChannelIE(InfoExtractor):
85     _VALID_URL = r'https?://www\.ustream\.tv/channel/(?P<slug>.+)'
86     IE_NAME = u'ustream:channel'
87
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.
93
94         p = ChannelParser()
95         p.feed(self._download_webpage(url, slug))
96         p.close()
97         channel_id = p.channel_id
98
99         p = SocialstreamParser()
100         BASE = 'http://www.ustream.tv'
101         next_url = '/ajax/socialstream/videos/%s/1.json' % channel_id
102         while next_url:
103             reply = json.loads(self._download_webpage(urljoin(BASE, next_url), channel_id))
104             p.feed(reply['data'])
105             next_url = reply['nextUrl']
106         p.close()
107         video_ids = p.content_ids
108
109         # From YoutubeChannelIE
110
111         self._downloader.to_screen(u'[ustream] Channel %s: Found %i videos' % (channel_id, len(video_ids)))
112
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)]