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