]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/ustream.py
[ustream] Add an alternative approach to extract title (fixes #5128)
[yt-dlp.git] / youtube_dl / extractor / ustream.py
CommitLineData
0eb799ba
JMF
1from __future__ import unicode_literals
2
f8610ba1 3import json
78af8eb1
PH
4import re
5
6from .common import InfoExtractor
1cc79574 7from ..compat import (
74ac9bdd 8 compat_urlparse,
74ac9bdd 9)
762155cc 10from ..utils import ExtractorError
78af8eb1
PH
11
12
13class UstreamIE(InfoExtractor):
b702eceb 14 _VALID_URL = r'https?://www\.ustream\.tv/(?P<type>recorded|embed|embed/recorded)/(?P<videoID>\d+)'
0eb799ba 15 IE_NAME = 'ustream'
ca6aada4 16 _TEST = {
0eb799ba 17 'url': 'http://www.ustream.tv/recorded/20274954',
0eb799ba
JMF
18 'md5': '088f151799e8f572f84eb62f17d73e5c',
19 'info_dict': {
9e875391
S
20 'id': '20274954',
21 'ext': 'flv',
22 'uploader': 'Young Americans for Liberty',
23 'title': 'Young Americans for Liberty February 7, 2012 2:28 AM',
0eb799ba 24 },
6f5ac90c 25 }
78af8eb1
PH
26
27 def _real_extract(self, url):
28 m = re.match(self._VALID_URL, url)
b702eceb 29 video_id = m.group('videoID')
30
9e875391
S
31 # some sites use this embed format (see: http://github.com/rg3/youtube-dl/issues/2990)
32 if m.group('type') == 'embed/recorded':
b702eceb 33 video_id = m.group('videoID')
b702eceb 34 desktop_url = 'http://www.ustream.tv/recorded/' + video_id
35 return self.url_result(desktop_url, 'Ustream')
5c386252 36 if m.group('type') == 'embed':
37 video_id = m.group('videoID')
38 webpage = self._download_webpage(url, video_id)
9e875391
S
39 desktop_video_id = self._html_search_regex(
40 r'ContentVideoIds=\["([^"]*?)"\]', webpage, 'desktop_video_id')
5c386252 41 desktop_url = 'http://www.ustream.tv/recorded/' + desktop_video_id
42 return self.url_result(desktop_url, 'Ustream')
43
f8610ba1
YCH
44 params = self._download_json(
45 'http://cdngw.ustream.tv/rgwjson/Viewer.getVideo/' + json.dumps({
46 'brandId': 1,
47 'videoId': int(video_id),
48 'autoplay': False,
49 }), video_id)
50
762155cc
YCH
51 if 'error' in params:
52 raise ExtractorError(params['error']['message'], expected=True)
53
f8610ba1
YCH
54 video_url = params['flv']
55
78af8eb1
PH
56 webpage = self._download_webpage(url, video_id)
57
58 self.report_extraction(video_id)
59
60 video_title = self._html_search_regex(r'data-title="(?P<title>.+)"',
2a813727
YCH
61 webpage, 'title', default=None)
62
63 if not video_title:
64 try:
65 video_title = params['moduleConfig']['meta']['title']
66 except KeyError:
67 pass
68
69 if not video_title:
70 video_title = 'Ustream video ' + video_id
78af8eb1
PH
71
72 uploader = self._html_search_regex(r'data-content-type="channel".*?>(?P<uploader>.*?)</a>',
9e1a5b84 73 webpage, 'uploader', fatal=False, flags=re.DOTALL)
78af8eb1
PH
74
75 thumbnail = self._html_search_regex(r'<link rel="image_src" href="(?P<thumb>.*?)"',
9e1a5b84 76 webpage, 'thumbnail', fatal=False)
0eb799ba
JMF
77
78 return {
79 'id': video_id,
80 'url': video_url,
81 'ext': 'flv',
82 'title': video_title,
83 'uploader': uploader,
84 'thumbnail': thumbnail,
85 }
86
bfd5c93a 87
bfd5c93a 88class UstreamChannelIE(InfoExtractor):
89 _VALID_URL = r'https?://www\.ustream\.tv/channel/(?P<slug>.+)'
0eb799ba 90 IE_NAME = 'ustream:channel'
22a6f150
PH
91 _TEST = {
92 'url': 'http://www.ustream.tv/channel/channeljapan',
93 'info_dict': {
94 'id': '10874166',
95 },
446a03bd 96 'playlist_mincount': 17,
22a6f150 97 }
bfd5c93a 98
99 def _real_extract(self, url):
100 m = re.match(self._VALID_URL, url)
22a6f150
PH
101 display_id = m.group('slug')
102 webpage = self._download_webpage(url, display_id)
44789f24 103 channel_id = self._html_search_meta('ustream:channel_id', webpage)
bfd5c93a 104
bfd5c93a 105 BASE = 'http://www.ustream.tv'
106 next_url = '/ajax/socialstream/videos/%s/1.json' % channel_id
a921f407 107 video_ids = []
bfd5c93a 108 while next_url:
22a6f150
PH
109 reply = self._download_json(
110 compat_urlparse.urljoin(BASE, next_url), display_id,
111 note='Downloading video information (next: %d)' % (len(video_ids) + 1))
a921f407 112 video_ids.extend(re.findall(r'data-content-id="(\d.*)"', reply['data']))
bfd5c93a 113 next_url = reply['nextUrl']
bfd5c93a 114
22a6f150
PH
115 entries = [
116 self.url_result('http://www.ustream.tv/recorded/' + vid, 'Ustream')
117 for vid in video_ids]
118 return {
119 '_type': 'playlist',
120 'id': channel_id,
121 'display_id': display_id,
122 'entries': entries,
123 }