]>
jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/linkedin.py
1 from itertools
import zip_longest
4 from .common
import InfoExtractor
10 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
': '_
%s' % 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, 'Downloading
%s JSON metadata
' % sub, 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 '%s/%s' % (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(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
162 srt_data
+= '%d\n%s --> %s\n%s\n\n' % (line
+ 1, srt_subtitles_timecode(start_time
),
163 srt_subtitles_timecode(end_time
),
167 def _real_extract(self
, url
):
168 course_slug
, video_slug
= self
._match
_valid
_url
(url
).groups()
171 for width
, height
in ((640, 360), (960, 540), (1280, 720)):
172 video_data
= self
._call
_api
(
173 course_slug
, 'selectedVideo', video_slug
, height
)['selectedVideo']
175 video_url_data
= video_data
.get('url') or {}
176 progressive_url
= video_url_data
.get('progressiveUrl')
179 'format_id': 'progressive-%dp' % height
,
180 'url': progressive_url
,
184 'source_preference': 1,
187 title
= video_data
['title']
189 audio_url
= video_data
.get('audio', {}).get('progressiveUrl')
194 'format_id': 'audio',
199 streaming_url
= video_url_data
.get('streamingUrl')
201 formats
.extend(self
._extract
_m
3u8_formats
(
202 streaming_url
, video_slug
, 'mp4',
203 'm3u8_native', m3u8_id
='hls', fatal
=False))
206 duration
= int_or_none(video_data
.get('durationInSeconds'))
207 transcript_lines
= try_get(video_data
, lambda x
: x
['transcript']['lines'], expected_type
=list)
211 'data': self
.json2srt(transcript_lines
, duration
)
215 'id': self
._get
_video
_id
(video_data
, course_slug
, video_slug
),
218 'thumbnail': video_data
.get('defaultThumbnail'),
219 'timestamp': float_or_none(video_data
.get('publishedOn'), 1000),
220 'duration': duration
,
221 'subtitles': subtitles
,
222 # It seems like this would be correctly handled by default
223 # However, unless someone can confirm this, the old
224 # behaviour is being kept as-is
225 '_format_sort_fields': ('res', 'source_preference')
229 class LinkedInLearningCourseIE(LinkedInLearningBaseIE
):
230 IE_NAME
= 'linkedin:learning:course'
231 _VALID_URL
= r
'https?://(?:www\.)?linkedin\.com/learning/(?P<id>[^/?#]+)'
233 'url': 'https://www.linkedin.com/learning/programming-foundations-fundamentals',
235 'id': 'programming-foundations-fundamentals',
236 'title': 'Programming Foundations: Fundamentals',
237 'description': 'md5:76e580b017694eb89dc8e8923fff5c86',
239 'playlist_mincount': 61,
243 def suitable(cls
, url
):
244 return False if LinkedInLearningIE
.suitable(url
) else super(LinkedInLearningCourseIE
, cls
).suitable(url
)
246 def _real_extract(self
, url
):
247 course_slug
= self
._match
_id
(url
)
248 course_data
= self
._call
_api
(course_slug
, 'chapters,description,title')
251 for chapter_number
, chapter
in enumerate(course_data
.get('chapters', []), 1):
252 chapter_title
= chapter
.get('title')
253 chapter_id
= self
._get
_urn
_id
(chapter
)
254 for video
in chapter
.get('videos', []):
255 video_slug
= video
.get('slug')
259 '_type': 'url_transparent',
260 'id': self
._get
_video
_id
(video
, course_slug
, video_slug
),
261 'title': video
.get('title'),
262 'url': 'https://www.linkedin.com/learning/%s/%s' % (course_slug
, video_slug
),
263 'chapter': chapter_title
,
264 'chapter_number': chapter_number
,
265 'chapter_id': chapter_id
,
266 'ie_key': LinkedInLearningIE
.ie_key(),
269 return self
.playlist_result(
270 entries
, course_slug
,
271 course_data
.get('title'),
272 course_data
.get('description'))