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