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