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.

142 lines
5.2 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. fixed_subtitles = {}
  55. for k, v in subtitles.items():
  56. subs = json.loads(v)
  57. if len(subs) == 0:
  58. continue
  59. srt = ''
  60. for pos in range(0, len(subs) - 1):
  61. seq_current = subs[pos]
  62. m_current = re.match(self._TIMECODE_REGEX, seq_current['Timecode'])
  63. if m_current is None:
  64. continue
  65. seq_next = subs[pos + 1]
  66. m_next = re.match(self._TIMECODE_REGEX, seq_next['Timecode'])
  67. if m_next is None:
  68. continue
  69. appear_time = m_current.group('timecode')
  70. disappear_time = m_next.group('timecode')
  71. text = seq_current['Caption']
  72. srt += '%s\r\n%s --> %s\r\n%s' % (str(pos), appear_time, disappear_time, text)
  73. if srt:
  74. fixed_subtitles[k] = srt
  75. return fixed_subtitles
  76. def _get_available_subtitles(self, video_id, webpage):
  77. url = 'http://www.lynda.com/ajax/player?videoId=%s&type=transcript' % video_id
  78. sub = self._download_webpage(url, None, note=False)
  79. sub_json = json.loads(sub)
  80. return {'en': url} if len(sub_json) > 0 else {}
  81. class LyndaCourseIE(InfoExtractor):
  82. IE_NAME = 'lynda:course'
  83. IE_DESC = 'lynda.com online courses'
  84. # Course link equals to welcome/introduction video link of same course
  85. # We will recognize it as course link
  86. _VALID_URL = r'https?://(?:www|m)\.lynda\.com/(?P<coursepath>[^/]+/[^/]+/(?P<courseid>\d+))-\d\.html'
  87. def _real_extract(self, url):
  88. mobj = re.match(self._VALID_URL, url)
  89. course_path = mobj.group('coursepath')
  90. course_id = mobj.group('courseid')
  91. page = self._download_webpage('http://www.lynda.com/ajax/player?courseId=%s&type=course' % course_id,
  92. course_id, 'Downloading course JSON')
  93. course_json = json.loads(page)
  94. if 'Status' in course_json and course_json['Status'] == 'NotFound':
  95. raise ExtractorError('Course %s does not exist' % course_id, expected=True)
  96. unaccessible_videos = 0
  97. videos = []
  98. for chapter in course_json['Chapters']:
  99. for video in chapter['Videos']:
  100. if video['HasAccess'] is not True:
  101. unaccessible_videos += 1
  102. continue
  103. videos.append(video['ID'])
  104. if unaccessible_videos > 0:
  105. self._downloader.report_warning('%s videos are only available for members and will not be downloaded' % unaccessible_videos)
  106. entries = [
  107. self.url_result('http://www.lynda.com/%s/%s-4.html' %
  108. (course_path, video_id),
  109. 'Lynda')
  110. for video_id in videos]
  111. course_title = course_json['Title']
  112. return self.playlist_result(entries, course_id, course_title)