You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
6.5 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. ExtractorError,
  7. float_or_none,
  8. int_or_none,
  9. urlencode_postdata,
  10. )
  11. class LinkedInLearningBaseIE(InfoExtractor):
  12. _NETRC_MACHINE = 'linkedin'
  13. def _call_api(self, course_slug, fields, video_slug=None, resolution=None):
  14. query = {
  15. 'courseSlug': course_slug,
  16. 'fields': fields,
  17. 'q': 'slugs',
  18. }
  19. sub = ''
  20. if video_slug:
  21. query.update({
  22. 'videoSlug': video_slug,
  23. 'resolution': '_%s' % resolution,
  24. })
  25. sub = ' %dp' % resolution
  26. api_url = 'https://www.linkedin.com/learning-api/detailedCourses'
  27. return self._download_json(
  28. api_url, video_slug, 'Downloading%s JSON metadata' % sub, headers={
  29. 'Csrf-Token': self._get_cookies(api_url)['JSESSIONID'].value,
  30. }, query=query)['elements'][0]
  31. def _get_urn_id(self, video_data):
  32. urn = video_data.get('urn')
  33. if urn:
  34. mobj = re.search(r'urn:li:lyndaCourse:\d+,(\d+)', urn)
  35. if mobj:
  36. return mobj.group(1)
  37. def _get_video_id(self, video_data, course_slug, video_slug):
  38. return self._get_urn_id(video_data) or '%s/%s' % (course_slug, video_slug)
  39. def _real_initialize(self):
  40. email, password = self._get_login_info()
  41. if email is None:
  42. return
  43. login_page = self._download_webpage(
  44. 'https://www.linkedin.com/uas/login?trk=learning',
  45. None, 'Downloading login page')
  46. action_url = self._search_regex(
  47. r'<form[^>]+action=(["\'])(?P<url>.+?)\1', login_page, 'post url',
  48. default='https://www.linkedin.com/uas/login-submit', group='url')
  49. data = self._hidden_inputs(login_page)
  50. data.update({
  51. 'session_key': email,
  52. 'session_password': password,
  53. })
  54. login_submit_page = self._download_webpage(
  55. action_url, None, 'Logging in',
  56. data=urlencode_postdata(data))
  57. error = self._search_regex(
  58. r'<span[^>]+class="error"[^>]*>\s*(.+?)\s*</span>',
  59. login_submit_page, 'error', default=None)
  60. if error:
  61. raise ExtractorError(error, expected=True)
  62. class LinkedInLearningIE(LinkedInLearningBaseIE):
  63. IE_NAME = 'linkedin:learning'
  64. _VALID_URL = r'https?://(?:www\.)?linkedin\.com/learning/(?P<course_slug>[^/]+)/(?P<id>[^/?#]+)'
  65. _TEST = {
  66. 'url': 'https://www.linkedin.com/learning/programming-foundations-fundamentals/welcome?autoplay=true',
  67. 'md5': 'a1d74422ff0d5e66a792deb996693167',
  68. 'info_dict': {
  69. 'id': '90426',
  70. 'ext': 'mp4',
  71. 'title': 'Welcome',
  72. 'timestamp': 1430396150.82,
  73. 'upload_date': '20150430',
  74. },
  75. }
  76. def _real_extract(self, url):
  77. course_slug, video_slug = re.match(self._VALID_URL, url).groups()
  78. video_data = None
  79. formats = []
  80. for width, height in ((640, 360), (960, 540), (1280, 720)):
  81. video_data = self._call_api(
  82. course_slug, 'selectedVideo', video_slug, height)['selectedVideo']
  83. video_url_data = video_data.get('url') or {}
  84. progressive_url = video_url_data.get('progressiveUrl')
  85. if progressive_url:
  86. formats.append({
  87. 'format_id': 'progressive-%dp' % height,
  88. 'url': progressive_url,
  89. 'height': height,
  90. 'width': width,
  91. 'source_preference': 1,
  92. })
  93. title = video_data['title']
  94. audio_url = video_data.get('audio', {}).get('progressiveUrl')
  95. if audio_url:
  96. formats.append({
  97. 'abr': 64,
  98. 'ext': 'm4a',
  99. 'format_id': 'audio',
  100. 'url': audio_url,
  101. 'vcodec': 'none',
  102. })
  103. streaming_url = video_url_data.get('streamingUrl')
  104. if streaming_url:
  105. formats.extend(self._extract_m3u8_formats(
  106. streaming_url, video_slug, 'mp4',
  107. 'm3u8_native', m3u8_id='hls', fatal=False))
  108. self._sort_formats(formats, ('width', 'height', 'source_preference', 'tbr', 'abr'))
  109. return {
  110. 'id': self._get_video_id(video_data, course_slug, video_slug),
  111. 'title': title,
  112. 'formats': formats,
  113. 'thumbnail': video_data.get('defaultThumbnail'),
  114. 'timestamp': float_or_none(video_data.get('publishedOn'), 1000),
  115. 'duration': int_or_none(video_data.get('durationInSeconds')),
  116. }
  117. class LinkedInLearningCourseIE(LinkedInLearningBaseIE):
  118. IE_NAME = 'linkedin:learning:course'
  119. _VALID_URL = r'https?://(?:www\.)?linkedin\.com/learning/(?P<id>[^/?#]+)'
  120. _TEST = {
  121. 'url': 'https://www.linkedin.com/learning/programming-foundations-fundamentals',
  122. 'info_dict': {
  123. 'id': 'programming-foundations-fundamentals',
  124. 'title': 'Programming Foundations: Fundamentals',
  125. 'description': 'md5:76e580b017694eb89dc8e8923fff5c86',
  126. },
  127. 'playlist_mincount': 61,
  128. }
  129. @classmethod
  130. def suitable(cls, url):
  131. return False if LinkedInLearningIE.suitable(url) else super(LinkedInLearningCourseIE, cls).suitable(url)
  132. def _real_extract(self, url):
  133. course_slug = self._match_id(url)
  134. course_data = self._call_api(course_slug, 'chapters,description,title')
  135. entries = []
  136. for chapter_number, chapter in enumerate(course_data.get('chapters', []), 1):
  137. chapter_title = chapter.get('title')
  138. chapter_id = self._get_urn_id(chapter)
  139. for video in chapter.get('videos', []):
  140. video_slug = video.get('slug')
  141. if not video_slug:
  142. continue
  143. entries.append({
  144. '_type': 'url_transparent',
  145. 'id': self._get_video_id(video, course_slug, video_slug),
  146. 'title': video.get('title'),
  147. 'url': 'https://www.linkedin.com/learning/%s/%s' % (course_slug, video_slug),
  148. 'chapter': chapter_title,
  149. 'chapter_number': chapter_number,
  150. 'chapter_id': chapter_id,
  151. 'ie_key': LinkedInLearningIE.ie_key(),
  152. })
  153. return self.playlist_result(
  154. entries, course_slug,
  155. course_data.get('title'),
  156. course_data.get('description'))