Merge branch 'vgtv' of https://github.com/mrkolby/youtube-dl into mrkolby-vgtv
[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     get_meta_content,
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         video_url = 'http://tcdn.ustream.tv/video/%s' % video_id
44         webpage = self._download_webpage(url, video_id)
45
46         self.report_extraction(video_id)
47
48         video_title = self._html_search_regex(r'data-title="(?P<title>.+)"',
49             webpage, 'title')
50
51         uploader = self._html_search_regex(r'data-content-type="channel".*?>(?P<uploader>.*?)</a>',
52             webpage, 'uploader', fatal=False, flags=re.DOTALL)
53
54         thumbnail = self._html_search_regex(r'<link rel="image_src" href="(?P<thumb>.*?)"',
55             webpage, 'thumbnail', fatal=False)
56
57         return {
58             'id': video_id,
59             'url': video_url,
60             'ext': 'flv',
61             'title': video_title,
62             'uploader': uploader,
63             'thumbnail': thumbnail,
64         }
65
66
67 class UstreamChannelIE(InfoExtractor):
68     _VALID_URL = r'https?://www\.ustream\.tv/channel/(?P<slug>.+)'
69     IE_NAME = 'ustream:channel'
70     _TEST = {
71         'url': 'http://www.ustream.tv/channel/channeljapan',
72         'info_dict': {
73             'id': '10874166',
74         },
75         'playlist_mincount': 54,
76     }
77
78     def _real_extract(self, url):
79         m = re.match(self._VALID_URL, url)
80         display_id = m.group('slug')
81         webpage = self._download_webpage(url, display_id)
82         channel_id = get_meta_content('ustream:channel_id', webpage)
83
84         BASE = 'http://www.ustream.tv'
85         next_url = '/ajax/socialstream/videos/%s/1.json' % channel_id
86         video_ids = []
87         while next_url:
88             reply = self._download_json(
89                 compat_urlparse.urljoin(BASE, next_url), display_id,
90                 note='Downloading video information (next: %d)' % (len(video_ids) + 1))
91             video_ids.extend(re.findall(r'data-content-id="(\d.*)"', reply['data']))
92             next_url = reply['nextUrl']
93
94         entries = [
95             self.url_result('http://www.ustream.tv/recorded/' + vid, 'Ustream')
96             for vid in video_ids]
97         return {
98             '_type': 'playlist',
99             'id': channel_id,
100             'display_id': display_id,
101             'entries': entries,
102         }