]>
Commit | Line | Data |
---|---|---|
aa7e974a RA |
1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
4 | import re | |
5 | ||
6 | from .common import InfoExtractor | |
7 | from ..utils import ( | |
8 | ExtractorError, | |
9 | float_or_none, | |
10 | int_or_none, | |
11 | urlencode_postdata, | |
c9120294 | 12 | urljoin, |
aa7e974a RA |
13 | ) |
14 | ||
15 | ||
16 | class LinkedInLearningBaseIE(InfoExtractor): | |
17 | _NETRC_MACHINE = 'linkedin' | |
c9120294 | 18 | _LOGIN_URL = 'https://www.linkedin.com/uas/login?trk=learning' |
aa7e974a RA |
19 | |
20 | def _call_api(self, course_slug, fields, video_slug=None, resolution=None): | |
21 | query = { | |
22 | 'courseSlug': course_slug, | |
23 | 'fields': fields, | |
24 | 'q': 'slugs', | |
25 | } | |
26 | sub = '' | |
27 | if video_slug: | |
28 | query.update({ | |
29 | 'videoSlug': video_slug, | |
30 | 'resolution': '_%s' % resolution, | |
31 | }) | |
32 | sub = ' %dp' % resolution | |
33 | api_url = 'https://www.linkedin.com/learning-api/detailedCourses' | |
34 | return self._download_json( | |
35 | api_url, video_slug, 'Downloading%s JSON metadata' % sub, headers={ | |
36 | 'Csrf-Token': self._get_cookies(api_url)['JSESSIONID'].value, | |
37 | }, query=query)['elements'][0] | |
38 | ||
91effe22 RA |
39 | def _get_urn_id(self, video_data): |
40 | urn = video_data.get('urn') | |
aa7e974a RA |
41 | if urn: |
42 | mobj = re.search(r'urn:li:lyndaCourse:\d+,(\d+)', urn) | |
43 | if mobj: | |
44 | return mobj.group(1) | |
91effe22 RA |
45 | |
46 | def _get_video_id(self, video_data, course_slug, video_slug): | |
47 | return self._get_urn_id(video_data) or '%s/%s' % (course_slug, video_slug) | |
aa7e974a RA |
48 | |
49 | def _real_initialize(self): | |
50 | email, password = self._get_login_info() | |
51 | if email is None: | |
52 | return | |
53 | ||
54 | login_page = self._download_webpage( | |
c9120294 RA |
55 | self._LOGIN_URL, None, 'Downloading login page') |
56 | action_url = urljoin(self._LOGIN_URL, self._search_regex( | |
aa7e974a | 57 | r'<form[^>]+action=(["\'])(?P<url>.+?)\1', login_page, 'post url', |
c9120294 | 58 | default='https://www.linkedin.com/uas/login-submit', group='url')) |
aa7e974a RA |
59 | data = self._hidden_inputs(login_page) |
60 | data.update({ | |
61 | 'session_key': email, | |
62 | 'session_password': password, | |
63 | }) | |
64 | login_submit_page = self._download_webpage( | |
65 | action_url, None, 'Logging in', | |
66 | data=urlencode_postdata(data)) | |
67 | error = self._search_regex( | |
68 | r'<span[^>]+class="error"[^>]*>\s*(.+?)\s*</span>', | |
69 | login_submit_page, 'error', default=None) | |
70 | if error: | |
71 | raise ExtractorError(error, expected=True) | |
72 | ||
73 | ||
74 | class LinkedInLearningIE(LinkedInLearningBaseIE): | |
75 | IE_NAME = 'linkedin:learning' | |
76 | _VALID_URL = r'https?://(?:www\.)?linkedin\.com/learning/(?P<course_slug>[^/]+)/(?P<id>[^/?#]+)' | |
77 | _TEST = { | |
78 | 'url': 'https://www.linkedin.com/learning/programming-foundations-fundamentals/welcome?autoplay=true', | |
79 | 'md5': 'a1d74422ff0d5e66a792deb996693167', | |
80 | 'info_dict': { | |
81 | 'id': '90426', | |
82 | 'ext': 'mp4', | |
83 | 'title': 'Welcome', | |
84 | 'timestamp': 1430396150.82, | |
85 | 'upload_date': '20150430', | |
86 | }, | |
87 | } | |
88 | ||
89 | def _real_extract(self, url): | |
5ad28e7f | 90 | course_slug, video_slug = self._match_valid_url(url).groups() |
aa7e974a RA |
91 | |
92 | video_data = None | |
93 | formats = [] | |
94 | for width, height in ((640, 360), (960, 540), (1280, 720)): | |
95 | video_data = self._call_api( | |
96 | course_slug, 'selectedVideo', video_slug, height)['selectedVideo'] | |
97 | ||
98 | video_url_data = video_data.get('url') or {} | |
99 | progressive_url = video_url_data.get('progressiveUrl') | |
100 | if progressive_url: | |
101 | formats.append({ | |
102 | 'format_id': 'progressive-%dp' % height, | |
103 | 'url': progressive_url, | |
104 | 'height': height, | |
105 | 'width': width, | |
106 | 'source_preference': 1, | |
107 | }) | |
108 | ||
109 | title = video_data['title'] | |
110 | ||
111 | audio_url = video_data.get('audio', {}).get('progressiveUrl') | |
112 | if audio_url: | |
113 | formats.append({ | |
114 | 'abr': 64, | |
115 | 'ext': 'm4a', | |
116 | 'format_id': 'audio', | |
117 | 'url': audio_url, | |
118 | 'vcodec': 'none', | |
119 | }) | |
120 | ||
121 | streaming_url = video_url_data.get('streamingUrl') | |
122 | if streaming_url: | |
123 | formats.extend(self._extract_m3u8_formats( | |
124 | streaming_url, video_slug, 'mp4', | |
125 | 'm3u8_native', m3u8_id='hls', fatal=False)) | |
126 | ||
54f37eea | 127 | # It seems like this would be correctly handled by default |
128 | # However, unless someone can confirm this, the old | |
129 | # behaviour is being kept as-is | |
130 | self._sort_formats(formats, ('res', 'source_preference')) | |
aa7e974a RA |
131 | |
132 | return { | |
91effe22 | 133 | 'id': self._get_video_id(video_data, course_slug, video_slug), |
aa7e974a RA |
134 | 'title': title, |
135 | 'formats': formats, | |
136 | 'thumbnail': video_data.get('defaultThumbnail'), | |
137 | 'timestamp': float_or_none(video_data.get('publishedOn'), 1000), | |
138 | 'duration': int_or_none(video_data.get('durationInSeconds')), | |
139 | } | |
140 | ||
141 | ||
142 | class LinkedInLearningCourseIE(LinkedInLearningBaseIE): | |
143 | IE_NAME = 'linkedin:learning:course' | |
144 | _VALID_URL = r'https?://(?:www\.)?linkedin\.com/learning/(?P<id>[^/?#]+)' | |
145 | _TEST = { | |
146 | 'url': 'https://www.linkedin.com/learning/programming-foundations-fundamentals', | |
147 | 'info_dict': { | |
148 | 'id': 'programming-foundations-fundamentals', | |
149 | 'title': 'Programming Foundations: Fundamentals', | |
150 | 'description': 'md5:76e580b017694eb89dc8e8923fff5c86', | |
151 | }, | |
152 | 'playlist_mincount': 61, | |
153 | } | |
154 | ||
155 | @classmethod | |
156 | def suitable(cls, url): | |
157 | return False if LinkedInLearningIE.suitable(url) else super(LinkedInLearningCourseIE, cls).suitable(url) | |
158 | ||
159 | def _real_extract(self, url): | |
160 | course_slug = self._match_id(url) | |
161 | course_data = self._call_api(course_slug, 'chapters,description,title') | |
162 | ||
163 | entries = [] | |
91effe22 | 164 | for chapter_number, chapter in enumerate(course_data.get('chapters', []), 1): |
aa7e974a | 165 | chapter_title = chapter.get('title') |
91effe22 | 166 | chapter_id = self._get_urn_id(chapter) |
aa7e974a RA |
167 | for video in chapter.get('videos', []): |
168 | video_slug = video.get('slug') | |
169 | if not video_slug: | |
170 | continue | |
171 | entries.append({ | |
b1447572 | 172 | '_type': 'url_transparent', |
91effe22 | 173 | 'id': self._get_video_id(video, course_slug, video_slug), |
aa7e974a RA |
174 | 'title': video.get('title'), |
175 | 'url': 'https://www.linkedin.com/learning/%s/%s' % (course_slug, video_slug), | |
176 | 'chapter': chapter_title, | |
91effe22 RA |
177 | 'chapter_number': chapter_number, |
178 | 'chapter_id': chapter_id, | |
aa7e974a RA |
179 | 'ie_key': LinkedInLearningIE.ie_key(), |
180 | }) | |
181 | ||
182 | return self.playlist_result( | |
183 | entries, course_slug, | |
184 | course_data.get('title'), | |
185 | course_data.get('description')) |