[UstreamIE] [generic] Added support for Ustream embed URLs (Fixes #2694)
[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 ..utils import (
8     compat_urlparse,
9     get_meta_content,
10 )
11
12
13 class UstreamIE(InfoExtractor):
14     _VALID_URL = r'https?://www\.ustream\.tv/(?P<type>recorded|embed)/(?P<videoID>\d+)'
15     IE_NAME = 'ustream'
16     _TESTS = [{
17         'url': 'http://www.ustream.tv/recorded/20274954',
18         'file': '20274954.flv',
19         'md5': '088f151799e8f572f84eb62f17d73e5c',
20         'info_dict': {
21             "uploader": "Young Americans for Liberty",
22             "title": "Young Americans for Liberty February 7, 2012 2:28 AM",
23         },
24     },
25     {
26         'url': 'http://www.ustream.tv/embed/17357891',
27         'file': 'NSA and Privacy Forum Debate featuring General Hayden and Barton Gellman-45734260.flv',
28         'md5': '27b99cdb639c9b12a79bca876a073417',
29         'info_dict': {
30             "uploader": "AU SPA: The NSA and Privacy",
31             "title": "NSA and Privacy Forum Debate featuring General Hayden and Barton Gellman"
32     },
33     }
34     ]
35
36     def _real_extract(self, url):
37         m = re.match(self._VALID_URL, url)
38         if m.group('type') == 'embed':
39             video_id = m.group('videoID')
40             webpage = self._download_webpage(url, video_id)
41             desktop_video_id = self._html_search_regex(r'ContentVideoIds=\["([^"]*?)"\]', webpage, 'desktop_video_id')
42             desktop_url = 'http://www.ustream.tv/recorded/' + desktop_video_id
43             return self.url_result(desktop_url, 'Ustream')
44
45         video_id = m.group('videoID')
46
47         video_url = 'http://tcdn.ustream.tv/video/%s' % video_id
48         webpage = self._download_webpage(url, video_id)
49
50         self.report_extraction(video_id)
51
52         video_title = self._html_search_regex(r'data-title="(?P<title>.+)"',
53             webpage, 'title')
54
55         uploader = self._html_search_regex(r'data-content-type="channel".*?>(?P<uploader>.*?)</a>',
56             webpage, 'uploader', fatal=False, flags=re.DOTALL)
57
58         thumbnail = self._html_search_regex(r'<link rel="image_src" href="(?P<thumb>.*?)"',
59             webpage, 'thumbnail', fatal=False)
60
61         return {
62             'id': video_id,
63             'url': video_url,
64             'ext': 'flv',
65             'title': video_title,
66             'uploader': uploader,
67             'thumbnail': thumbnail,
68         }
69
70
71 class UstreamChannelIE(InfoExtractor):
72     _VALID_URL = r'https?://www\.ustream\.tv/channel/(?P<slug>.+)'
73     IE_NAME = 'ustream:channel'
74
75     def _real_extract(self, url):
76         m = re.match(self._VALID_URL, url)
77         slug = m.group('slug')
78         webpage = self._download_webpage(url, slug)
79         channel_id = get_meta_content('ustream:channel_id', webpage)
80
81         BASE = 'http://www.ustream.tv'
82         next_url = '/ajax/socialstream/videos/%s/1.json' % channel_id
83         video_ids = []
84         while next_url:
85             reply = json.loads(self._download_webpage(compat_urlparse.urljoin(BASE, next_url), channel_id))
86             video_ids.extend(re.findall(r'data-content-id="(\d.*)"', reply['data']))
87             next_url = reply['nextUrl']
88
89         urls = ['http://www.ustream.tv/recorded/' + vid for vid in video_ids]
90         url_entries = [self.url_result(eurl, 'Ustream') for eurl in urls]
91         return self.playlist_result(url_entries, channel_id)