]>
Commit | Line | Data |
---|---|---|
aa7e974a RA |
1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
8dc831f7 | 4 | from itertools import zip_longest |
aa7e974a RA |
5 | import re |
6 | ||
7 | from .common import InfoExtractor | |
8 | from ..utils import ( | |
402cd603 | 9 | clean_html, |
10 | extract_attributes, | |
aa7e974a RA |
11 | ExtractorError, |
12 | float_or_none, | |
402cd603 | 13 | get_element_by_class, |
aa7e974a | 14 | int_or_none, |
8dc831f7 | 15 | srt_subtitles_timecode, |
402cd603 | 16 | strip_or_none, |
17 | mimetype2ext, | |
8dc831f7 | 18 | try_get, |
aa7e974a | 19 | urlencode_postdata, |
c9120294 | 20 | urljoin, |
aa7e974a RA |
21 | ) |
22 | ||
23 | ||
402cd603 | 24 | class LinkedInBaseIE(InfoExtractor): |
aa7e974a | 25 | _NETRC_MACHINE = 'linkedin' |
da483200 | 26 | _logged_in = False |
aa7e974a | 27 | |
52efa4b3 | 28 | def _perform_login(self, username, password): |
402cd603 | 29 | if self._logged_in: |
30 | return | |
402cd603 | 31 | |
32 | login_page = self._download_webpage( | |
33 | self._LOGIN_URL, None, 'Downloading login page') | |
34 | action_url = urljoin(self._LOGIN_URL, self._search_regex( | |
35 | r'<form[^>]+action=(["\'])(?P<url>.+?)\1', login_page, 'post url', | |
36 | default='https://www.linkedin.com/uas/login-submit', group='url')) | |
37 | data = self._hidden_inputs(login_page) | |
38 | data.update({ | |
52efa4b3 | 39 | 'session_key': username, |
402cd603 | 40 | 'session_password': password, |
41 | }) | |
42 | login_submit_page = self._download_webpage( | |
43 | action_url, None, 'Logging in', | |
44 | data=urlencode_postdata(data)) | |
45 | error = self._search_regex( | |
46 | r'<span[^>]+class="error"[^>]*>\s*(.+?)\s*</span>', | |
47 | login_submit_page, 'error', default=None) | |
48 | if error: | |
49 | raise ExtractorError(error, expected=True) | |
50 | LinkedInBaseIE._logged_in = True | |
51 | ||
52 | ||
53 | class LinkedInLearningBaseIE(LinkedInBaseIE): | |
54 | _LOGIN_URL = 'https://www.linkedin.com/uas/login?trk=learning' | |
55 | ||
aa7e974a RA |
56 | def _call_api(self, course_slug, fields, video_slug=None, resolution=None): |
57 | query = { | |
58 | 'courseSlug': course_slug, | |
59 | 'fields': fields, | |
60 | 'q': 'slugs', | |
61 | } | |
62 | sub = '' | |
63 | if video_slug: | |
64 | query.update({ | |
65 | 'videoSlug': video_slug, | |
66 | 'resolution': '_%s' % resolution, | |
67 | }) | |
68 | sub = ' %dp' % resolution | |
69 | api_url = 'https://www.linkedin.com/learning-api/detailedCourses' | |
da483200 | 70 | if not self._get_cookies(api_url).get('JSESSIONID'): |
71 | self.raise_login_required() | |
aa7e974a RA |
72 | return self._download_json( |
73 | api_url, video_slug, 'Downloading%s JSON metadata' % sub, headers={ | |
74 | 'Csrf-Token': self._get_cookies(api_url)['JSESSIONID'].value, | |
75 | }, query=query)['elements'][0] | |
76 | ||
91effe22 RA |
77 | def _get_urn_id(self, video_data): |
78 | urn = video_data.get('urn') | |
aa7e974a RA |
79 | if urn: |
80 | mobj = re.search(r'urn:li:lyndaCourse:\d+,(\d+)', urn) | |
81 | if mobj: | |
82 | return mobj.group(1) | |
91effe22 RA |
83 | |
84 | def _get_video_id(self, video_data, course_slug, video_slug): | |
85 | return self._get_urn_id(video_data) or '%s/%s' % (course_slug, video_slug) | |
aa7e974a | 86 | |
aa7e974a | 87 | |
402cd603 | 88 | class LinkedInIE(LinkedInBaseIE): |
89 | _VALID_URL = r'https?://(?:www\.)?linkedin\.com/posts/.+?(?P<id>\d+)' | |
90 | _TESTS = [{ | |
91 | 'url': 'https://www.linkedin.com/posts/mishalkhawaja_sendinblueviews-toronto-digitalmarketing-ugcPost-6850898786781339649-mM20', | |
92 | 'info_dict': { | |
93 | 'id': '6850898786781339649', | |
94 | 'ext': 'mp4', | |
95 | 'title': 'Mishal K. on LinkedIn: #sendinblueviews #toronto #digitalmarketing', | |
96 | 'description': 'md5:be125430bab1c574f16aeb186a4d5b19', | |
97 | 'creator': 'Mishal K.' | |
98 | }, | |
99 | }] | |
100 | ||
101 | def _real_extract(self, url): | |
102 | video_id = self._match_id(url) | |
103 | webpage = self._download_webpage(url, video_id) | |
104 | ||
105 | title = self._html_search_regex(r'<title>([^<]+)</title>', webpage, 'title') | |
106 | description = clean_html(get_element_by_class('share-update-card__update-text', webpage)) | |
107 | like_count = int_or_none(get_element_by_class('social-counts-reactions__social-counts-numRections', webpage)) | |
108 | creator = strip_or_none(clean_html(get_element_by_class('comment__actor-name', webpage))) | |
9222c381 | 109 | |
402cd603 | 110 | sources = self._parse_json(extract_attributes(self._search_regex(r'(<video[^>]+>)', webpage, 'video'))['data-sources'], video_id) |
111 | formats = [{ | |
112 | 'url': source['src'], | |
113 | 'ext': mimetype2ext(source.get('type')), | |
114 | 'tbr': float_or_none(source.get('data-bitrate'), scale=1000), | |
115 | } for source in sources] | |
116 | ||
117 | self._sort_formats(formats) | |
118 | ||
119 | return { | |
120 | 'id': video_id, | |
121 | 'formats': formats, | |
122 | 'title': title, | |
123 | 'like_count': like_count, | |
124 | 'creator': creator, | |
125 | 'thumbnail': self._og_search_thumbnail(webpage), | |
126 | 'description': description, | |
127 | } | |
aa7e974a RA |
128 | |
129 | ||
130 | class LinkedInLearningIE(LinkedInLearningBaseIE): | |
131 | IE_NAME = 'linkedin:learning' | |
132 | _VALID_URL = r'https?://(?:www\.)?linkedin\.com/learning/(?P<course_slug>[^/]+)/(?P<id>[^/?#]+)' | |
133 | _TEST = { | |
134 | 'url': 'https://www.linkedin.com/learning/programming-foundations-fundamentals/welcome?autoplay=true', | |
135 | 'md5': 'a1d74422ff0d5e66a792deb996693167', | |
136 | 'info_dict': { | |
137 | 'id': '90426', | |
138 | 'ext': 'mp4', | |
139 | 'title': 'Welcome', | |
140 | 'timestamp': 1430396150.82, | |
141 | 'upload_date': '20150430', | |
142 | }, | |
143 | } | |
144 | ||
8dc831f7 AG |
145 | def json2srt(self, transcript_lines, duration=None): |
146 | srt_data = '' | |
147 | for line, (line_dict, next_dict) in enumerate(zip_longest(transcript_lines, transcript_lines[1:])): | |
148 | start_time, caption = line_dict['transcriptStartAt'] / 1000, line_dict['caption'] | |
149 | end_time = next_dict['transcriptStartAt'] / 1000 if next_dict else duration or start_time + 1 | |
80c360d7 AG |
150 | srt_data += '%d\n%s --> %s\n%s\n\n' % (line + 1, srt_subtitles_timecode(start_time), |
151 | srt_subtitles_timecode(end_time), | |
152 | caption) | |
8dc831f7 AG |
153 | return srt_data |
154 | ||
aa7e974a | 155 | def _real_extract(self, url): |
5ad28e7f | 156 | course_slug, video_slug = self._match_valid_url(url).groups() |
aa7e974a | 157 | |
aa7e974a RA |
158 | formats = [] |
159 | for width, height in ((640, 360), (960, 540), (1280, 720)): | |
160 | video_data = self._call_api( | |
161 | course_slug, 'selectedVideo', video_slug, height)['selectedVideo'] | |
162 | ||
163 | video_url_data = video_data.get('url') or {} | |
164 | progressive_url = video_url_data.get('progressiveUrl') | |
165 | if progressive_url: | |
166 | formats.append({ | |
167 | 'format_id': 'progressive-%dp' % height, | |
168 | 'url': progressive_url, | |
8dc831f7 | 169 | 'ext': 'mp4', |
aa7e974a RA |
170 | 'height': height, |
171 | 'width': width, | |
172 | 'source_preference': 1, | |
173 | }) | |
174 | ||
175 | title = video_data['title'] | |
176 | ||
177 | audio_url = video_data.get('audio', {}).get('progressiveUrl') | |
178 | if audio_url: | |
179 | formats.append({ | |
180 | 'abr': 64, | |
181 | 'ext': 'm4a', | |
182 | 'format_id': 'audio', | |
183 | 'url': audio_url, | |
184 | 'vcodec': 'none', | |
185 | }) | |
186 | ||
187 | streaming_url = video_url_data.get('streamingUrl') | |
188 | if streaming_url: | |
189 | formats.extend(self._extract_m3u8_formats( | |
190 | streaming_url, video_slug, 'mp4', | |
191 | 'm3u8_native', m3u8_id='hls', fatal=False)) | |
192 | ||
54f37eea | 193 | # It seems like this would be correctly handled by default |
194 | # However, unless someone can confirm this, the old | |
195 | # behaviour is being kept as-is | |
196 | self._sort_formats(formats, ('res', 'source_preference')) | |
8dc831f7 AG |
197 | subtitles = {} |
198 | duration = int_or_none(video_data.get('durationInSeconds')) | |
199 | transcript_lines = try_get(video_data, lambda x: x['transcript']['lines'], expected_type=list) | |
200 | if transcript_lines: | |
201 | subtitles['en'] = [{ | |
202 | 'ext': 'srt', | |
203 | 'data': self.json2srt(transcript_lines, duration) | |
204 | }] | |
aa7e974a RA |
205 | |
206 | return { | |
91effe22 | 207 | 'id': self._get_video_id(video_data, course_slug, video_slug), |
aa7e974a RA |
208 | 'title': title, |
209 | 'formats': formats, | |
210 | 'thumbnail': video_data.get('defaultThumbnail'), | |
211 | 'timestamp': float_or_none(video_data.get('publishedOn'), 1000), | |
8dc831f7 AG |
212 | 'duration': duration, |
213 | 'subtitles': subtitles, | |
aa7e974a RA |
214 | } |
215 | ||
216 | ||
217 | class LinkedInLearningCourseIE(LinkedInLearningBaseIE): | |
218 | IE_NAME = 'linkedin:learning:course' | |
219 | _VALID_URL = r'https?://(?:www\.)?linkedin\.com/learning/(?P<id>[^/?#]+)' | |
220 | _TEST = { | |
221 | 'url': 'https://www.linkedin.com/learning/programming-foundations-fundamentals', | |
222 | 'info_dict': { | |
223 | 'id': 'programming-foundations-fundamentals', | |
224 | 'title': 'Programming Foundations: Fundamentals', | |
225 | 'description': 'md5:76e580b017694eb89dc8e8923fff5c86', | |
226 | }, | |
227 | 'playlist_mincount': 61, | |
228 | } | |
229 | ||
230 | @classmethod | |
231 | def suitable(cls, url): | |
232 | return False if LinkedInLearningIE.suitable(url) else super(LinkedInLearningCourseIE, cls).suitable(url) | |
233 | ||
234 | def _real_extract(self, url): | |
235 | course_slug = self._match_id(url) | |
236 | course_data = self._call_api(course_slug, 'chapters,description,title') | |
237 | ||
238 | entries = [] | |
91effe22 | 239 | for chapter_number, chapter in enumerate(course_data.get('chapters', []), 1): |
aa7e974a | 240 | chapter_title = chapter.get('title') |
91effe22 | 241 | chapter_id = self._get_urn_id(chapter) |
aa7e974a RA |
242 | for video in chapter.get('videos', []): |
243 | video_slug = video.get('slug') | |
244 | if not video_slug: | |
245 | continue | |
246 | entries.append({ | |
b1447572 | 247 | '_type': 'url_transparent', |
91effe22 | 248 | 'id': self._get_video_id(video, course_slug, video_slug), |
aa7e974a RA |
249 | 'title': video.get('title'), |
250 | 'url': 'https://www.linkedin.com/learning/%s/%s' % (course_slug, video_slug), | |
251 | 'chapter': chapter_title, | |
91effe22 RA |
252 | 'chapter_number': chapter_number, |
253 | 'chapter_id': chapter_id, | |
aa7e974a RA |
254 | 'ie_key': LinkedInLearningIE.ie_key(), |
255 | }) | |
256 | ||
257 | return self.playlist_result( | |
258 | entries, course_slug, | |
259 | course_data.get('title'), | |
260 | course_data.get('description')) |