[lynda] Add support for lynda.com (#1966)
[youtube-dl] / youtube_dl / extractor / lynda.py
1 import re
2 import json
3
4 from .common import InfoExtractor
5 from ..utils import ExtractorError
6
7
8 class LyndaIE(InfoExtractor):
9     IE_NAME = u'lynda'
10     IE_DESC = u'lynda.com videos'
11     _VALID_URL = r'https?://www\.lynda\.com/[^/]+/[^/]+/\d+/(\d+)-\d\.html'
12
13     _TEST = {
14         u'url': u'http://www.lynda.com/Bootstrap-tutorials/Using-exercise-files/110885/114408-4.html',
15         u'file': u'114408.mp4',
16         u'md5': u'ecfc6862da89489161fb9cd5f5a6fac1',
17         u"info_dict": {
18             u'title': u'Using the exercise files',
19             u'duration': 68
20         }
21     }
22
23     def _real_extract(self, url):
24         mobj = re.match(self._VALID_URL, url)
25         video_id = mobj.group(1)
26
27         page = self._download_webpage('http://www.lynda.com/ajax/player?videoId=%s&type=video' % video_id,
28                                       video_id, u'Downloading video JSON')
29         video_json = json.loads(page)
30
31         if u'Status' in video_json and video_json[u'Status'] == u'NotFound':
32             raise ExtractorError(u'Video %s does not exist' % video_id, expected=True)
33
34         if video_json[u'HasAccess'] is False:
35             raise ExtractorError(u'Video %s is only available for members' % video_id, expected=True)
36
37         video_id = video_json[u'ID']
38         duration = video_json[u'DurationInSeconds']
39         title = video_json[u'Title']
40
41         formats = [{'url': fmt[u'Url'],
42                     'ext': fmt[u'Extension'],
43                     'width': fmt[u'Width'],
44                     'height': fmt[u'Height'],
45                     'filesize': fmt[u'FileSize'],
46                     'format_id': fmt[u'Resolution']
47                     } for fmt in video_json[u'Formats']]
48
49         self._sort_formats(formats)
50
51         return {
52             'id': video_id,
53             'title': title,
54             'duration': duration,
55             'formats': formats
56         }
57
58
59 class LyndaCourseIE(InfoExtractor):
60     IE_NAME = u'lynda:course'
61     IE_DESC = u'lynda.com online courses'
62
63     # Course link equals to welcome/introduction video link of same course
64     # We will recognize it as course link
65     _VALID_URL = r'https?://(?:www|m)\.lynda\.com/(?P<coursepath>[^/]+/[^/]+/(?P<courseid>\d+))-\d\.html'
66
67     def _real_extract(self, url):
68         mobj = re.match(self._VALID_URL, url)
69         course_path = mobj.group('coursepath')
70         course_id = mobj.group('courseid')
71
72         page = self._download_webpage('http://www.lynda.com/ajax/player?courseId=%s&type=course' % course_id,
73                                       course_id, u'Downloading course JSON')
74         course_json = json.loads(page)
75
76         if u'Status' in course_json and course_json[u'Status'] == u'NotFound':
77             raise ExtractorError(u'Course %s does not exist' % course_id, expected=True)
78
79         unaccessible_videos = 0
80         videos = []
81
82         for chapter in course_json[u'Chapters']:
83             for video in chapter[u'Videos']:
84                 if video[u'HasAccess'] is not True:
85                     unaccessible_videos += 1
86                     continue
87                 videos.append(video[u'ID'])
88
89         if unaccessible_videos > 0:
90             self._downloader.report_warning(u'%s videos are only available for members and will not be downloaded' % unaccessible_videos)
91
92         entries = [self.url_result('http://www.lynda.com/%s/%s-4.html' % (course_path, video_id), 'Lynda') for video_id in videos]
93
94         course_title = course_json[u'Title']
95
96         return self.playlist_result(entries, course_id, course_title)