]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/stanfordoc.py
[ie/youtube] Suppress "Unavailable videos are hidden" warning (#10159)
[yt-dlp.git] / yt_dlp / extractor / stanfordoc.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5 ExtractorError,
6 orderedSet,
7 unescapeHTML,
8 )
9
10
11 class StanfordOpenClassroomIE(InfoExtractor):
12 IE_NAME = 'stanfordoc'
13 IE_DESC = 'Stanford Open ClassRoom'
14 _VALID_URL = r'https?://openclassroom\.stanford\.edu(?P<path>/?|(/MainFolder/(?:HomePage|CoursePage|VideoPage)\.php([?]course=(?P<course>[^&]+)(&video=(?P<video>[^&]+))?(&.*)?)?))$'
15 _TEST = {
16 'url': 'http://openclassroom.stanford.edu/MainFolder/VideoPage.php?course=PracticalUnix&video=intro-environment&speed=100',
17 'md5': '544a9468546059d4e80d76265b0443b8',
18 'info_dict': {
19 'id': 'PracticalUnix_intro-environment',
20 'ext': 'mp4',
21 'title': 'Intro Environment',
22 },
23 }
24
25 def _real_extract(self, url):
26 mobj = self._match_valid_url(url)
27
28 if mobj.group('course') and mobj.group('video'): # A specific video
29 course = mobj.group('course')
30 video = mobj.group('video')
31 info = {
32 'id': course + '_' + video,
33 'uploader': None,
34 'upload_date': None,
35 }
36
37 base_url = 'http://openclassroom.stanford.edu/MainFolder/courses/' + course + '/videos/'
38 xml_url = base_url + video + '.xml'
39 mdoc = self._download_xml(xml_url, info['id'])
40 try:
41 info['title'] = mdoc.findall('./title')[0].text
42 info['url'] = base_url + mdoc.findall('./videoFile')[0].text
43 except IndexError:
44 raise ExtractorError('Invalid metadata XML file')
45 return info
46 elif mobj.group('course'): # A course page
47 course = mobj.group('course')
48 info = {
49 'id': course,
50 '_type': 'playlist',
51 'uploader': None,
52 'upload_date': None,
53 }
54
55 coursepage = self._download_webpage(
56 url, info['id'],
57 note='Downloading course info page',
58 errnote='Unable to download course info page')
59
60 info['title'] = self._html_search_regex(
61 r'<h1>([^<]+)</h1>', coursepage, 'title', default=info['id'])
62
63 info['description'] = self._html_search_regex(
64 r'(?s)<description>([^<]+)</description>',
65 coursepage, 'description', fatal=False)
66
67 links = orderedSet(re.findall(r'<a href="(VideoPage\.php\?[^"]+)">', coursepage))
68 info['entries'] = [self.url_result(
69 f'http://openclassroom.stanford.edu/MainFolder/{unescapeHTML(l)}',
70 ) for l in links]
71 return info
72 else: # Root page
73 info = {
74 'id': 'Stanford OpenClassroom',
75 '_type': 'playlist',
76 'uploader': None,
77 'upload_date': None,
78 }
79 info['title'] = info['id']
80
81 root_url = 'http://openclassroom.stanford.edu/MainFolder/HomePage.php'
82 rootpage = self._download_webpage(root_url, info['id'],
83 errnote='Unable to download course info page')
84
85 links = orderedSet(re.findall(r'<a href="(CoursePage\.php\?[^"]+)">', rootpage))
86 info['entries'] = [self.url_result(
87 f'http://openclassroom.stanford.edu/MainFolder/{unescapeHTML(l)}',
88 ) for l in links]
89 return info