mit.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. from __future__ import unicode_literals
  2. import re
  3. import json
  4. from .common import InfoExtractor
  5. from .youtube import YoutubeIE
  6. from ..utils import (
  7. compat_urlparse,
  8. clean_html,
  9. ExtractorError,
  10. get_element_by_id,
  11. )
  12. class TechTVMITIE(InfoExtractor):
  13. IE_NAME = 'techtv.mit.edu'
  14. _VALID_URL = r'https?://techtv\.mit\.edu/(videos|embeds)/(?P<id>\d+)'
  15. _TEST = {
  16. 'url': 'http://techtv.mit.edu/videos/25418-mit-dna-learning-center-set',
  17. 'md5': '1f8cb3e170d41fd74add04d3c9330e5f',
  18. 'info_dict': {
  19. 'id': '25418',
  20. 'ext': 'mp4',
  21. 'title': 'MIT DNA Learning Center Set',
  22. 'description': 'md5:82313335e8a8a3f243351ba55bc1b474',
  23. },
  24. }
  25. def _real_extract(self, url):
  26. mobj = re.match(self._VALID_URL, url)
  27. video_id = mobj.group('id')
  28. raw_page = self._download_webpage(
  29. 'http://techtv.mit.edu/videos/%s' % video_id, video_id)
  30. clean_page = re.compile(r'<!--.*?-->', re.S).sub('', raw_page)
  31. base_url = self._search_regex(
  32. r'ipadUrl: \'(.+?cloudfront.net/)', raw_page, 'base url')
  33. formats_json = self._search_regex(
  34. r'bitrates: (\[.+?\])', raw_page, 'video formats')
  35. formats_mit = json.loads(formats_json)
  36. formats = [
  37. {
  38. 'format_id': f['label'],
  39. 'url': base_url + f['url'].partition(':')[2],
  40. 'ext': f['url'].partition(':')[0],
  41. 'format': f['label'],
  42. 'width': f['width'],
  43. 'vbr': f['bitrate'],
  44. }
  45. for f in formats_mit
  46. ]
  47. title = get_element_by_id('edit-title', clean_page)
  48. description = clean_html(get_element_by_id('edit-description', clean_page))
  49. thumbnail = self._search_regex(
  50. r'playlist:.*?url: \'(.+?)\'',
  51. raw_page, 'thumbnail', flags=re.DOTALL)
  52. return {
  53. 'id': video_id,
  54. 'title': title,
  55. 'formats': formats,
  56. 'description': description,
  57. 'thumbnail': thumbnail,
  58. }
  59. class MITIE(TechTVMITIE):
  60. IE_NAME = 'video.mit.edu'
  61. _VALID_URL = r'https?://video\.mit\.edu/watch/(?P<title>[^/]+)'
  62. _TEST = {
  63. 'url': 'http://video.mit.edu/watch/the-government-is-profiling-you-13222/',
  64. 'file': '.mp4',
  65. 'md5': '7db01d5ccc1895fc5010e9c9e13648da',
  66. 'info_dict': {
  67. 'id': '21783',
  68. 'ext': 'mp4',
  69. 'title': 'The Government is Profiling You',
  70. 'description': 'md5:ad5795fe1e1623b73620dbfd47df9afd',
  71. },
  72. }
  73. def _real_extract(self, url):
  74. mobj = re.match(self._VALID_URL, url)
  75. page_title = mobj.group('title')
  76. webpage = self._download_webpage(url, page_title)
  77. embed_url = self._search_regex(
  78. r'<iframe .*?src="(.+?)"', webpage, 'embed url')
  79. return self.url_result(embed_url, ie='TechTVMIT')
  80. class OCWMITIE(InfoExtractor):
  81. IE_NAME = 'ocw.mit.edu'
  82. _VALID_URL = r'^http://ocw\.mit\.edu/courses/(?P<topic>[a-z0-9\-]+)'
  83. _BASE_URL = 'http://ocw.mit.edu/'
  84. _TESTS = [
  85. {
  86. '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/',
  87. 'info_dict': {
  88. 'id': 'EObHWIEKGjA',
  89. 'ext': 'mp4',
  90. 'title': 'Lecture 7: Multiple Discrete Random Variables: Expectations, Conditioning, Independence',
  91. 'description': 'In this lecture, the professor discussed multiple random variables, expectations, and binomial distribution.',
  92. #'subtitles': '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/MIT6_041F11_lec07_300k.mp4.srt'
  93. }
  94. },
  95. {
  96. '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/',
  97. 'info_dict': {
  98. 'id': '7K1sB05pE0A',
  99. 'ext': 'mp4',
  100. 'title': 'Session 1: Introduction to Derivatives',
  101. 'description': 'This section contains lecture video excerpts, lecture notes, an interactive mathlet with supporting documents, and problem solving videos.',
  102. #'subtitles': 'http://ocw.mit.edu//courses/mathematics/18-01sc-single-variable-calculus-fall-2010/ocw-18.01-f07-lec01_300k.SRT'
  103. }
  104. }
  105. ]
  106. def _real_extract(self, url):
  107. mobj = re.match(self._VALID_URL, url)
  108. topic = mobj.group('topic')
  109. webpage = self._download_webpage(url, topic)
  110. title = self._html_search_meta('WT.cg_s', webpage)
  111. description = self._html_search_meta('Description', webpage)
  112. # search for call to ocw_embed_chapter_media(container_id, media_url, provider, page_url, image_url, start, stop, captions_file)
  113. embed_chapter_media = re.search(r'ocw_embed_chapter_media\((.+?)\)', webpage)
  114. if embed_chapter_media:
  115. metadata = re.sub(r'[\'"]', '', embed_chapter_media.group(1))
  116. metadata = re.split(r', ?', metadata)
  117. yt = metadata[1]
  118. subs = compat_urlparse.urljoin(self._BASE_URL, metadata[7])
  119. else:
  120. # search for call to ocw_embed_chapter_media(container_id, media_url, provider, page_url, image_url, captions_file)
  121. embed_media = re.search(r'ocw_embed_media\((.+?)\)', webpage)
  122. if embed_media:
  123. metadata = re.sub(r'[\'"]', '', embed_media.group(1))
  124. metadata = re.split(r', ?', metadata)
  125. yt = metadata[1]
  126. subs = compat_urlparse.urljoin(self._BASE_URL, metadata[5])
  127. else:
  128. raise ExtractorError('Unable to find embedded YouTube video.')
  129. video_id = YoutubeIE.extract_id(yt)
  130. return {
  131. '_type': 'url_transparent',
  132. 'id': video_id,
  133. 'title': title,
  134. 'description': description,
  135. 'url': yt,
  136. 'url_transparent'
  137. 'subtitles': subs,
  138. 'ie_key': 'Youtube',
  139. }