mit.py 6.3 KB

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