]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/linkedin.py
259fc4c5e8d6d7fe214faf408c97110ce55ac5bc
[youtubedl] / youtube_dl / extractor / linkedin.py
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,
12 )
13
14
15 class LinkedInLearningBaseIE(InfoExtractor):
16 _NETRC_MACHINE = 'linkedin'
17
18 def _call_api(self, course_slug, fields, video_slug=None, resolution=None):
19 query = {
20 'courseSlug': course_slug,
21 'fields': fields,
22 'q': 'slugs',
23 }
24 sub = ''
25 if video_slug:
26 query.update({
27 'videoSlug': video_slug,
28 'resolution': '_%s' % resolution,
29 })
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]
36
37 def _get_video_id(self, urn, course_slug, video_slug):
38 if urn:
39 mobj = re.search(r'urn:li:lyndaCourse:\d+,(\d+)', urn)
40 if mobj:
41 return mobj.group(1)
42 return '%s/%s' % (course_slug, video_slug)
43
44 def _real_initialize(self):
45 email, password = self._get_login_info()
46 if email is None:
47 return
48
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)
56 data.update({
57 'session_key': email,
58 'session_password': password,
59 })
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)
66 if error:
67 raise ExtractorError(error, expected=True)
68
69
70 class LinkedInLearningIE(LinkedInLearningBaseIE):
71 IE_NAME = 'linkedin:learning'
72 _VALID_URL = r'https?://(?:www\.)?linkedin\.com/learning/(?P<course_slug>[^/]+)/(?P<id>[^/?#]+)'
73 _TEST = {
74 'url': 'https://www.linkedin.com/learning/programming-foundations-fundamentals/welcome?autoplay=true',
75 'md5': 'a1d74422ff0d5e66a792deb996693167',
76 'info_dict': {
77 'id': '90426',
78 'ext': 'mp4',
79 'title': 'Welcome',
80 'timestamp': 1430396150.82,
81 'upload_date': '20150430',
82 },
83 }
84
85 def _real_extract(self, url):
86 course_slug, video_slug = re.match(self._VALID_URL, url).groups()
87
88 video_data = None
89 formats = []
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']
93
94 video_url_data = video_data.get('url') or {}
95 progressive_url = video_url_data.get('progressiveUrl')
96 if progressive_url:
97 formats.append({
98 'format_id': 'progressive-%dp' % height,
99 'url': progressive_url,
100 'height': height,
101 'width': width,
102 'source_preference': 1,
103 })
104
105 title = video_data['title']
106
107 audio_url = video_data.get('audio', {}).get('progressiveUrl')
108 if audio_url:
109 formats.append({
110 'abr': 64,
111 'ext': 'm4a',
112 'format_id': 'audio',
113 'url': audio_url,
114 'vcodec': 'none',
115 })
116
117 streaming_url = video_url_data.get('streamingUrl')
118 if streaming_url:
119 formats.extend(self._extract_m3u8_formats(
120 streaming_url, video_slug, 'mp4',
121 'm3u8_native', m3u8_id='hls', fatal=False))
122
123 self._sort_formats(formats, ('width', 'height', 'source_preference', 'tbr', 'abr'))
124
125 return {
126 'id': self._get_video_id(video_data.get('urn'), course_slug, video_slug),
127 'title': title,
128 'formats': formats,
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')),
132 }
133
134
135 class LinkedInLearningCourseIE(LinkedInLearningBaseIE):
136 IE_NAME = 'linkedin:learning:course'
137 _VALID_URL = r'https?://(?:www\.)?linkedin\.com/learning/(?P<id>[^/?#]+)'
138 _TEST = {
139 'url': 'https://www.linkedin.com/learning/programming-foundations-fundamentals',
140 'info_dict': {
141 'id': 'programming-foundations-fundamentals',
142 'title': 'Programming Foundations: Fundamentals',
143 'description': 'md5:76e580b017694eb89dc8e8923fff5c86',
144 },
145 'playlist_mincount': 61,
146 }
147
148 @classmethod
149 def suitable(cls, url):
150 return False if LinkedInLearningIE.suitable(url) else super(LinkedInLearningCourseIE, cls).suitable(url)
151
152 def _real_extract(self, url):
153 course_slug = self._match_id(url)
154 course_data = self._call_api(course_slug, 'chapters,description,title')
155
156 entries = []
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')
161 if not video_slug:
162 continue
163 entries.append({
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(),
170 })
171
172 return self.playlist_result(
173 entries, course_slug,
174 course_data.get('title'),
175 course_data.get('description'))