]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/microsoftvirtualacademy.py
[cleanup] Add more ruff rules (#10149)
[yt-dlp.git] / yt_dlp / extractor / microsoftvirtualacademy.py
CommitLineData
f23a92a0
S
1import re
2
3from .common import InfoExtractor
f23a92a0
S
4from ..utils import (
5 int_or_none,
6 parse_duration,
7 smuggle_url,
8 unsmuggle_url,
9 xpath_text,
10)
11
12
13class MicrosoftVirtualAcademyBaseIE(InfoExtractor):
14 def _extract_base_url(self, course_id, display_id):
15 return self._download_json(
add96eb9 16 f'https://api-mlxprod.microsoft.com/services/products/anonymous/{course_id}',
f23a92a0
S
17 display_id, 'Downloading course base URL')
18
19 def _extract_chapter_and_title(self, title):
20 if not title:
21 return None, None
22 m = re.search(r'(?P<chapter>\d+)\s*\|\s*(?P<title>.+)', title)
23 return (int(m.group('chapter')), m.group('title')) if m else (None, title)
24
25
26class MicrosoftVirtualAcademyIE(MicrosoftVirtualAcademyBaseIE):
27 IE_NAME = 'mva'
28 IE_DESC = 'Microsoft Virtual Academy videos'
add96eb9 29 _VALID_URL = rf'(?:{IE_NAME}:|https?://(?:mva\.microsoft|(?:www\.)?microsoftvirtualacademy)\.com/[^/]+/training-courses/[^/?#&]+-)(?P<course_id>\d+)(?::|\?l=)(?P<id>[\da-zA-Z]+_\d+)'
f23a92a0
S
30
31 _TESTS = [{
32 'url': 'https://mva.microsoft.com/en-US/training-courses/microsoft-azure-fundamentals-virtual-machines-11788?l=gfVXISmEB_6804984382',
33 'md5': '7826c44fc31678b12ad8db11f6b5abb9',
34 'info_dict': {
35 'id': 'gfVXISmEB_6804984382',
36 'ext': 'mp4',
37 'title': 'Course Introduction',
38 'formats': 'mincount:3',
39 'subtitles': {
40 'en': [{
41 'ext': 'ttml',
42 }],
43 },
add96eb9 44 },
f23a92a0
S
45 }, {
46 'url': 'mva:11788:gfVXISmEB_6804984382',
47 'only_matching': True,
48 }]
49
50 def _real_extract(self, url):
51 url, smuggled_data = unsmuggle_url(url, {})
52
5ad28e7f 53 mobj = self._match_valid_url(url)
f23a92a0
S
54 course_id = mobj.group('course_id')
55 video_id = mobj.group('id')
56
57 base_url = smuggled_data.get('base_url') or self._extract_base_url(course_id, video_id)
58
59 settings = self._download_xml(
add96eb9 60 f'{base_url}/content/content_{video_id}/videosettings.xml?v=1',
f23a92a0
S
61 video_id, 'Downloading video settings XML')
62
63 _, title = self._extract_chapter_and_title(xpath_text(
64 settings, './/Title', 'title', fatal=True))
65
66 formats = []
67
f9934b96 68 for sources in settings.findall('.//MediaSources'):
639e3b5c 69 sources_type = sources.get('videoType')
f9934b96 70 for source in sources.findall('./MediaSource'):
f23a92a0
S
71 video_url = source.text
72 if not video_url or not video_url.startswith('http'):
73 continue
639e3b5c
RA
74 if sources_type == 'smoothstreaming':
75 formats.extend(self._extract_ism_formats(
76 video_url, video_id, 'mss', fatal=False))
77 continue
f23a92a0
S
78 video_mode = source.get('videoMode')
79 height = int_or_none(self._search_regex(
80 r'^(\d+)[pP]$', video_mode or '', 'height', default=None))
81 codec = source.get('codec')
82 acodec, vcodec = [None] * 2
83 if codec:
84 codecs = codec.split(',')
85 if len(codecs) == 2:
86 acodec, vcodec = codecs
87 elif len(codecs) == 1:
88 vcodec = codecs[0]
89 formats.append({
90 'url': video_url,
91 'format_id': video_mode,
92 'height': height,
93 'acodec': acodec,
94 'vcodec': vcodec,
95 })
f23a92a0
S
96
97 subtitles = {}
f9934b96 98 for source in settings.findall('.//MarkerResourceSource'):
f23a92a0
S
99 subtitle_url = source.text
100 if not subtitle_url:
101 continue
102 subtitles.setdefault('en', []).append({
add96eb9 103 'url': f'{base_url}/{subtitle_url}',
f23a92a0
S
104 'ext': source.get('type'),
105 })
106
107 return {
108 'id': video_id,
109 'title': title,
110 'subtitles': subtitles,
add96eb9 111 'formats': formats,
f23a92a0
S
112 }
113
114
115class MicrosoftVirtualAcademyCourseIE(MicrosoftVirtualAcademyBaseIE):
116 IE_NAME = 'mva:course'
117 IE_DESC = 'Microsoft Virtual Academy courses'
add96eb9 118 _VALID_URL = rf'(?:{IE_NAME}:|https?://(?:mva\.microsoft|(?:www\.)?microsoftvirtualacademy)\.com/[^/]+/training-courses/(?P<display_id>[^/?#&]+)-)(?P<id>\d+)'
f23a92a0
S
119
120 _TESTS = [{
121 'url': 'https://mva.microsoft.com/en-US/training-courses/microsoft-azure-fundamentals-virtual-machines-11788',
122 'info_dict': {
123 'id': '11788',
124 'title': 'Microsoft Azure Fundamentals: Virtual Machines',
125 },
126 'playlist_count': 36,
127 }, {
128 # with emphasized chapters
129 'url': 'https://mva.microsoft.com/en-US/training-courses/developing-windows-10-games-with-construct-2-16335',
130 'info_dict': {
131 'id': '16335',
132 'title': 'Developing Windows 10 Games with Construct 2',
133 },
134 'playlist_count': 10,
135 }, {
136 'url': 'https://www.microsoftvirtualacademy.com/en-US/training-courses/microsoft-azure-fundamentals-virtual-machines-11788',
137 'only_matching': True,
138 }, {
139 'url': 'mva:course:11788',
140 'only_matching': True,
141 }]
142
143 @classmethod
144 def suitable(cls, url):
add96eb9 145 return False if MicrosoftVirtualAcademyIE.suitable(url) else super().suitable(url)
f23a92a0
S
146
147 def _real_extract(self, url):
5ad28e7f 148 mobj = self._match_valid_url(url)
f23a92a0
S
149 course_id = mobj.group('id')
150 display_id = mobj.group('display_id')
151
152 base_url = self._extract_base_url(course_id, display_id)
153
154 manifest = self._download_json(
add96eb9 155 f'{base_url}/imsmanifestlite.json',
f23a92a0
S
156 display_id, 'Downloading course manifest JSON')['manifest']
157
158 organization = manifest['organizations']['organization'][0]
159
160 entries = []
161 for chapter in organization['item']:
162 chapter_number, chapter_title = self._extract_chapter_and_title(chapter.get('title'))
163 chapter_id = chapter.get('@identifier')
164 for item in chapter.get('item', []):
165 item_id = item.get('@identifier')
166 if not item_id:
167 continue
168 metadata = item.get('resource', {}).get('metadata') or {}
169 if metadata.get('learningresourcetype') != 'Video':
170 continue
171 _, title = self._extract_chapter_and_title(item.get('title'))
172 duration = parse_duration(metadata.get('duration'))
173 description = metadata.get('description')
174 entries.append({
175 '_type': 'url_transparent',
176 'url': smuggle_url(
add96eb9 177 f'mva:{course_id}:{item_id}', {'base_url': base_url}),
f23a92a0
S
178 'title': title,
179 'description': description,
180 'duration': duration,
181 'chapter': chapter_title,
182 'chapter_number': chapter_number,
183 'chapter_id': chapter_id,
184 })
185
186 title = organization.get('title') or manifest.get('metadata', {}).get('title')
187
188 return self.playlist_result(entries, course_id, title)