+class LyndaIE(LyndaBaseIE):
+ IE_NAME = 'lynda'
+ IE_DESC = 'lynda.com videos'
+ _VALID_URL = r'https?://www\.lynda\.com/(?:[^/]+/[^/]+/\d+|player/embed)/(?P<id>\d+)'
+ _NETRC_MACHINE = 'lynda'
+
+ _TIMECODE_REGEX = r'\[(?P<timecode>\d+:\d+:\d+[\.,]\d+)\]'
+
+ _TESTS = [{
+ 'url': 'http://www.lynda.com/Bootstrap-tutorials/Using-exercise-files/110885/114408-4.html',
+ 'md5': 'ecfc6862da89489161fb9cd5f5a6fac1',
+ 'info_dict': {
+ 'id': '114408',
+ 'ext': 'mp4',
+ 'title': 'Using the exercise files',
+ 'duration': 68
+ }
+ }, {
+ 'url': 'https://www.lynda.com/player/embed/133770?tr=foo=1;bar=g;fizz=rt&fs=0',
+ 'only_matching': True,
+ }]
+
+ def _real_extract(self, url):
+ video_id = self._match_id(url)
+
+ page = self._download_webpage(
+ 'http://www.lynda.com/ajax/player?videoId=%s&type=video' % video_id,
+ video_id, 'Downloading video JSON')
+ video_json = json.loads(page)
+
+ if 'Status' in video_json:
+ raise ExtractorError(
+ 'lynda returned error: %s' % video_json['Message'], expected=True)
+
+ if video_json['HasAccess'] is False:
+ raise ExtractorError(
+ 'Video %s is only available for members. '
+ % video_id + self._ACCOUNT_CREDENTIALS_HINT, expected=True)
+
+ video_id = compat_str(video_json['ID'])
+ duration = video_json['DurationInSeconds']
+ title = video_json['Title']
+
+ formats = []
+
+ fmts = video_json.get('Formats')
+ if fmts:
+ formats.extend([
+ {
+ 'url': fmt['Url'],
+ 'ext': fmt['Extension'],
+ 'width': fmt['Width'],
+ 'height': fmt['Height'],
+ 'filesize': fmt['FileSize'],
+ 'format_id': str(fmt['Resolution'])
+ } for fmt in fmts])
+
+ prioritized_streams = video_json.get('PrioritizedStreams')
+ if prioritized_streams:
+ formats.extend([
+ {
+ 'url': video_url,
+ 'width': int_or_none(format_id),
+ 'format_id': format_id,
+ } for format_id, video_url in prioritized_streams['0'].items()
+ ])
+
+ self._check_formats(formats, video_id)
+ self._sort_formats(formats)
+
+ subtitles = self.extract_subtitles(video_id, page)
+
+ return {
+ 'id': video_id,
+ 'title': title,
+ 'duration': duration,
+ 'subtitles': subtitles,
+ 'formats': formats
+ }
+
+ def _fix_subtitles(self, subs):
+ srt = ''
+ seq_counter = 0
+ for pos in range(0, len(subs) - 1):
+ seq_current = subs[pos]
+ m_current = re.match(self._TIMECODE_REGEX, seq_current['Timecode'])
+ if m_current is None: