2024-03-31 18:01:09 -06:00
|
|
|
import itertools
|
2018-10-29 12:28:09 -06:00
|
|
|
import re
|
|
|
|
|
|
|
|
from .common import InfoExtractor
|
|
|
|
from ..utils import (
|
|
|
|
ExtractorError,
|
2024-02-15 17:19:00 -07:00
|
|
|
extract_attributes,
|
2018-10-29 12:28:09 -06:00
|
|
|
float_or_none,
|
|
|
|
int_or_none,
|
2021-11-18 14:57:40 -07:00
|
|
|
mimetype2ext,
|
2024-05-26 13:27:21 -06:00
|
|
|
srt_subtitles_timecode,
|
2024-02-15 17:19:00 -07:00
|
|
|
traverse_obj,
|
2021-09-25 05:25:33 -06:00
|
|
|
try_get,
|
2024-02-15 17:19:00 -07:00
|
|
|
url_or_none,
|
2018-10-29 12:28:09 -06:00
|
|
|
urlencode_postdata,
|
2019-04-11 01:44:58 -06:00
|
|
|
urljoin,
|
2018-10-29 12:28:09 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-11-18 14:57:40 -07:00
|
|
|
class LinkedInBaseIE(InfoExtractor):
|
2018-10-29 12:28:09 -06:00
|
|
|
_NETRC_MACHINE = 'linkedin'
|
2021-10-31 01:38:03 -06:00
|
|
|
_logged_in = False
|
2018-10-29 12:28:09 -06:00
|
|
|
|
2022-03-18 14:53:33 -06:00
|
|
|
def _perform_login(self, username, password):
|
2021-11-18 14:57:40 -07:00
|
|
|
if self._logged_in:
|
|
|
|
return
|
|
|
|
|
|
|
|
login_page = self._download_webpage(
|
|
|
|
self._LOGIN_URL, None, 'Downloading login page')
|
|
|
|
action_url = urljoin(self._LOGIN_URL, self._search_regex(
|
|
|
|
r'<form[^>]+action=(["\'])(?P<url>.+?)\1', login_page, 'post url',
|
|
|
|
default='https://www.linkedin.com/uas/login-submit', group='url'))
|
|
|
|
data = self._hidden_inputs(login_page)
|
|
|
|
data.update({
|
2022-03-18 14:53:33 -06:00
|
|
|
'session_key': username,
|
2021-11-18 14:57:40 -07:00
|
|
|
'session_password': password,
|
|
|
|
})
|
|
|
|
login_submit_page = self._download_webpage(
|
|
|
|
action_url, None, 'Logging in',
|
|
|
|
data=urlencode_postdata(data))
|
|
|
|
error = self._search_regex(
|
|
|
|
r'<span[^>]+class="error"[^>]*>\s*(.+?)\s*</span>',
|
|
|
|
login_submit_page, 'error', default=None)
|
|
|
|
if error:
|
|
|
|
raise ExtractorError(error, expected=True)
|
|
|
|
LinkedInBaseIE._logged_in = True
|
|
|
|
|
|
|
|
|
|
|
|
class LinkedInLearningBaseIE(LinkedInBaseIE):
|
|
|
|
_LOGIN_URL = 'https://www.linkedin.com/uas/login?trk=learning'
|
|
|
|
|
2018-10-29 12:28:09 -06:00
|
|
|
def _call_api(self, course_slug, fields, video_slug=None, resolution=None):
|
|
|
|
query = {
|
|
|
|
'courseSlug': course_slug,
|
|
|
|
'fields': fields,
|
|
|
|
'q': 'slugs',
|
|
|
|
}
|
|
|
|
sub = ''
|
|
|
|
if video_slug:
|
|
|
|
query.update({
|
|
|
|
'videoSlug': video_slug,
|
2024-06-11 17:09:58 -06:00
|
|
|
'resolution': f'_{resolution}',
|
2018-10-29 12:28:09 -06:00
|
|
|
})
|
|
|
|
sub = ' %dp' % resolution
|
|
|
|
api_url = 'https://www.linkedin.com/learning-api/detailedCourses'
|
2021-10-31 01:38:03 -06:00
|
|
|
if not self._get_cookies(api_url).get('JSESSIONID'):
|
|
|
|
self.raise_login_required()
|
2018-10-29 12:28:09 -06:00
|
|
|
return self._download_json(
|
2024-06-11 17:09:58 -06:00
|
|
|
api_url, video_slug, f'Downloading{sub} JSON metadata', headers={
|
2018-10-29 12:28:09 -06:00
|
|
|
'Csrf-Token': self._get_cookies(api_url)['JSESSIONID'].value,
|
|
|
|
}, query=query)['elements'][0]
|
|
|
|
|
2019-02-07 23:21:31 -07:00
|
|
|
def _get_urn_id(self, video_data):
|
|
|
|
urn = video_data.get('urn')
|
2018-10-29 12:28:09 -06:00
|
|
|
if urn:
|
|
|
|
mobj = re.search(r'urn:li:lyndaCourse:\d+,(\d+)', urn)
|
|
|
|
if mobj:
|
|
|
|
return mobj.group(1)
|
2019-02-07 23:21:31 -07:00
|
|
|
|
|
|
|
def _get_video_id(self, video_data, course_slug, video_slug):
|
2024-06-11 17:09:58 -06:00
|
|
|
return self._get_urn_id(video_data) or f'{course_slug}/{video_slug}'
|
2018-10-29 12:28:09 -06:00
|
|
|
|
|
|
|
|
2021-11-18 14:57:40 -07:00
|
|
|
class LinkedInIE(LinkedInBaseIE):
|
2024-02-15 17:19:00 -07:00
|
|
|
_VALID_URL = r'https?://(?:www\.)?linkedin\.com/posts/[^/?#]+-(?P<id>\d+)-\w{4}/?(?:[?#]|$)'
|
2021-11-18 14:57:40 -07:00
|
|
|
_TESTS = [{
|
|
|
|
'url': 'https://www.linkedin.com/posts/mishalkhawaja_sendinblueviews-toronto-digitalmarketing-ugcPost-6850898786781339649-mM20',
|
|
|
|
'info_dict': {
|
|
|
|
'id': '6850898786781339649',
|
|
|
|
'ext': 'mp4',
|
2024-02-15 17:19:00 -07:00
|
|
|
'title': 'Mishal K. on LinkedIn: #sendinblueviews #toronto #digitalmarketing #nowhiring #sendinblue…',
|
|
|
|
'description': 'md5:2998a31f6f479376dd62831f53a80f71',
|
|
|
|
'uploader': 'Mishal K.',
|
|
|
|
'thumbnail': 're:^https?://media.licdn.com/dms/image/.*$',
|
2024-06-11 17:09:58 -06:00
|
|
|
'like_count': int,
|
2024-02-15 17:19:00 -07:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
'url': 'https://www.linkedin.com/posts/the-mathworks_2_what-is-mathworks-cloud-center-activity-7151241570371948544-4Gu7',
|
|
|
|
'info_dict': {
|
|
|
|
'id': '7151241570371948544',
|
|
|
|
'ext': 'mp4',
|
|
|
|
'title': 'MathWorks on LinkedIn: What Is MathWorks Cloud Center?',
|
|
|
|
'description': 'md5:95f9d4eeb6337882fb47eefe13d7a40c',
|
|
|
|
'uploader': 'MathWorks',
|
|
|
|
'thumbnail': 're:^https?://media.licdn.com/dms/image/.*$',
|
|
|
|
'like_count': int,
|
2024-06-11 17:09:58 -06:00
|
|
|
'subtitles': 'mincount:1',
|
2021-11-18 14:57:40 -07:00
|
|
|
},
|
|
|
|
}]
|
|
|
|
|
|
|
|
def _real_extract(self, url):
|
|
|
|
video_id = self._match_id(url)
|
|
|
|
webpage = self._download_webpage(url, video_id)
|
|
|
|
|
2024-02-15 17:19:00 -07:00
|
|
|
video_attrs = extract_attributes(self._search_regex(r'(<video[^>]+>)', webpage, 'video'))
|
|
|
|
sources = self._parse_json(video_attrs['data-sources'], video_id)
|
2021-11-18 14:57:40 -07:00
|
|
|
formats = [{
|
|
|
|
'url': source['src'],
|
|
|
|
'ext': mimetype2ext(source.get('type')),
|
|
|
|
'tbr': float_or_none(source.get('data-bitrate'), scale=1000),
|
|
|
|
} for source in sources]
|
2024-02-15 17:19:00 -07:00
|
|
|
subtitles = {'en': [{
|
|
|
|
'url': video_attrs['data-captions-url'],
|
|
|
|
'ext': 'vtt',
|
|
|
|
}]} if url_or_none(video_attrs.get('data-captions-url')) else {}
|
2021-11-18 14:57:40 -07:00
|
|
|
|
|
|
|
return {
|
|
|
|
'id': video_id,
|
|
|
|
'formats': formats,
|
2024-02-15 17:19:00 -07:00
|
|
|
'title': self._og_search_title(webpage, default=None) or self._html_extract_title(webpage),
|
|
|
|
'like_count': int_or_none(self._search_regex(
|
|
|
|
r'\bdata-num-reactions="(\d+)"', webpage, 'reactions', default=None)),
|
|
|
|
'uploader': traverse_obj(
|
|
|
|
self._yield_json_ld(webpage, video_id),
|
|
|
|
(lambda _, v: v['@type'] == 'SocialMediaPosting', 'author', 'name', {str}), get_all=False),
|
2021-11-18 14:57:40 -07:00
|
|
|
'thumbnail': self._og_search_thumbnail(webpage),
|
2024-02-15 17:19:00 -07:00
|
|
|
'description': self._og_search_description(webpage, default=None),
|
|
|
|
'subtitles': subtitles,
|
2021-11-18 14:57:40 -07:00
|
|
|
}
|
2018-10-29 12:28:09 -06:00
|
|
|
|
|
|
|
|
|
|
|
class LinkedInLearningIE(LinkedInLearningBaseIE):
|
|
|
|
IE_NAME = 'linkedin:learning'
|
|
|
|
_VALID_URL = r'https?://(?:www\.)?linkedin\.com/learning/(?P<course_slug>[^/]+)/(?P<id>[^/?#]+)'
|
|
|
|
_TEST = {
|
|
|
|
'url': 'https://www.linkedin.com/learning/programming-foundations-fundamentals/welcome?autoplay=true',
|
|
|
|
'md5': 'a1d74422ff0d5e66a792deb996693167',
|
|
|
|
'info_dict': {
|
|
|
|
'id': '90426',
|
|
|
|
'ext': 'mp4',
|
|
|
|
'title': 'Welcome',
|
|
|
|
'timestamp': 1430396150.82,
|
|
|
|
'upload_date': '20150430',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-09-25 05:25:33 -06:00
|
|
|
def json2srt(self, transcript_lines, duration=None):
|
|
|
|
srt_data = ''
|
2024-03-31 18:01:09 -06:00
|
|
|
for line, (line_dict, next_dict) in enumerate(itertools.zip_longest(transcript_lines, transcript_lines[1:])):
|
2021-09-25 05:25:33 -06:00
|
|
|
start_time, caption = line_dict['transcriptStartAt'] / 1000, line_dict['caption']
|
|
|
|
end_time = next_dict['transcriptStartAt'] / 1000 if next_dict else duration or start_time + 1
|
2024-06-11 17:09:58 -06:00
|
|
|
srt_data += (
|
|
|
|
f'{line + 1}\n'
|
|
|
|
f'{srt_subtitles_timecode(start_time)} --> {srt_subtitles_timecode(end_time)}\n'
|
|
|
|
f'{caption}\n\n')
|
2021-09-25 05:25:33 -06:00
|
|
|
return srt_data
|
|
|
|
|
2018-10-29 12:28:09 -06:00
|
|
|
def _real_extract(self, url):
|
2021-08-18 19:41:24 -06:00
|
|
|
course_slug, video_slug = self._match_valid_url(url).groups()
|
2018-10-29 12:28:09 -06:00
|
|
|
|
|
|
|
formats = []
|
|
|
|
for width, height in ((640, 360), (960, 540), (1280, 720)):
|
|
|
|
video_data = self._call_api(
|
|
|
|
course_slug, 'selectedVideo', video_slug, height)['selectedVideo']
|
|
|
|
|
|
|
|
video_url_data = video_data.get('url') or {}
|
|
|
|
progressive_url = video_url_data.get('progressiveUrl')
|
|
|
|
if progressive_url:
|
|
|
|
formats.append({
|
2024-06-11 17:09:58 -06:00
|
|
|
'format_id': f'progressive-{height}p',
|
2018-10-29 12:28:09 -06:00
|
|
|
'url': progressive_url,
|
2021-09-25 05:25:33 -06:00
|
|
|
'ext': 'mp4',
|
2018-10-29 12:28:09 -06:00
|
|
|
'height': height,
|
|
|
|
'width': width,
|
|
|
|
'source_preference': 1,
|
|
|
|
})
|
|
|
|
|
|
|
|
title = video_data['title']
|
|
|
|
|
|
|
|
audio_url = video_data.get('audio', {}).get('progressiveUrl')
|
|
|
|
if audio_url:
|
|
|
|
formats.append({
|
|
|
|
'abr': 64,
|
|
|
|
'ext': 'm4a',
|
|
|
|
'format_id': 'audio',
|
|
|
|
'url': audio_url,
|
|
|
|
'vcodec': 'none',
|
|
|
|
})
|
|
|
|
|
|
|
|
streaming_url = video_url_data.get('streamingUrl')
|
|
|
|
if streaming_url:
|
|
|
|
formats.extend(self._extract_m3u8_formats(
|
|
|
|
streaming_url, video_slug, 'mp4',
|
|
|
|
'm3u8_native', m3u8_id='hls', fatal=False))
|
|
|
|
|
2021-09-25 05:25:33 -06:00
|
|
|
subtitles = {}
|
|
|
|
duration = int_or_none(video_data.get('durationInSeconds'))
|
|
|
|
transcript_lines = try_get(video_data, lambda x: x['transcript']['lines'], expected_type=list)
|
|
|
|
if transcript_lines:
|
|
|
|
subtitles['en'] = [{
|
|
|
|
'ext': 'srt',
|
2024-06-11 17:09:58 -06:00
|
|
|
'data': self.json2srt(transcript_lines, duration),
|
2021-09-25 05:25:33 -06:00
|
|
|
}]
|
2018-10-29 12:28:09 -06:00
|
|
|
|
|
|
|
return {
|
2019-02-07 23:21:31 -07:00
|
|
|
'id': self._get_video_id(video_data, course_slug, video_slug),
|
2018-10-29 12:28:09 -06:00
|
|
|
'title': title,
|
|
|
|
'formats': formats,
|
|
|
|
'thumbnail': video_data.get('defaultThumbnail'),
|
|
|
|
'timestamp': float_or_none(video_data.get('publishedOn'), 1000),
|
2021-09-25 05:25:33 -06:00
|
|
|
'duration': duration,
|
|
|
|
'subtitles': subtitles,
|
2022-11-16 22:10:03 -07:00
|
|
|
# It seems like this would be correctly handled by default
|
|
|
|
# However, unless someone can confirm this, the old
|
|
|
|
# behaviour is being kept as-is
|
2024-06-11 17:09:58 -06:00
|
|
|
'_format_sort_fields': ('res', 'source_preference'),
|
2018-10-29 12:28:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class LinkedInLearningCourseIE(LinkedInLearningBaseIE):
|
|
|
|
IE_NAME = 'linkedin:learning:course'
|
|
|
|
_VALID_URL = r'https?://(?:www\.)?linkedin\.com/learning/(?P<id>[^/?#]+)'
|
|
|
|
_TEST = {
|
|
|
|
'url': 'https://www.linkedin.com/learning/programming-foundations-fundamentals',
|
|
|
|
'info_dict': {
|
|
|
|
'id': 'programming-foundations-fundamentals',
|
|
|
|
'title': 'Programming Foundations: Fundamentals',
|
|
|
|
'description': 'md5:76e580b017694eb89dc8e8923fff5c86',
|
|
|
|
},
|
|
|
|
'playlist_mincount': 61,
|
|
|
|
}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def suitable(cls, url):
|
2024-06-11 17:09:58 -06:00
|
|
|
return False if LinkedInLearningIE.suitable(url) else super().suitable(url)
|
2018-10-29 12:28:09 -06:00
|
|
|
|
|
|
|
def _real_extract(self, url):
|
|
|
|
course_slug = self._match_id(url)
|
|
|
|
course_data = self._call_api(course_slug, 'chapters,description,title')
|
|
|
|
|
|
|
|
entries = []
|
2019-02-07 23:21:31 -07:00
|
|
|
for chapter_number, chapter in enumerate(course_data.get('chapters', []), 1):
|
2018-10-29 12:28:09 -06:00
|
|
|
chapter_title = chapter.get('title')
|
2019-02-07 23:21:31 -07:00
|
|
|
chapter_id = self._get_urn_id(chapter)
|
2018-10-29 12:28:09 -06:00
|
|
|
for video in chapter.get('videos', []):
|
|
|
|
video_slug = video.get('slug')
|
|
|
|
if not video_slug:
|
|
|
|
continue
|
|
|
|
entries.append({
|
2018-10-29 14:49:12 -06:00
|
|
|
'_type': 'url_transparent',
|
2019-02-07 23:21:31 -07:00
|
|
|
'id': self._get_video_id(video, course_slug, video_slug),
|
2018-10-29 12:28:09 -06:00
|
|
|
'title': video.get('title'),
|
2024-06-11 17:09:58 -06:00
|
|
|
'url': f'https://www.linkedin.com/learning/{course_slug}/{video_slug}',
|
2018-10-29 12:28:09 -06:00
|
|
|
'chapter': chapter_title,
|
2019-02-07 23:21:31 -07:00
|
|
|
'chapter_number': chapter_number,
|
|
|
|
'chapter_id': chapter_id,
|
2018-10-29 12:28:09 -06:00
|
|
|
'ie_key': LinkedInLearningIE.ie_key(),
|
|
|
|
})
|
|
|
|
|
|
|
|
return self.playlist_result(
|
|
|
|
entries, course_slug,
|
|
|
|
course_data.get('title'),
|
|
|
|
course_data.get('description'))
|