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