4 from .common
import InfoExtractor
11 srt_subtitles_timecode
,
20 class LinkedInBaseIE(InfoExtractor
):
21 _NETRC_MACHINE
= 'linkedin'
24 def _perform_login(self
, username
, password
):
28 login_page
= self
._download
_webpage
(
29 self
._LOGIN
_URL
, None, 'Downloading login page')
30 action_url
= urljoin(self
._LOGIN
_URL
, self
._search
_regex
(
31 r
'<form[^>]+action=(["\'])(?P
<url
>.+?
)\
1', login_page, 'post url
',
32 default='https
://www
.linkedin
.com
/uas
/login
-submit
', group='url
'))
33 data = self._hidden_inputs(login_page)
35 'session_key
': username,
36 'session_password
': password,
38 login_submit_page = self._download_webpage(
39 action_url, None, 'Logging
in',
40 data=urlencode_postdata(data))
41 error = self._search_regex(
42 r'<span
[^
>]+class="error"[^
>]*>\s
*(.+?
)\s
*</span
>',
43 login_submit_page, 'error
', default=None)
45 raise ExtractorError(error, expected=True)
46 LinkedInBaseIE._logged_in = True
49 class LinkedInLearningBaseIE(LinkedInBaseIE):
50 _LOGIN_URL = 'https
://www
.linkedin
.com
/uas
/login?trk
=learning
'
52 def _call_api(self, course_slug, fields, video_slug=None, resolution=None):
54 'courseSlug
': course_slug,
61 'videoSlug
': video_slug,
62 'resolution
': f'_{resolution}
',
64 sub = ' %dp
' % resolution
65 api_url = 'https
://www
.linkedin
.com
/learning
-api
/detailedCourses
'
66 if not self._get_cookies(api_url).get('JSESSIONID
'):
67 self.raise_login_required()
68 return self._download_json(
69 api_url, video_slug, f'Downloading{sub} JSON metadata
', headers={
70 'Csrf
-Token
': self._get_cookies(api_url)['JSESSIONID
'].value,
71 }, query=query)['elements
'][0]
73 def _get_urn_id(self, video_data):
74 urn = video_data.get('urn
')
76 mobj = re.search(r'urn
:li
:lyndaCourse
:\d
+,(\d
+)', urn)
80 def _get_video_id(self, video_data, course_slug, video_slug):
81 return self._get_urn_id(video_data) or f'{course_slug}
/{video_slug}
'
84 class LinkedInIE(LinkedInBaseIE):
85 _VALID_URL = r'https?
://(?
:www\
.)?linkedin\
.com
/posts
/[^
/?
#]+-(?P<id>\d+)-\w{4}/?(?:[?#]|$)'
87 'url': 'https://www.linkedin.com/posts/mishalkhawaja_sendinblueviews-toronto-digitalmarketing-ugcPost-6850898786781339649-mM20',
89 'id': '6850898786781339649',
91 'title': 'Mishal K. on LinkedIn: #sendinblueviews #toronto #digitalmarketing #nowhiring #sendinblue…',
92 'description': 'md5:2998a31f6f479376dd62831f53a80f71',
93 'uploader': 'Mishal K.',
94 'thumbnail': 're:^https?://media.licdn.com/dms/image/.*$',
98 'url': 'https://www.linkedin.com/posts/the-mathworks_2_what-is-mathworks-cloud-center-activity-7151241570371948544-4Gu7',
100 'id': '7151241570371948544',
102 'title': 'MathWorks on LinkedIn: What Is MathWorks Cloud Center?',
103 'description': 'md5:95f9d4eeb6337882fb47eefe13d7a40c',
104 'uploader': 'MathWorks',
105 'thumbnail': 're:^https?://media.licdn.com/dms/image/.*$',
107 'subtitles': 'mincount:1',
111 def _real_extract(self
, url
):
112 video_id
= self
._match
_id
(url
)
113 webpage
= self
._download
_webpage
(url
, video_id
)
115 video_attrs
= extract_attributes(self
._search
_regex
(r
'(<video[^>]+>)', webpage
, 'video'))
116 sources
= self
._parse
_json
(video_attrs
['data-sources'], video_id
)
118 'url': source
['src'],
119 'ext': mimetype2ext(source
.get('type')),
120 'tbr': float_or_none(source
.get('data-bitrate'), scale
=1000),
121 } for source
in sources
]
122 subtitles
= {'en': [{
123 'url': video_attrs
['data-captions-url'],
125 }]} if url_or_none(video_attrs
.get('data-captions-url')) else {}
130 'title': self
._og
_search
_title
(webpage
, default
=None) or self
._html
_extract
_title
(webpage
),
131 'like_count': int_or_none(self
._search
_regex
(
132 r
'\bdata-num-reactions="(\d+)"', webpage
, 'reactions', default
=None)),
133 'uploader': traverse_obj(
134 self
._yield
_json
_ld
(webpage
, video_id
),
135 (lambda _
, v
: v
['@type'] == 'SocialMediaPosting', 'author', 'name', {str}
), get_all
=False),
136 'thumbnail': self
._og
_search
_thumbnail
(webpage
),
137 'description': self
._og
_search
_description
(webpage
, default
=None),
138 'subtitles': subtitles
,
142 class LinkedInLearningIE(LinkedInLearningBaseIE
):
143 IE_NAME
= 'linkedin:learning'
144 _VALID_URL
= r
'https?://(?:www\.)?linkedin\.com/learning/(?P<course_slug>[^/]+)/(?P<id>[^/?#]+)'
146 'url': 'https://www.linkedin.com/learning/programming-foundations-fundamentals/welcome?autoplay=true',
147 'md5': 'a1d74422ff0d5e66a792deb996693167',
152 'timestamp': 1430396150.82,
153 'upload_date': '20150430',
157 def json2srt(self
, transcript_lines
, duration
=None):
159 for line
, (line_dict
, next_dict
) in enumerate(itertools
.zip_longest(transcript_lines
, transcript_lines
[1:])):
160 start_time
, caption
= line_dict
['transcriptStartAt'] / 1000, line_dict
['caption']
161 end_time
= next_dict
['transcriptStartAt'] / 1000 if next_dict
else duration
or start_time
+ 1
164 f
'{srt_subtitles_timecode(start_time)} --> {srt_subtitles_timecode(end_time)}\n'
168 def _real_extract(self
, url
):
169 course_slug
, video_slug
= self
._match
_valid
_url
(url
).groups()
172 for width
, height
in ((640, 360), (960, 540), (1280, 720)):
173 video_data
= self
._call
_api
(
174 course_slug
, 'selectedVideo', video_slug
, height
)['selectedVideo']
176 video_url_data
= video_data
.get('url') or {}
177 progressive_url
= video_url_data
.get('progressiveUrl')
180 'format_id': f
'progressive-{height}p',
181 'url': progressive_url
,
185 'source_preference': 1,
188 title
= video_data
['title']
190 audio_url
= video_data
.get('audio', {}).get('progressiveUrl')
195 'format_id': 'audio',
200 streaming_url
= video_url_data
.get('streamingUrl')
202 formats
.extend(self
._extract
_m
3u8_formats
(
203 streaming_url
, video_slug
, 'mp4',
204 'm3u8_native', m3u8_id
='hls', fatal
=False))
207 duration
= int_or_none(video_data
.get('durationInSeconds'))
208 transcript_lines
= try_get(video_data
, lambda x
: x
['transcript']['lines'], expected_type
=list)
212 'data': self
.json2srt(transcript_lines
, duration
),
216 'id': self
._get
_video
_id
(video_data
, course_slug
, video_slug
),
219 'thumbnail': video_data
.get('defaultThumbnail'),
220 'timestamp': float_or_none(video_data
.get('publishedOn'), 1000),
221 'duration': duration
,
222 'subtitles': subtitles
,
223 # It seems like this would be correctly handled by default
224 # However, unless someone can confirm this, the old
225 # behaviour is being kept as-is
226 '_format_sort_fields': ('res', 'source_preference'),
230 class LinkedInLearningCourseIE(LinkedInLearningBaseIE
):
231 IE_NAME
= 'linkedin:learning:course'
232 _VALID_URL
= r
'https?://(?:www\.)?linkedin\.com/learning/(?P<id>[^/?#]+)'
234 'url': 'https://www.linkedin.com/learning/programming-foundations-fundamentals',
236 'id': 'programming-foundations-fundamentals',
237 'title': 'Programming Foundations: Fundamentals',
238 'description': 'md5:76e580b017694eb89dc8e8923fff5c86',
240 'playlist_mincount': 61,
244 def suitable(cls
, url
):
245 return False if LinkedInLearningIE
.suitable(url
) else super().suitable(url
)
247 def _real_extract(self
, url
):
248 course_slug
= self
._match
_id
(url
)
249 course_data
= self
._call
_api
(course_slug
, 'chapters,description,title')
252 for chapter_number
, chapter
in enumerate(course_data
.get('chapters', []), 1):
253 chapter_title
= chapter
.get('title')
254 chapter_id
= self
._get
_urn
_id
(chapter
)
255 for video
in chapter
.get('videos', []):
256 video_slug
= video
.get('slug')
260 '_type': 'url_transparent',
261 'id': self
._get
_video
_id
(video
, course_slug
, video_slug
),
262 'title': video
.get('title'),
263 'url': f
'https://www.linkedin.com/learning/{course_slug}/{video_slug}',
264 'chapter': chapter_title
,
265 'chapter_number': chapter_number
,
266 'chapter_id': chapter_id
,
267 'ie_key': LinkedInLearningIE
.ie_key(),
270 return self
.playlist_result(
271 entries
, course_slug
,
272 course_data
.get('title'),
273 course_data
.get('description'))