PEP8: applied even more rules
[youtube-dl] / youtube_dl / extractor / ustream.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     compat_urlparse,
8 )
9
10
11 class UstreamIE(InfoExtractor):
12     _VALID_URL = r'https?://www\.ustream\.tv/(?P<type>recorded|embed|embed/recorded)/(?P<videoID>\d+)'
13     IE_NAME = 'ustream'
14     _TEST = {
15         'url': 'http://www.ustream.tv/recorded/20274954',
16         'md5': '088f151799e8f572f84eb62f17d73e5c',
17         'info_dict': {
18             'id': '20274954',
19             'ext': 'flv',
20             'uploader': 'Young Americans for Liberty',
21             'title': 'Young Americans for Liberty February 7, 2012 2:28 AM',
22         },
23     }
24
25     def _real_extract(self, url):
26         m = re.match(self._VALID_URL, url)
27         video_id = m.group('videoID')
28
29         # some sites use this embed format (see: http://github.com/rg3/youtube-dl/issues/2990)
30         if m.group('type') == 'embed/recorded':
31             video_id = m.group('videoID')
32             desktop_url = 'http://www.ustream.tv/recorded/' + video_id
33             return self.url_result(desktop_url, 'Ustream')
34         if m.group('type') == 'embed':
35             video_id = m.group('videoID')
36             webpage = self._download_webpage(url, video_id)
37             desktop_video_id = self._html_search_regex(
38                 r'ContentVideoIds=\["([^"]*?)"\]', webpage, 'desktop_video_id')
39             desktop_url = 'http://www.ustream.tv/recorded/' + desktop_video_id
40             return self.url_result(desktop_url, 'Ustream')
41
42         video_url = 'http://tcdn.ustream.tv/video/%s' % video_id
43         webpage = self._download_webpage(url, video_id)
44
45         self.report_extraction(video_id)
46
47         video_title = self._html_search_regex(r'data-title="(?P<title>.+)"',
48                                               webpage, 'title')
49
50         uploader = self._html_search_regex(r'data-content-type="channel".*?>(?P<uploader>.*?)</a>',
51                                            webpage, 'uploader', fatal=False, flags=re.DOTALL)
52
53         thumbnail = self._html_search_regex(r'<link rel="image_src" href="(?P<thumb>.*?)"',
54                                             webpage, 'thumbnail', fatal=False)
55
56         return {
57             'id': video_id,
58             'url': video_url,
59             'ext': 'flv',
60             'title': video_title,
61             'uploader': uploader,
62             'thumbnail': thumbnail,
63         }
64
65
66 class UstreamChannelIE(InfoExtractor):
67     _VALID_URL = r'https?://www\.ustream\.tv/channel/(?P<slug>.+)'
68     IE_NAME = 'ustream:channel'
69     _TEST = {
70         'url': 'http://www.ustream.tv/channel/channeljapan',
71         'info_dict': {
72             'id': '10874166',
73         },
74         'playlist_mincount': 17,
75     }
76
77     def _real_extract(self, url):
78         m = re.match(self._VALID_URL, url)
79         display_id = m.group('slug')
80         webpage = self._download_webpage(url, display_id)
81         channel_id = self._html_search_meta('ustream:channel_id', webpage)
82
83         BASE = 'http://www.ustream.tv'
84         next_url = '/ajax/socialstream/videos/%s/1.json' % channel_id
85         video_ids = []
86         while next_url:
87             reply = self._download_json(
88                 compat_urlparse.urljoin(BASE, next_url), display_id,
89                 note='Downloading video information (next: %d)' % (len(video_ids) + 1))
90             video_ids.extend(re.findall(r'data-content-id="(\d.*)"', reply['data']))
91             next_url = reply['nextUrl']
92
93         entries = [
94             self.url_result('http://www.ustream.tv/recorded/' + vid, 'Ustream')
95             for vid in video_ids]
96         return {
97             '_type': 'playlist',
98             'id': channel_id,
99             'display_id': display_id,
100             'entries': entries,
101         }