linkedin.py 6.3 KB

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