2 from __future__ import unicode_literals
6 from .common import InfoExtractor
15 class LinkedInLearningBaseIE(InfoExtractor):
16 _NETRC_MACHINE = 'linkedin'
18 def _call_api(self, course_slug, fields, video_slug=None, resolution=None):
20 'courseSlug': course_slug,
27 'videoSlug': video_slug,
28 'resolution': '_%s' % resolution,
30 sub = ' %dp' % resolution
31 api_url = 'https://www.linkedin.com/learning-api/detailedCourses'
32 return self._download_json(
33 api_url, video_slug, 'Downloading%s JSON metadata' % sub, headers={
34 'Csrf-Token': self._get_cookies(api_url)['JSESSIONID'].value,
35 }, query=query)['elements'][0]
37 def _get_video_id(self, urn, course_slug, video_slug):
39 mobj = re.search(r'urn:li:lyndaCourse:\d+,(\d+)', urn)
42 return '%s/%s' % (course_slug, video_slug)
44 def _real_initialize(self):
45 email, password = self._get_login_info()
49 login_page = self._download_webpage(
50 'https://www.linkedin.com/uas/login?trk=learning',
51 None, 'Downloading login page')
52 action_url = self._search_regex(
53 r'<form[^>]+action=(["\'])(?P<url>.+?)\1', login_page, 'post url',
54 default='https://www.linkedin.com/uas/login-submit', group='url')
55 data = self._hidden_inputs(login_page)
58 'session_password': password,
60 login_submit_page = self._download_webpage(
61 action_url, None, 'Logging in',
62 data=urlencode_postdata(data))
63 error = self._search_regex(
64 r'<span[^>]+class="error"[^>]*>\s*(.+?)\s*</span>',
65 login_submit_page, 'error', default=None)
67 raise ExtractorError(error, expected=True)
70 class LinkedInLearningIE(LinkedInLearningBaseIE):
71 IE_NAME = 'linkedin:learning'
72 _VALID_URL = r'https?://(?:www\.)?linkedin\.com/learning/(?P<course_slug>[^/]+)/(?P<id>[^/?#]+)'
74 'url': 'https://www.linkedin.com/learning/programming-foundations-fundamentals/welcome?autoplay=true',
75 'md5': 'a1d74422ff0d5e66a792deb996693167',
80 'timestamp': 1430396150.82,
81 'upload_date': '20150430',
85 def _real_extract(self, url):
86 course_slug, video_slug = re.match(self._VALID_URL, url).groups()
90 for width, height in ((640, 360), (960, 540), (1280, 720)):
91 video_data = self._call_api(
92 course_slug, 'selectedVideo', video_slug, height)['selectedVideo']
94 video_url_data = video_data.get('url') or {}
95 progressive_url = video_url_data.get('progressiveUrl')
98 'format_id': 'progressive-%dp' % height,
99 'url': progressive_url,
102 'source_preference': 1,
105 title = video_data['title']
107 audio_url = video_data.get('audio', {}).get('progressiveUrl')
112 'format_id': 'audio',
117 streaming_url = video_url_data.get('streamingUrl')
119 formats.extend(self._extract_m3u8_formats(
120 streaming_url, video_slug, 'mp4',
121 'm3u8_native', m3u8_id='hls', fatal=False))
123 self._sort_formats(formats, ('width', 'height', 'source_preference', 'tbr', 'abr'))
126 'id': self._get_video_id(video_data.get('urn'), course_slug, video_slug),
129 'thumbnail': video_data.get('defaultThumbnail'),
130 'timestamp': float_or_none(video_data.get('publishedOn'), 1000),
131 'duration': int_or_none(video_data.get('durationInSeconds')),
135 class LinkedInLearningCourseIE(LinkedInLearningBaseIE):
136 IE_NAME = 'linkedin:learning:course'
137 _VALID_URL = r'https?://(?:www\.)?linkedin\.com/learning/(?P<id>[^/?#]+)'
139 'url': 'https://www.linkedin.com/learning/programming-foundations-fundamentals',
141 'id': 'programming-foundations-fundamentals',
142 'title': 'Programming Foundations: Fundamentals',
143 'description': 'md5:76e580b017694eb89dc8e8923fff5c86',
145 'playlist_mincount': 61,
149 def suitable(cls, url):
150 return False if LinkedInLearningIE.suitable(url) else super(LinkedInLearningCourseIE, cls).suitable(url)
152 def _real_extract(self, url):
153 course_slug = self._match_id(url)
154 course_data = self._call_api(course_slug, 'chapters,description,title')
157 for chapter in course_data.get('chapters', []):
158 chapter_title = chapter.get('title')
159 for video in chapter.get('videos', []):
160 video_slug = video.get('slug')
164 '_type': 'url_transparent',
165 'id': self._get_video_id(video.get('urn'), course_slug, video_slug),
166 'title': video.get('title'),
167 'url': 'https://www.linkedin.com/learning/%s/%s' % (course_slug, video_slug),
168 'chapter': chapter_title,
169 'ie_key': LinkedInLearningIE.ie_key(),
172 return self.playlist_result(
173 entries, course_slug,
174 course_data.get('title'),
175 course_data.get('description'))