1 from __future__ import unicode_literals
6 from .subtitles import SubtitlesInfoExtractor
7 from .common import InfoExtractor
8 from ..utils import ExtractorError
11 class LyndaIE(SubtitlesInfoExtractor):
13 IE_DESC = 'lynda.com videos'
14 _VALID_URL = r'https?://www\.lynda\.com/[^/]+/[^/]+/\d+/(\d+)-\d\.html'
17 'url': 'http://www.lynda.com/Bootstrap-tutorials/Using-exercise-files/110885/114408-4.html',
19 'md5': 'ecfc6862da89489161fb9cd5f5a6fac1',
21 'title': 'Using the exercise files',
26 def _real_extract(self, url):
27 mobj = re.match(self._VALID_URL, url)
28 video_id = mobj.group(1)
30 page = self._download_webpage('http://www.lynda.com/ajax/player?videoId=%s&type=video' % video_id,
31 video_id, 'Downloading video JSON')
32 video_json = json.loads(page)
34 if 'Status' in video_json and video_json['Status'] == 'NotFound':
35 raise ExtractorError('Video %s does not exist' % video_id, expected=True)
37 if video_json['HasAccess'] is False:
38 raise ExtractorError('Video %s is only available for members' % video_id, expected=True)
40 video_id = video_json['ID']
41 duration = video_json['DurationInSeconds']
42 title = video_json['Title']
44 formats = [{'url': fmt['Url'],
45 'ext': fmt['Extension'],
46 'width': fmt['Width'],
47 'height': fmt['Height'],
48 'filesize': fmt['FileSize'],
49 'format_id': str(fmt['Resolution'])
50 } for fmt in video_json['Formats']]
52 self._sort_formats(formats)
54 if self._downloader.params.get('listsubtitles', False):
55 self._list_available_subtitles(video_id, page)
58 subtitles = self._fix_subtitles(self.extract_subtitles(video_id, page))
64 'subtitles': subtitles,
68 _TIMECODE_REGEX = r'\[(?P<timecode>\d+:\d+:\d+[\.,]\d+)\]'
70 def _fix_subtitles(self, subtitles):
72 for k, v in subtitles.items():
77 for pos in range(0, len(subs) - 1):
78 seq_current = subs[pos]
79 m_current = re.match(self._TIMECODE_REGEX, seq_current['Timecode'])
82 seq_next = subs[pos+1]
83 m_next = re.match(self._TIMECODE_REGEX, seq_next['Timecode'])
86 appear_time = m_current.group('timecode')
87 disappear_time = m_next.group('timecode')
88 text = seq_current['Caption']
89 srt += '%s\r\n%s --> %s\r\n%s' % (str(pos), appear_time, disappear_time, text)
91 fixed_subtitles[k] = srt
92 return fixed_subtitles
94 def _get_available_subtitles(self, video_id, webpage):
95 url = 'http://www.lynda.com/ajax/player?videoId=%s&type=transcript' % video_id
96 sub = self._download_webpage(url, None, note=False)
97 sub_json = json.loads(sub)
98 return {'en': url} if len(sub_json) > 0 else {}
101 class LyndaCourseIE(InfoExtractor):
102 IE_NAME = 'lynda:course'
103 IE_DESC = 'lynda.com online courses'
105 # Course link equals to welcome/introduction video link of same course
106 # We will recognize it as course link
107 _VALID_URL = r'https?://(?:www|m)\.lynda\.com/(?P<coursepath>[^/]+/[^/]+/(?P<courseid>\d+))-\d\.html'
109 def _real_extract(self, url):
110 mobj = re.match(self._VALID_URL, url)
111 course_path = mobj.group('coursepath')
112 course_id = mobj.group('courseid')
114 page = self._download_webpage('http://www.lynda.com/ajax/player?courseId=%s&type=course' % course_id,
115 course_id, 'Downloading course JSON')
116 course_json = json.loads(page)
118 if 'Status' in course_json and course_json['Status'] == 'NotFound':
119 raise ExtractorError('Course %s does not exist' % course_id, expected=True)
121 unaccessible_videos = 0
124 for chapter in course_json['Chapters']:
125 for video in chapter['Videos']:
126 if video['HasAccess'] is not True:
127 unaccessible_videos += 1
129 videos.append(video['ID'])
131 if unaccessible_videos > 0:
132 self._downloader.report_warning('%s videos are only available for members and will not be downloaded' % unaccessible_videos)
135 self.url_result('http://www.lynda.com/%s/%s-4.html' %
136 (course_path, video_id),
138 for video_id in videos]
140 course_title = course_json['Title']
142 return self.playlist_result(entries, course_id, course_title)