]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/channel9.py
Prepare to release.
[youtubedl] / youtube_dl / extractor / channel9.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7 clean_html,
8 ExtractorError,
9 int_or_none,
10 parse_iso8601,
11 qualities,
12 unescapeHTML,
13 )
14
15
16 class Channel9IE(InfoExtractor):
17 IE_DESC = 'Channel 9'
18 IE_NAME = 'channel9'
19 _VALID_URL = r'https?://(?:www\.)?(?:channel9\.msdn\.com|s\.ch9\.ms)/(?P<contentpath>.+?)(?P<rss>/RSS)?/?(?:[?#&]|$)'
20
21 _TESTS = [{
22 'url': 'http://channel9.msdn.com/Events/TechEd/Australia/2013/KOS002',
23 'md5': '32083d4eaf1946db6d454313f44510ca',
24 'info_dict': {
25 'id': '6c413323-383a-49dc-88f9-a22800cab024',
26 'ext': 'wmv',
27 'title': 'Developer Kick-Off Session: Stuff We Love',
28 'description': 'md5:b80bf9355a503c193aff7ec6cd5a7731',
29 'duration': 4576,
30 'thumbnail': r're:https?://.*\.jpg',
31 'timestamp': 1377717420,
32 'upload_date': '20130828',
33 'session_code': 'KOS002',
34 'session_room': 'Arena 1A',
35 'session_speakers': ['Andrew Coates', 'Brady Gaster', 'Mads Kristensen', 'Ed Blankenship', 'Patrick Klug'],
36 },
37 }, {
38 'url': 'http://channel9.msdn.com/posts/Self-service-BI-with-Power-BI-nuclear-testing',
39 'md5': 'dcf983ee6acd2088e7188c3cf79b46bc',
40 'info_dict': {
41 'id': 'fe8e435f-bb93-4e01-8e97-a28c01887024',
42 'ext': 'wmv',
43 'title': 'Self-service BI with Power BI - nuclear testing',
44 'description': 'md5:2d17fec927fc91e9e17783b3ecc88f54',
45 'duration': 1540,
46 'thumbnail': r're:https?://.*\.jpg',
47 'timestamp': 1386381991,
48 'upload_date': '20131207',
49 'authors': ['Mike Wilmot'],
50 },
51 }, {
52 # low quality mp4 is best
53 'url': 'https://channel9.msdn.com/Events/CPP/CppCon-2015/Ranges-for-the-Standard-Library',
54 'info_dict': {
55 'id': '33ad69d2-6a4e-4172-83a1-a523013dec76',
56 'ext': 'mp4',
57 'title': 'Ranges for the Standard Library',
58 'description': 'md5:9895e0a9fd80822d2f01c454b8f4a372',
59 'duration': 5646,
60 'thumbnail': r're:https?://.*\.jpg',
61 'upload_date': '20150930',
62 'timestamp': 1443640735,
63 },
64 'params': {
65 'skip_download': True,
66 },
67 }, {
68 'url': 'https://channel9.msdn.com/Niners/Splendid22/Queue/76acff796e8f411184b008028e0d492b/RSS',
69 'info_dict': {
70 'id': 'Niners/Splendid22/Queue/76acff796e8f411184b008028e0d492b',
71 'title': 'Channel 9',
72 },
73 'playlist_mincount': 100,
74 }, {
75 'url': 'https://channel9.msdn.com/Events/DEVintersection/DEVintersection-2016/RSS',
76 'only_matching': True,
77 }, {
78 'url': 'https://channel9.msdn.com/Events/Speakers/scott-hanselman/RSS?UrlSafeName=scott-hanselman',
79 'only_matching': True,
80 }]
81
82 _RSS_URL = 'http://channel9.msdn.com/%s/RSS'
83
84 def _extract_list(self, video_id, rss_url=None):
85 if not rss_url:
86 rss_url = self._RSS_URL % video_id
87 rss = self._download_xml(rss_url, video_id, 'Downloading RSS')
88 entries = [self.url_result(session_url.text, 'Channel9')
89 for session_url in rss.findall('./channel/item/link')]
90 title_text = rss.find('./channel/title').text
91 return self.playlist_result(entries, video_id, title_text)
92
93 def _real_extract(self, url):
94 content_path, rss = re.match(self._VALID_URL, url).groups()
95
96 if rss:
97 return self._extract_list(content_path, url)
98
99 webpage = self._download_webpage(
100 url, content_path, 'Downloading web page')
101
102 episode_data = self._search_regex(
103 r"data-episode='([^']+)'", webpage, 'episode data', default=None)
104 if episode_data:
105 episode_data = self._parse_json(unescapeHTML(
106 episode_data), content_path)
107 content_id = episode_data['contentId']
108 is_session = '/Sessions(' in episode_data['api']
109 content_url = 'https://channel9.msdn.com/odata' + episode_data['api']
110 if is_session:
111 content_url += '?$expand=Speakers'
112 else:
113 content_url += '?$expand=Authors'
114 content_data = self._download_json(content_url, content_id)
115 title = content_data['Title']
116
117 QUALITIES = (
118 'mp3',
119 'wmv', 'mp4',
120 'wmv-low', 'mp4-low',
121 'wmv-mid', 'mp4-mid',
122 'wmv-high', 'mp4-high',
123 )
124
125 quality_key = qualities(QUALITIES)
126
127 def quality(quality_id, format_url):
128 return (len(QUALITIES) if '_Source.' in format_url
129 else quality_key(quality_id))
130
131 formats = []
132 urls = set()
133
134 SITE_QUALITIES = {
135 'MP3': 'mp3',
136 'MP4': 'mp4',
137 'Low Quality WMV': 'wmv-low',
138 'Low Quality MP4': 'mp4-low',
139 'Mid Quality WMV': 'wmv-mid',
140 'Mid Quality MP4': 'mp4-mid',
141 'High Quality WMV': 'wmv-high',
142 'High Quality MP4': 'mp4-high',
143 }
144
145 formats_select = self._search_regex(
146 r'(?s)<select[^>]+name=["\']format[^>]+>(.+?)</select', webpage,
147 'formats select', default=None)
148 if formats_select:
149 for mobj in re.finditer(
150 r'<option\b[^>]+\bvalue=(["\'])(?P<url>(?:(?!\1).)+)\1[^>]*>\s*(?P<format>[^<]+?)\s*<',
151 formats_select):
152 format_url = mobj.group('url')
153 if format_url in urls:
154 continue
155 urls.add(format_url)
156 format_id = mobj.group('format')
157 quality_id = SITE_QUALITIES.get(format_id, format_id)
158 formats.append({
159 'url': format_url,
160 'format_id': quality_id,
161 'quality': quality(quality_id, format_url),
162 'vcodec': 'none' if quality_id == 'mp3' else None,
163 })
164
165 API_QUALITIES = {
166 'VideoMP4Low': 'mp4-low',
167 'VideoWMV': 'wmv-mid',
168 'VideoMP4Medium': 'mp4-mid',
169 'VideoMP4High': 'mp4-high',
170 'VideoWMVHQ': 'wmv-hq',
171 }
172
173 for format_id, q in API_QUALITIES.items():
174 q_url = content_data.get(format_id)
175 if not q_url or q_url in urls:
176 continue
177 urls.add(q_url)
178 formats.append({
179 'url': q_url,
180 'format_id': q,
181 'quality': quality(q, q_url),
182 })
183
184 self._sort_formats(formats)
185
186 slides = content_data.get('Slides')
187 zip_file = content_data.get('ZipFile')
188
189 if not formats and not slides and not zip_file:
190 raise ExtractorError(
191 'None of recording, slides or zip are available for %s' % content_path)
192
193 subtitles = {}
194 for caption in content_data.get('Captions', []):
195 caption_url = caption.get('Url')
196 if not caption_url:
197 continue
198 subtitles.setdefault(caption.get('Language', 'en'), []).append({
199 'url': caption_url,
200 'ext': 'vtt',
201 })
202
203 common = {
204 'id': content_id,
205 'title': title,
206 'description': clean_html(content_data.get('Description') or content_data.get('Body')),
207 'thumbnail': content_data.get('Thumbnail') or content_data.get('VideoPlayerPreviewImage'),
208 'duration': int_or_none(content_data.get('MediaLengthInSeconds')),
209 'timestamp': parse_iso8601(content_data.get('PublishedDate')),
210 'avg_rating': int_or_none(content_data.get('Rating')),
211 'rating_count': int_or_none(content_data.get('RatingCount')),
212 'view_count': int_or_none(content_data.get('Views')),
213 'comment_count': int_or_none(content_data.get('CommentCount')),
214 'subtitles': subtitles,
215 }
216 if is_session:
217 speakers = []
218 for s in content_data.get('Speakers', []):
219 speaker_name = s.get('FullName')
220 if not speaker_name:
221 continue
222 speakers.append(speaker_name)
223
224 common.update({
225 'session_code': content_data.get('Code'),
226 'session_room': content_data.get('Room'),
227 'session_speakers': speakers,
228 })
229 else:
230 authors = []
231 for a in content_data.get('Authors', []):
232 author_name = a.get('DisplayName')
233 if not author_name:
234 continue
235 authors.append(author_name)
236 common['authors'] = authors
237
238 contents = []
239
240 if slides:
241 d = common.copy()
242 d.update({'title': title + '-Slides', 'url': slides})
243 contents.append(d)
244
245 if zip_file:
246 d = common.copy()
247 d.update({'title': title + '-Zip', 'url': zip_file})
248 contents.append(d)
249
250 if formats:
251 d = common.copy()
252 d.update({'title': title, 'formats': formats})
253 contents.append(d)
254 return self.playlist_result(contents)
255 else:
256 return self._extract_list(content_path)