[ustream] Remove unused import
[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 ..compat import (
7     compat_urlparse,
8 )
9 from ..utils import (
10     ExtractorError,
11     int_or_none,
12     float_or_none,
13 )
14
15
16 class UstreamIE(InfoExtractor):
17     _VALID_URL = r'https?://www\.ustream\.tv/(?P<type>recorded|embed|embed/recorded)/(?P<id>\d+)'
18     IE_NAME = 'ustream'
19     _TESTS = [{
20         'url': 'http://www.ustream.tv/recorded/20274954',
21         'md5': '088f151799e8f572f84eb62f17d73e5c',
22         'info_dict': {
23             'id': '20274954',
24             'ext': 'flv',
25             'uploader': 'Young Americans for Liberty',
26             'title': 'Young Americans for Liberty February 7, 2012 2:28 AM',
27         },
28     }, {
29         # From http://sportscanada.tv/canadagames/index.php/week2/figure-skating/444
30         # Title and uploader available only from params JSON
31         'url': 'http://www.ustream.tv/embed/recorded/59307601?ub=ff0000&lc=ff0000&oc=ffffff&uc=ffffff&v=3&wmode=direct',
32         'md5': '5a2abf40babeac9812ed20ae12d34e10',
33         'info_dict': {
34             'id': '59307601',
35             'ext': 'flv',
36             'title': '-CG11- Canada Games Figure Skating',
37             'uploader': 'sportscanadatv',
38         }
39     }]
40
41     def _real_extract(self, url):
42         m = re.match(self._VALID_URL, url)
43         video_id = m.group('id')
44
45         # some sites use this embed format (see: http://github.com/rg3/youtube-dl/issues/2990)
46         if m.group('type') == 'embed/recorded':
47             video_id = m.group('id')
48             desktop_url = 'http://www.ustream.tv/recorded/' + video_id
49             return self.url_result(desktop_url, 'Ustream')
50         if m.group('type') == 'embed':
51             video_id = m.group('id')
52             webpage = self._download_webpage(url, video_id)
53             desktop_video_id = self._html_search_regex(
54                 r'ContentVideoIds=\["([^"]*?)"\]', webpage, 'desktop_video_id')
55             desktop_url = 'http://www.ustream.tv/recorded/' + desktop_video_id
56             return self.url_result(desktop_url, 'Ustream')
57
58         params = self._download_json(
59             'https://api.ustream.tv/videos/%s.json' % video_id, video_id)
60
61         error = params.get('error')
62         if error:
63             raise ExtractorError(
64                 '%s returned error: %s' % (self.IE_NAME, error), expected=True)
65
66         video = params['video']
67
68         formats = [{
69             'id': format_id,
70             'url': video_url,
71             'ext': format_id,
72         } for format_id, video_url in video['media_urls'].items()]
73         self._sort_formats(formats)
74
75         title = video['title']
76         description = video.get('description')
77         timestamp = int_or_none(video.get('created_at'))
78         duration = float_or_none(video.get('length'))
79         filesize = float_or_none(video.get('file_size'))
80         view_count = int_or_none(video.get('views'))
81
82         uploader = video.get('owner', {}).get('username')
83         uploader_id = video.get('owner', {}).get('id')
84
85         thumbnails = [{
86             'id': thumbnail_id,
87             'url': thumbnail_url,
88         } for thumbnail_id, thumbnail_url in video.get('thumbnail', {}).items()]
89
90         return {
91             'id': video_id,
92             'title': title,
93             'description': description,
94             'thumbnails': thumbnails,
95             'timestamp': timestamp,
96             'duration': duration,
97             'filesize': filesize,
98             'view_count': view_count,
99             'uploader': uploader,
100             'uploader_id': uploader_id,
101             'formats': formats,
102         }
103
104
105 class UstreamChannelIE(InfoExtractor):
106     _VALID_URL = r'https?://www\.ustream\.tv/channel/(?P<slug>.+)'
107     IE_NAME = 'ustream:channel'
108     _TEST = {
109         'url': 'http://www.ustream.tv/channel/channeljapan',
110         'info_dict': {
111             'id': '10874166',
112         },
113         'playlist_mincount': 17,
114     }
115
116     def _real_extract(self, url):
117         m = re.match(self._VALID_URL, url)
118         display_id = m.group('slug')
119         webpage = self._download_webpage(url, display_id)
120         channel_id = self._html_search_meta('ustream:channel_id', webpage)
121
122         BASE = 'http://www.ustream.tv'
123         next_url = '/ajax/socialstream/videos/%s/1.json' % channel_id
124         video_ids = []
125         while next_url:
126             reply = self._download_json(
127                 compat_urlparse.urljoin(BASE, next_url), display_id,
128                 note='Downloading video information (next: %d)' % (len(video_ids) + 1))
129             video_ids.extend(re.findall(r'data-content-id="(\d.*)"', reply['data']))
130             next_url = reply['nextUrl']
131
132         entries = [
133             self.url_result('http://www.ustream.tv/recorded/' + vid, 'Ustream')
134             for vid in video_ids]
135         return {
136             '_type': 'playlist',
137             'id': channel_id,
138             'display_id': display_id,
139             'entries': entries,
140         }