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.

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