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