[cleanup] Make more playlist entries lazy (#11763)
[yt-dlp.git] / yt_dlp / extractor / microsoftembed.py
blob2575d6c5e4ce339639e4ec5298c88aa1892c8698
1 import re
3 from .common import InfoExtractor
4 from ..utils import (
5 int_or_none,
6 parse_iso8601,
7 traverse_obj,
8 unified_timestamp,
9 url_basename,
10 url_or_none,
14 class MicrosoftEmbedIE(InfoExtractor):
15 _VALID_URL = r'https?://(?:www\.)?microsoft\.com/(?:[^/]+/)?videoplayer/embed/(?P<id>[a-z0-9A-Z]+)'
17 _TESTS = [{
18 'url': 'https://www.microsoft.com/en-us/videoplayer/embed/RWL07e',
19 'md5': 'eb0ae9007f9b305f9acd0a03e74cb1a9',
20 'info_dict': {
21 'id': 'RWL07e',
22 'title': 'Microsoft for Public Health and Social Services',
23 'ext': 'mp4',
24 'thumbnail': 'http://img-prod-cms-rt-microsoft-com.akamaized.net/cms/api/am/imageFileData/RWL7Ju?ver=cae5',
25 'age_limit': 0,
26 'timestamp': 1631658316,
27 'upload_date': '20210914',
29 'expected_warnings': ['Failed to parse XML: syntax error: line 1, column 0'],
31 _API_URL = 'https://prod-video-cms-rt-microsoft-com.akamaized.net/vhs/api/videos/'
33 def _real_extract(self, url):
34 video_id = self._match_id(url)
35 metadata = self._download_json(self._API_URL + video_id, video_id)
37 formats = []
38 for source_type, source in metadata['streams'].items():
39 if source_type == 'smooth_Streaming':
40 formats.extend(self._extract_ism_formats(source['url'], video_id, 'mss', fatal=False))
41 elif source_type == 'apple_HTTP_Live_Streaming':
42 formats.extend(self._extract_m3u8_formats(source['url'], video_id, 'mp4', fatal=False))
43 elif source_type == 'mPEG_DASH':
44 formats.extend(self._extract_mpd_formats(source['url'], video_id, fatal=False))
45 else:
46 formats.append({
47 'format_id': source_type,
48 'url': source['url'],
49 'height': source.get('heightPixels'),
50 'width': source.get('widthPixels'),
53 subtitles = {
54 lang: [{
55 'url': data.get('url'),
56 'ext': 'vtt',
57 }] for lang, data in traverse_obj(metadata, 'captions', default={}).items()
60 thumbnails = [{
61 'url': thumb.get('url'),
62 'width': thumb.get('width') or None,
63 'height': thumb.get('height') or None,
64 } for thumb in traverse_obj(metadata, ('snippet', 'thumbnails', ...))]
65 self._remove_duplicate_formats(thumbnails)
67 return {
68 'id': video_id,
69 'title': traverse_obj(metadata, ('snippet', 'title')),
70 'timestamp': unified_timestamp(traverse_obj(metadata, ('snippet', 'activeStartDate'))),
71 'age_limit': int_or_none(traverse_obj(metadata, ('snippet', 'minimumAge'))) or 0,
72 'formats': formats,
73 'subtitles': subtitles,
74 'thumbnails': thumbnails,
78 class MicrosoftMediusBaseIE(InfoExtractor):
79 @staticmethod
80 def _sub_to_dict(subtitle_list):
81 subtitles = {}
82 for sub in subtitle_list:
83 subtitles.setdefault(sub.pop('tag', 'und'), []).append(sub)
84 return subtitles
86 def _extract_ism(self, ism_url, video_id):
87 formats = self._extract_ism_formats(ism_url, video_id)
88 for fmt in formats:
89 if fmt['language'] != 'eng' and 'English' not in fmt['format_id']:
90 fmt['language_preference'] = -10
91 return formats
94 class MicrosoftMediusIE(MicrosoftMediusBaseIE):
95 _VALID_URL = r'https?://medius\.microsoft\.com/Embed/(?:Video\?id=|video-nc/|VideoDetails/)(?P<id>[\da-f-]+)'
97 _TESTS = [{
98 'url': 'https://medius.microsoft.com/Embed/video-nc/9640d86c-f513-4889-959e-5dace86e7d2b',
99 'info_dict': {
100 'id': '9640d86c-f513-4889-959e-5dace86e7d2b',
101 'ext': 'ismv',
102 'title': 'Rapidly code, test and ship from secure cloud developer environments',
103 'description': 'md5:33c8e4facadc438613476eea24165f71',
104 'thumbnail': r're:https://mediusimg\.event\.microsoft\.com/video-\d+/thumbnail\.jpg.*',
105 'subtitles': 'count:30',
107 }, {
108 'url': 'https://medius.microsoft.com/Embed/video-nc/81215af5-c813-4dcd-aede-94f4e1a7daa3',
109 'info_dict': {
110 'id': '81215af5-c813-4dcd-aede-94f4e1a7daa3',
111 'ext': 'ismv',
112 'title': 'Microsoft Build opening',
113 'description': 'md5:43455096141077a1f23144cab8cec1cb',
114 'thumbnail': r're:https://mediusimg\.event\.microsoft\.com/video-\d+/thumbnail\.jpg.*',
115 'subtitles': 'count:31',
117 }, {
118 'url': 'https://medius.microsoft.com/Embed/VideoDetails/78493569-9b3b-4a85-a409-ee76e789e25c',
119 'info_dict': {
120 'id': '78493569-9b3b-4a85-a409-ee76e789e25c',
121 'ext': 'ismv',
122 'title': ' Anomaly Detection & Root cause at Edge',
123 'description': 'md5:f8f1ad93d7918649bfb97fa081b03b83',
124 'thumbnail': r're:https://mediusdownload.event.microsoft.com/asset.*\.jpg.*',
125 'subtitles': 'count:17',
127 }, {
128 'url': 'https://medius.microsoft.com/Embed/Video?id=0dc69bda-079b-4070-a7db-a8da1a06a9c7',
129 'only_matching': True,
130 }, {
131 'url': 'https://medius.microsoft.com/Embed/video-nc/fe823a91-959c-465b-96d4-8f4db624f72c',
132 'only_matching': True,
135 def _extract_subtitle(self, webpage, video_id):
136 captions = traverse_obj(
137 self._search_json(r'const\s+captionsConfiguration\s*=', webpage, 'captions', video_id, default=None),
138 ('languageList', lambda _, v: url_or_none(v['src']), {
139 'url': 'src',
140 'tag': ('srclang', {str}),
141 'name': ('kind', {str}),
142 })) or [{'url': url, 'tag': url_basename(url).split('.vtt')[0].split('_')[-1]}
143 for url in re.findall(r'var\s+file\s+=\s+\{[^}]+\'(https://[^\']+\.vtt\?[^\']+)', webpage)]
145 return self._sub_to_dict(captions)
147 def _real_extract(self, url):
148 video_id = self._match_id(url)
149 webpage = self._download_webpage(f'https://medius.microsoft.com/Embed/video-nc/{video_id}', video_id)
151 return {
152 'id': video_id,
153 'title': self._og_search_title(webpage),
154 'description': self._og_search_description(webpage),
155 'formats': self._extract_ism(
156 self._search_regex(r'StreamUrl\s*=\s*"([^"]+manifest)"', webpage, 'ism url'), video_id),
157 'thumbnail': self._og_search_thumbnail(webpage),
158 'subtitles': self._extract_subtitle(webpage, video_id),
162 class MicrosoftLearnPlaylistIE(InfoExtractor):
163 _VALID_URL = r'https?://learn\.microsoft\.com/(?:[\w-]+/)?(?P<type>shows|events)/(?P<id>[\w-]+)/?(?:[?#]|$)'
164 _TESTS = [{
165 'url': 'https://learn.microsoft.com/en-us/shows/bash-for-beginners',
166 'info_dict': {
167 'id': 'bash-for-beginners',
168 'title': 'Bash for Beginners',
169 'description': 'md5:16a91c07222117d1e00912f0dbc02c2c',
171 'playlist_count': 20,
172 }, {
173 'url': 'https://learn.microsoft.com/en-us/events/build-2022',
174 'info_dict': {
175 'id': 'build-2022',
176 'title': 'Microsoft Build 2022 - Events',
177 'description': 'md5:c16b43848027df837b22c6fbac7648d3',
179 'playlist_count': 201,
182 def _entries(self, url_base, video_id):
183 skip = 0
184 while True:
185 playlist_info = self._download_json(url_base, video_id, f'Downloading entries {skip}', query={
186 'locale': 'en-us',
187 '$skip': skip,
189 url_paths = traverse_obj(playlist_info, ('results', ..., 'url', {str}))
190 for url_path in url_paths:
191 yield self.url_result(f'https://learn.microsoft.com/en-us{url_path}')
192 skip += len(url_paths)
193 if skip >= playlist_info.get('count', 0) or not url_paths:
194 break
196 def _real_extract(self, url):
197 playlist_id, playlist_type = self._match_valid_url(url).group('id', 'type')
198 webpage = self._download_webpage(url, playlist_id)
200 metainfo = {
201 'title': self._og_search_title(webpage),
202 'description': self._og_search_description(webpage),
204 sub_type = 'episodes' if playlist_type == 'shows' else 'sessions'
206 url_base = f'https://learn.microsoft.com/api/contentbrowser/search/{playlist_type}/{playlist_id}/{sub_type}'
207 return self.playlist_result(self._entries(url_base, playlist_id), playlist_id, **metainfo)
210 class MicrosoftLearnEpisodeIE(MicrosoftMediusBaseIE):
211 _VALID_URL = r'https?://learn\.microsoft\.com/(?:[\w-]+/)?shows/[\w-]+/(?P<id>[^?#/]+)'
212 _TESTS = [{
213 'url': 'https://learn.microsoft.com/en-us/shows/bash-for-beginners/what-is-the-difference-between-a-terminal-and-a-shell-2-of-20-bash-for-beginners/',
214 'info_dict': {
215 'id': 'd44e1a03-a0e5-45c2-9496-5c9fa08dc94c',
216 'ext': 'ismv',
217 'title': 'What is the Difference Between a Terminal and a Shell? (Part 2 of 20)',
218 'description': 'md5:7bbbfb593d21c2cf2babc3715ade6b88',
219 'timestamp': 1676339547,
220 'upload_date': '20230214',
221 'thumbnail': r're:https://learn\.microsoft\.com/video/media/.*\.png',
222 'subtitles': 'count:14',
226 def _real_extract(self, url):
227 video_id = self._match_id(url)
228 webpage = self._download_webpage(url, video_id)
230 entry_id = self._html_search_meta('entryId', webpage, 'entryId', fatal=True)
231 video_info = self._download_json(
232 f'https://learn.microsoft.com/api/video/public/v1/entries/{entry_id}', video_id)
233 return {
234 'id': entry_id,
235 'formats': self._extract_ism(video_info['publicVideo']['adaptiveVideoUrl'], video_id),
236 'subtitles': self._sub_to_dict(traverse_obj(video_info, (
237 'publicVideo', 'captions', lambda _, v: url_or_none(v['url']), {
238 'tag': ('language', {str}),
239 'url': 'url',
240 }))),
241 'title': self._og_search_title(webpage),
242 'description': self._og_search_description(webpage),
243 **traverse_obj(video_info, {
244 'timestamp': ('createTime', {parse_iso8601}),
245 'thumbnails': ('publicVideo', 'thumbnailOtherSizes', ..., {'url': {url_or_none}}),
250 class MicrosoftLearnSessionIE(InfoExtractor):
251 _VALID_URL = r'https?://learn\.microsoft\.com/(?:[\w-]+/)?events/[\w-]+/(?P<id>[^?#/]+)'
252 _TESTS = [{
253 'url': 'https://learn.microsoft.com/en-us/events/build-2022/ts01-rapidly-code-test-ship-from-secure-cloud-developer-environments',
254 'info_dict': {
255 'id': '9640d86c-f513-4889-959e-5dace86e7d2b',
256 'ext': 'ismv',
257 'title': 'Rapidly code, test and ship from secure cloud developer environments - Events',
258 'description': 'md5:f26c1a85d41c1cffd27a0279254a25c3',
259 'timestamp': 1653408600,
260 'upload_date': '20220524',
261 'thumbnail': r're:https://mediusimg\.event\.microsoft\.com/video-\d+/thumbnail\.jpg.*',
265 def _real_extract(self, url):
266 video_id = self._match_id(url)
267 webpage = self._download_webpage(url, video_id)
269 metainfo = {
270 'title': self._og_search_title(webpage),
271 'description': self._og_search_description(webpage),
272 'timestamp': parse_iso8601(self._html_search_meta('startDate', webpage, 'startDate')),
275 return self.url_result(
276 self._html_search_meta('externalVideoUrl', webpage, 'videoUrl', fatal=True),
277 url_transparent=True, ie=MicrosoftMediusIE, **metainfo)
280 class MicrosoftBuildIE(InfoExtractor):
281 _VALID_URL = [
282 r'https?://build\.microsoft\.com/[\w-]+/sessions/(?P<id>[\da-f-]+)',
283 r'https?://build\.microsoft\.com/[\w-]+/(?P<id>sessions)/?(?:[?#]|$)',
286 _TESTS = [{
287 'url': 'https://build.microsoft.com/en-US/sessions/b49feb31-afcd-4217-a538-d3ca1d171198?source=sessions',
288 'info_dict': {
289 'id': 'aee55fb5-fcf9-4b38-b764-a3527cb57554',
290 'ext': 'ismv',
291 'title': 'Microsoft Build opening keynote',
292 'description': 'md5:d38338f336ef4b6ef9ad2a7466a76655',
293 'timestamp': 1716307200,
294 'upload_date': '20240521',
295 'thumbnail': r're:https://mediusimg\.event\.microsoft\.com/video-\d+/thumbnail\.jpg.*',
297 }, {
298 'url': 'https://build.microsoft.com/en-US/sessions',
299 'info_dict': {
300 'id': 'sessions',
302 'playlist_mincount': 418,
305 def _real_extract(self, url):
306 video_id = self._match_id(url)
308 entries = [
309 self.url_result(
310 video_info['onDemand'], ie=MicrosoftMediusIE, url_transparent=True, **traverse_obj(video_info, {
311 'id': ('sessionId', {str}),
312 'title': ('title', {str}),
313 'description': ('description', {str}),
314 'timestamp': ('startDateTime', {parse_iso8601}),
316 for video_info in self._download_json(
317 'https://api-v2.build.microsoft.com/api/session/all/en-US', video_id, 'Downloading video info')
319 if video_id == 'sessions':
320 return self.playlist_result(entries, video_id)
321 else:
322 return traverse_obj(entries, (lambda _, v: v['id'] == video_id), get_all=False)