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