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