2 from __future__
import unicode_literals
4 from itertools
import zip_longest
7 from .common
import InfoExtractor
15 srt_subtitles_timecode
,
24 class LinkedInBaseIE(InfoExtractor
):
25 _NETRC_MACHINE
= 'linkedin'
28 def _real_initialize(self
):
31 email
, password
= self
._get
_login
_info
()
35 login_page
= self
._download
_webpage
(
36 self
._LOGIN
_URL
, None, 'Downloading login page')
37 action_url
= urljoin(self
._LOGIN
_URL
, self
._search
_regex
(
38 r
'<form[^>]+action=(["\'])(?P
<url
>.+?
)\
1', login_page, 'post url
',
39 default='https
://www
.linkedin
.com
/uas
/login
-submit
', group='url
'))
40 data = self._hidden_inputs(login_page)
43 'session_password
': password,
45 login_submit_page = self._download_webpage(
46 action_url, None, 'Logging
in',
47 data=urlencode_postdata(data))
48 error = self._search_regex(
49 r'<span
[^
>]+class="error"[^
>]*>\s
*(.+?
)\s
*</span
>',
50 login_submit_page, 'error
', default=None)
52 raise ExtractorError(error, expected=True)
53 LinkedInBaseIE._logged_in = True
56 class LinkedInLearningBaseIE(LinkedInBaseIE):
57 _LOGIN_URL = 'https
://www
.linkedin
.com
/uas
/login?trk
=learning
'
59 def _call_api(self, course_slug, fields, video_slug=None, resolution=None):
61 'courseSlug
': course_slug,
68 'videoSlug
': video_slug,
69 'resolution
': '_
%s' % resolution,
71 sub = ' %dp
' % resolution
72 api_url = 'https
://www
.linkedin
.com
/learning
-api
/detailedCourses
'
73 if not self._get_cookies(api_url).get('JSESSIONID
'):
74 self.raise_login_required()
75 return self._download_json(
76 api_url, video_slug, 'Downloading
%s JSON metadata
' % sub, headers={
77 'Csrf
-Token
': self._get_cookies(api_url)['JSESSIONID
'].value,
78 }, query=query)['elements
'][0]
80 def _get_urn_id(self, video_data):
81 urn = video_data.get('urn
')
83 mobj = re.search(r'urn
:li
:lyndaCourse
:\d
+,(\d
+)', urn)
87 def _get_video_id(self, video_data, course_slug, video_slug):
88 return self._get_urn_id(video_data) or '%s/%s' % (course_slug, video_slug)
91 class LinkedInIE(LinkedInBaseIE):
92 _VALID_URL = r'https?
://(?
:www\
.)?linkedin\
.com
/posts
/.+?
(?P
<id>\d
+)'
94 'url
': 'https
://www
.linkedin
.com
/posts
/mishalkhawaja_sendinblueviews
-toronto
-digitalmarketing
-ugcPost
-6850898786781339649-mM20
',
96 'id': '6850898786781339649',
98 'title
': 'Mishal K
. on LinkedIn
: #sendinblueviews #toronto #digitalmarketing',
99 'description': 'md5:be125430bab1c574f16aeb186a4d5b19',
100 'creator': 'Mishal K.'
104 def _real_extract(self
, url
):
105 video_id
= self
._match
_id
(url
)
106 webpage
= self
._download
_webpage
(url
, video_id
)
108 title
= self
._html
_search
_regex
(r
'<title>([^<]+)</title>', webpage
, 'title')
109 description
= clean_html(get_element_by_class('share-update-card__update-text', webpage
))
110 like_count
= int_or_none(get_element_by_class('social-counts-reactions__social-counts-numRections', webpage
))
111 creator
= strip_or_none(clean_html(get_element_by_class('comment__actor-name', webpage
)))
113 sources
= self
._parse
_json
(extract_attributes(self
._search
_regex
(r
'(<video[^>]+>)', webpage
, 'video'))['data-sources'], video_id
)
115 'url': source
['src'],
116 'ext': mimetype2ext(source
.get('type')),
117 'tbr': float_or_none(source
.get('data-bitrate'), scale
=1000),
118 } for source
in sources
]
120 self
._sort
_formats
(formats
)
126 'like_count': like_count
,
128 'thumbnail': self
._og
_search
_thumbnail
(webpage
),
129 'description': description
,
133 class LinkedInLearningIE(LinkedInLearningBaseIE
):
134 IE_NAME
= 'linkedin:learning'
135 _VALID_URL
= r
'https?://(?:www\.)?linkedin\.com/learning/(?P<course_slug>[^/]+)/(?P<id>[^/?#]+)'
137 'url': 'https://www.linkedin.com/learning/programming-foundations-fundamentals/welcome?autoplay=true',
138 'md5': 'a1d74422ff0d5e66a792deb996693167',
143 'timestamp': 1430396150.82,
144 'upload_date': '20150430',
148 def json2srt(self
, transcript_lines
, duration
=None):
150 for line
, (line_dict
, next_dict
) in enumerate(zip_longest(transcript_lines
, transcript_lines
[1:])):
151 start_time
, caption
= line_dict
['transcriptStartAt'] / 1000, line_dict
['caption']
152 end_time
= next_dict
['transcriptStartAt'] / 1000 if next_dict
else duration
or start_time
+ 1
153 srt_data
+= '%d\n%s --> %s\n%s\n\n' % (line
+ 1, srt_subtitles_timecode(start_time
),
154 srt_subtitles_timecode(end_time
),
158 def _real_extract(self
, url
):
159 course_slug
, video_slug
= self
._match
_valid
_url
(url
).groups()
162 for width
, height
in ((640, 360), (960, 540), (1280, 720)):
163 video_data
= self
._call
_api
(
164 course_slug
, 'selectedVideo', video_slug
, height
)['selectedVideo']
166 video_url_data
= video_data
.get('url') or {}
167 progressive_url
= video_url_data
.get('progressiveUrl')
170 'format_id': 'progressive-%dp' % height
,
171 'url': progressive_url
,
175 'source_preference': 1,
178 title
= video_data
['title']
180 audio_url
= video_data
.get('audio', {}).get('progressiveUrl')
185 'format_id': 'audio',
190 streaming_url
= video_url_data
.get('streamingUrl')
192 formats
.extend(self
._extract
_m
3u8_formats
(
193 streaming_url
, video_slug
, 'mp4',
194 'm3u8_native', m3u8_id
='hls', fatal
=False))
196 # It seems like this would be correctly handled by default
197 # However, unless someone can confirm this, the old
198 # behaviour is being kept as-is
199 self
._sort
_formats
(formats
, ('res', 'source_preference'))
201 duration
= int_or_none(video_data
.get('durationInSeconds'))
202 transcript_lines
= try_get(video_data
, lambda x
: x
['transcript']['lines'], expected_type
=list)
206 'data': self
.json2srt(transcript_lines
, duration
)
210 'id': self
._get
_video
_id
(video_data
, course_slug
, video_slug
),
213 'thumbnail': video_data
.get('defaultThumbnail'),
214 'timestamp': float_or_none(video_data
.get('publishedOn'), 1000),
215 'duration': duration
,
216 'subtitles': subtitles
,
220 class LinkedInLearningCourseIE(LinkedInLearningBaseIE
):
221 IE_NAME
= 'linkedin:learning:course'
222 _VALID_URL
= r
'https?://(?:www\.)?linkedin\.com/learning/(?P<id>[^/?#]+)'
224 'url': 'https://www.linkedin.com/learning/programming-foundations-fundamentals',
226 'id': 'programming-foundations-fundamentals',
227 'title': 'Programming Foundations: Fundamentals',
228 'description': 'md5:76e580b017694eb89dc8e8923fff5c86',
230 'playlist_mincount': 61,
234 def suitable(cls
, url
):
235 return False if LinkedInLearningIE
.suitable(url
) else super(LinkedInLearningCourseIE
, cls
).suitable(url
)
237 def _real_extract(self
, url
):
238 course_slug
= self
._match
_id
(url
)
239 course_data
= self
._call
_api
(course_slug
, 'chapters,description,title')
242 for chapter_number
, chapter
in enumerate(course_data
.get('chapters', []), 1):
243 chapter_title
= chapter
.get('title')
244 chapter_id
= self
._get
_urn
_id
(chapter
)
245 for video
in chapter
.get('videos', []):
246 video_slug
= video
.get('slug')
250 '_type': 'url_transparent',
251 'id': self
._get
_video
_id
(video
, course_slug
, video_slug
),
252 'title': video
.get('title'),
253 'url': 'https://www.linkedin.com/learning/%s/%s' % (course_slug
, video_slug
),
254 'chapter': chapter_title
,
255 'chapter_number': chapter_number
,
256 'chapter_id': chapter_id
,
257 'ie_key': LinkedInLearningIE
.ie_key(),
260 return self
.playlist_result(
261 entries
, course_slug
,
262 course_data
.get('title'),
263 course_data
.get('description'))