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