Merge pull request #1413 from tewe/master
[youtube-dl] / youtube_dl / extractor / ustream.py
1 import json
2 import re
3
4 from .common import InfoExtractor
5 from ..utils import (
6     compat_urlparse,
7     compat_html_parser,
8 )
9
10
11 class UstreamIE(InfoExtractor):
12     _VALID_URL = r'https?://www\.ustream\.tv/recorded/(?P<videoID>\d+)'
13     IE_NAME = u'ustream'
14     _TEST = {
15         u'url': u'http://www.ustream.tv/recorded/20274954',
16         u'file': u'20274954.flv',
17         u'md5': u'088f151799e8f572f84eb62f17d73e5c',
18         u'info_dict': {
19             u"uploader": u"Young Americans for Liberty", 
20             u"title": u"Young Americans for Liberty February 7, 2012 2:28 AM"
21         }
22     }
23
24     def _real_extract(self, url):
25         m = re.match(self._VALID_URL, url)
26         video_id = m.group('videoID')
27
28         video_url = u'http://tcdn.ustream.tv/video/%s' % video_id
29         webpage = self._download_webpage(url, video_id)
30
31         self.report_extraction(video_id)
32
33         video_title = self._html_search_regex(r'data-title="(?P<title>.+)"',
34             webpage, u'title')
35
36         uploader = self._html_search_regex(r'data-content-type="channel".*?>(?P<uploader>.*?)</a>',
37             webpage, u'uploader', fatal=False, flags=re.DOTALL)
38
39         thumbnail = self._html_search_regex(r'<link rel="image_src" href="(?P<thumb>.*?)"',
40             webpage, u'thumbnail', fatal=False)
41
42         info = {
43                 'id': video_id,
44                 'url': video_url,
45                 'ext': 'flv',
46                 'title': video_title,
47                 'uploader': uploader,
48                 'thumbnail': thumbnail,
49                }
50         return info
51
52 # More robust than regular expressions
53
54 class ChannelParser(compat_html_parser.HTMLParser):
55     """
56     <meta name="ustream:channel_id" content="1234">
57     """
58     channel_id = None
59
60     def handle_starttag(self, tag, attrs):
61         if tag != 'meta':
62             return
63         values = dict(attrs)
64         if values.get('name') != 'ustream:channel_id':
65             return
66         value = values.get('content', '')
67         if value.isdigit():
68             self.channel_id = value
69
70 class SocialstreamParser(compat_html_parser.HTMLParser):
71     """
72     <li class="content123 video" data-content-id="123" data-length="1452"
73         data-href="/recorded/123" data-og-url="/recorded/123">
74     """
75     def __init__(self):
76         compat_html_parser.HTMLParser.__init__(self)
77         self.content_ids = []
78
79     def handle_starttag(self, tag, attrs):
80         if tag != 'li':
81             return
82         for (attr, value) in attrs:
83             if attr == 'data-content-id' and value.isdigit():
84                 self.content_ids.append(value)
85
86 class UstreamChannelIE(InfoExtractor):
87     _VALID_URL = r'https?://www\.ustream\.tv/channel/(?P<slug>.+)'
88     IE_NAME = u'ustream:channel'
89
90     def _real_extract(self, url):
91         m = re.match(self._VALID_URL, url)
92         slug = m.group('slug')
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(compat_urlparse.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         urls = ['http://www.ustream.tv/recorded/' + vid for vid in video_ids]
110         url_entries = [self.url_result(eurl, 'Ustream') for eurl in urls]
111         return self.playlist_result(url_entries, channel_id)