microsoftembed.py 13 KB

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