[ustream] Simplify channel extraction
[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     get_meta_content,
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 class UstreamChannelIE(InfoExtractor):
53     _VALID_URL = r'https?://www\.ustream\.tv/channel/(?P<slug>.+)'
54     IE_NAME = u'ustream:channel'
55
56     def _real_extract(self, url):
57         m = re.match(self._VALID_URL, url)
58         slug = m.group('slug')
59         webpage = self._download_webpage(url, slug)
60         channel_id = get_meta_content('ustream:channel_id', webpage)
61
62         BASE = 'http://www.ustream.tv'
63         next_url = '/ajax/socialstream/videos/%s/1.json' % channel_id
64         video_ids = []
65         while next_url:
66             reply = json.loads(self._download_webpage(compat_urlparse.urljoin(BASE, next_url), channel_id))
67             video_ids.extend(re.findall(r'data-content-id="(\d.*)"', reply['data']))
68             next_url = reply['nextUrl']
69
70         urls = ['http://www.ustream.tv/recorded/' + vid for vid in video_ids]
71         url_entries = [self.url_result(eurl, 'Ustream') for eurl in urls]
72         return self.playlist_result(url_entries, channel_id)