microsoftvirtualacademy.py 7.1 KB

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