stanfordoc.py 3.4 KB

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