mit.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import json
  2. import re
  3. from .common import InfoExtractor
  4. from .youtube import YoutubeIE
  5. from ..utils import (
  6. ExtractorError,
  7. clean_html,
  8. get_element_by_id,
  9. )
  10. class TechTVMITIE(InfoExtractor):
  11. IE_NAME = 'techtv.mit.edu'
  12. _VALID_URL = r'https?://techtv\.mit\.edu/(?:videos|embeds)/(?P<id>\d+)'
  13. _TEST = {
  14. 'url': 'http://techtv.mit.edu/videos/25418-mit-dna-learning-center-set',
  15. 'md5': '00a3a27ee20d44bcaa0933ccec4a2cf7',
  16. 'info_dict': {
  17. 'id': '25418',
  18. 'ext': 'mp4',
  19. 'title': 'MIT DNA and Protein Sets',
  20. 'description': 'md5:46f5c69ce434f0a97e7c628cc142802d',
  21. },
  22. }
  23. def _real_extract(self, url):
  24. video_id = self._match_id(url)
  25. raw_page = self._download_webpage(
  26. f'http://techtv.mit.edu/videos/{video_id}', video_id)
  27. clean_page = re.compile(r'<!--.*?-->', re.S).sub('', raw_page)
  28. base_url = self._proto_relative_url(self._search_regex(
  29. r'ipadUrl: \'(.+?cloudfront.net/)', raw_page, 'base url'), 'http:')
  30. formats_json = self._search_regex(
  31. r'bitrates: (\[.+?\])', raw_page, 'video formats')
  32. formats_mit = json.loads(formats_json)
  33. formats = [
  34. {
  35. 'format_id': f['label'],
  36. 'url': base_url + f['url'].partition(':')[2],
  37. 'ext': f['url'].partition(':')[0],
  38. 'format': f['label'],
  39. 'width': f['width'],
  40. 'vbr': f['bitrate'],
  41. }
  42. for f in formats_mit
  43. ]
  44. title = get_element_by_id('edit-title', clean_page)
  45. description = clean_html(get_element_by_id('edit-description', clean_page))
  46. thumbnail = self._search_regex(
  47. r'playlist:.*?url: \'(.+?)\'',
  48. raw_page, 'thumbnail', flags=re.DOTALL)
  49. return {
  50. 'id': video_id,
  51. 'title': title,
  52. 'formats': formats,
  53. 'description': description,
  54. 'thumbnail': thumbnail,
  55. }
  56. class OCWMITIE(InfoExtractor):
  57. IE_NAME = 'ocw.mit.edu'
  58. _VALID_URL = r'^https?://ocw\.mit\.edu/courses/(?P<topic>[a-z0-9\-]+)'
  59. _BASE_URL = 'http://ocw.mit.edu/'
  60. _TESTS = [
  61. {
  62. 'url': 'http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-041-probabilistic-systems-analysis-and-applied-probability-fall-2010/video-lectures/lecture-7-multiple-variables-expectations-independence/',
  63. 'info_dict': {
  64. 'id': 'EObHWIEKGjA',
  65. 'ext': 'webm',
  66. 'title': 'Lecture 7: Multiple Discrete Random Variables: Expectations, Conditioning, Independence',
  67. 'description': 'In this lecture, the professor discussed multiple random variables, expectations, and binomial distribution.',
  68. 'upload_date': '20121109',
  69. 'uploader_id': 'MIT',
  70. 'uploader': 'MIT OpenCourseWare',
  71. },
  72. },
  73. {
  74. 'url': 'http://ocw.mit.edu/courses/mathematics/18-01sc-single-variable-calculus-fall-2010/1.-differentiation/part-a-definition-and-basic-rules/session-1-introduction-to-derivatives/',
  75. 'info_dict': {
  76. 'id': '7K1sB05pE0A',
  77. 'ext': 'mp4',
  78. 'title': 'Session 1: Introduction to Derivatives',
  79. 'upload_date': '20090818',
  80. 'uploader_id': 'MIT',
  81. 'uploader': 'MIT OpenCourseWare',
  82. 'description': 'This section contains lecture video excerpts, lecture notes, an interactive mathlet with supporting documents, and problem solving videos.',
  83. },
  84. },
  85. ]
  86. def _real_extract(self, url):
  87. mobj = self._match_valid_url(url)
  88. topic = mobj.group('topic')
  89. webpage = self._download_webpage(url, topic)
  90. title = self._html_search_meta('WT.cg_s', webpage)
  91. description = self._html_search_meta('Description', webpage)
  92. # search for call to ocw_embed_chapter_media(container_id, media_url, provider, page_url, image_url, start, stop, captions_file)
  93. embed_chapter_media = re.search(r'ocw_embed_chapter_media\((.+?)\)', webpage)
  94. if embed_chapter_media:
  95. metadata = re.sub(r'[\'"]', '', embed_chapter_media.group(1))
  96. metadata = re.split(r', ?', metadata)
  97. yt = metadata[1]
  98. else:
  99. # search for call to ocw_embed_chapter_media(container_id, media_url, provider, page_url, image_url, captions_file)
  100. embed_media = re.search(r'ocw_embed_media\((.+?)\)', webpage)
  101. if embed_media:
  102. metadata = re.sub(r'[\'"]', '', embed_media.group(1))
  103. metadata = re.split(r', ?', metadata)
  104. yt = metadata[1]
  105. else:
  106. raise ExtractorError('Unable to find embedded YouTube video.')
  107. video_id = YoutubeIE.extract_id(yt)
  108. return {
  109. '_type': 'url_transparent',
  110. 'id': video_id,
  111. 'title': title,
  112. 'description': description,
  113. 'url': yt,
  114. 'ie_key': 'Youtube',
  115. }