logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

mit.py (5210B)


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