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.

145 lines
5.3 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. import json
  4. from .subtitles import SubtitlesInfoExtractor
  5. from .common import InfoExtractor
  6. from ..utils import ExtractorError
  7. class LyndaIE(SubtitlesInfoExtractor):
  8. IE_NAME = 'lynda'
  9. IE_DESC = 'lynda.com videos'
  10. _VALID_URL = r'https?://www\.lynda\.com/[^/]+/[^/]+/\d+/(\d+)-\d\.html'
  11. _TEST = {
  12. 'url': 'http://www.lynda.com/Bootstrap-tutorials/Using-exercise-files/110885/114408-4.html',
  13. 'file': '114408.mp4',
  14. 'md5': 'ecfc6862da89489161fb9cd5f5a6fac1',
  15. 'info_dict': {
  16. 'title': 'Using the exercise files',
  17. 'duration': 68
  18. }
  19. }
  20. def _real_extract(self, url):
  21. mobj = re.match(self._VALID_URL, url)
  22. video_id = mobj.group(1)
  23. page = self._download_webpage('http://www.lynda.com/ajax/player?videoId=%s&type=video' % video_id,
  24. video_id, 'Downloading video JSON')
  25. video_json = json.loads(page)
  26. if 'Status' in video_json and video_json['Status'] == 'NotFound':
  27. raise ExtractorError('Video %s does not exist' % video_id, expected=True)
  28. if video_json['HasAccess'] is False:
  29. raise ExtractorError('Video %s is only available for members' % video_id, expected=True)
  30. video_id = video_json['ID']
  31. duration = video_json['DurationInSeconds']
  32. title = video_json['Title']
  33. formats = [{'url': fmt['Url'],
  34. 'ext': fmt['Extension'],
  35. 'width': fmt['Width'],
  36. 'height': fmt['Height'],
  37. 'filesize': fmt['FileSize'],
  38. 'format_id': str(fmt['Resolution'])
  39. } for fmt in video_json['Formats']]
  40. self._sort_formats(formats)
  41. if self._downloader.params.get('listsubtitles', False):
  42. self._list_available_subtitles(video_id, page)
  43. return
  44. subtitles = self._fix_subtitles(self.extract_subtitles(video_id, page))
  45. return {
  46. 'id': video_id,
  47. 'title': title,
  48. 'duration': duration,
  49. 'subtitles': subtitles,
  50. 'formats': formats
  51. }
  52. _TIMECODE_REGEX = r'\[(?P<timecode>\d+:\d+:\d+[\.,]\d+)\]'
  53. def _fix_subtitles(self, subtitles):
  54. if subtitles is None:
  55. return subtitles # subtitles not requested
  56. fixed_subtitles = {}
  57. for k, v in subtitles.items():
  58. subs = json.loads(v)
  59. if len(subs) == 0:
  60. continue
  61. srt = ''
  62. for pos in range(0, len(subs) - 1):
  63. seq_current = subs[pos]
  64. m_current = re.match(self._TIMECODE_REGEX, seq_current['Timecode'])
  65. if m_current is None:
  66. continue
  67. seq_next = subs[pos + 1]
  68. m_next = re.match(self._TIMECODE_REGEX, seq_next['Timecode'])
  69. if m_next is None:
  70. continue
  71. appear_time = m_current.group('timecode')
  72. disappear_time = m_next.group('timecode')
  73. text = seq_current['Caption']
  74. srt += '%s\r\n%s --> %s\r\n%s' % (str(pos), appear_time, disappear_time, text)
  75. if srt:
  76. fixed_subtitles[k] = srt
  77. return fixed_subtitles
  78. def _get_available_subtitles(self, video_id, webpage):
  79. url = 'http://www.lynda.com/ajax/player?videoId=%s&type=transcript' % video_id
  80. sub = self._download_webpage(url, None, note=False)
  81. sub_json = json.loads(sub)
  82. return {'en': url} if len(sub_json) > 0 else {}
  83. class LyndaCourseIE(InfoExtractor):
  84. IE_NAME = 'lynda:course'
  85. IE_DESC = 'lynda.com online courses'
  86. # Course link equals to welcome/introduction video link of same course
  87. # We will recognize it as course link
  88. _VALID_URL = r'https?://(?:www|m)\.lynda\.com/(?P<coursepath>[^/]+/[^/]+/(?P<courseid>\d+))-\d\.html'
  89. def _real_extract(self, url):
  90. mobj = re.match(self._VALID_URL, url)
  91. course_path = mobj.group('coursepath')
  92. course_id = mobj.group('courseid')
  93. page = self._download_webpage('http://www.lynda.com/ajax/player?courseId=%s&type=course' % course_id,
  94. course_id, 'Downloading course JSON')
  95. course_json = json.loads(page)
  96. if 'Status' in course_json and course_json['Status'] == 'NotFound':
  97. raise ExtractorError('Course %s does not exist' % course_id, expected=True)
  98. unaccessible_videos = 0
  99. videos = []
  100. for chapter in course_json['Chapters']:
  101. for video in chapter['Videos']:
  102. if video['HasAccess'] is not True:
  103. unaccessible_videos += 1
  104. continue
  105. videos.append(video['ID'])
  106. if unaccessible_videos > 0:
  107. self._downloader.report_warning('%s videos are only available for members and will not be downloaded' % unaccessible_videos)
  108. entries = [
  109. self.url_result('http://www.lynda.com/%s/%s-4.html' %
  110. (course_path, video_id),
  111. 'Lynda')
  112. for video_id in videos]
  113. course_title = course_json['Title']
  114. return self.playlist_result(entries, course_id, course_title)