You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
3.4 KiB

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