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.

164 lines
6.2 KiB

  1. from __future__ import unicode_literals
  2. import json
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. # compat_str,
  7. compat_HTTPError,
  8. )
  9. from ..utils import (
  10. clean_html,
  11. ExtractorError,
  12. # remove_end,
  13. str_or_none,
  14. strip_or_none,
  15. unified_timestamp,
  16. # urljoin,
  17. )
  18. class PacktPubBaseIE(InfoExtractor):
  19. # _PACKT_BASE = 'https://www.packtpub.com'
  20. _STATIC_PRODUCTS_BASE = 'https://static.packt-cdn.com/products/'
  21. class PacktPubIE(PacktPubBaseIE):
  22. _VALID_URL = r'https?://(?:(?:www\.)?packtpub\.com/mapt|subscription\.packtpub\.com)/video/[^/]+/(?P<course_id>\d+)/(?P<chapter_id>[^/]+)/(?P<id>[^/]+)(?:/(?P<display_id>[^/?&#]+))?'
  23. _TESTS = [{
  24. 'url': 'https://www.packtpub.com/mapt/video/web-development/9781787122215/20528/20530/Project+Intro',
  25. 'md5': '1e74bd6cfd45d7d07666f4684ef58f70',
  26. 'info_dict': {
  27. 'id': '20530',
  28. 'ext': 'mp4',
  29. 'title': 'Project Intro',
  30. 'thumbnail': r're:(?i)^https?://.*\.jpg',
  31. 'timestamp': 1490918400,
  32. 'upload_date': '20170331',
  33. },
  34. }, {
  35. 'url': 'https://subscription.packtpub.com/video/web_development/9781787122215/20528/20530/project-intro',
  36. 'only_matching': True,
  37. }, {
  38. 'url': 'https://subscription.packtpub.com/video/programming/9781838988906/p1/video1_1/business-card-project',
  39. 'only_matching': True,
  40. }]
  41. _NETRC_MACHINE = 'packtpub'
  42. _TOKEN = None
  43. def _real_initialize(self):
  44. username, password = self._get_login_info()
  45. if username is None:
  46. return
  47. try:
  48. self._TOKEN = self._download_json(
  49. 'https://services.packtpub.com/auth-v1/users/tokens', None,
  50. 'Downloading Authorization Token', data=json.dumps({
  51. 'username': username,
  52. 'password': password,
  53. }).encode())['data']['access']
  54. except ExtractorError as e:
  55. if isinstance(e.cause, compat_HTTPError) and e.cause.code in (400, 401, 404):
  56. message = self._parse_json(e.cause.read().decode(), None)['message']
  57. raise ExtractorError(message, expected=True)
  58. raise
  59. def _real_extract(self, url):
  60. course_id, chapter_id, video_id, display_id = re.match(self._VALID_URL, url).groups()
  61. headers = {}
  62. if self._TOKEN:
  63. headers['Authorization'] = 'Bearer ' + self._TOKEN
  64. try:
  65. video_url = self._download_json(
  66. 'https://services.packtpub.com/products-v1/products/%s/%s/%s' % (course_id, chapter_id, video_id), video_id,
  67. 'Downloading JSON video', headers=headers)['data']
  68. except ExtractorError as e:
  69. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 400:
  70. self.raise_login_required('This video is locked')
  71. raise
  72. # TODO: find a better way to avoid duplicating course requests
  73. # metadata = self._download_json(
  74. # '%s/products/%s/chapters/%s/sections/%s/metadata'
  75. # % (self._MAPT_REST, course_id, chapter_id, video_id),
  76. # video_id)['data']
  77. # title = metadata['pageTitle']
  78. # course_title = metadata.get('title')
  79. # if course_title:
  80. # title = remove_end(title, ' - %s' % course_title)
  81. # timestamp = unified_timestamp(metadata.get('publicationDate'))
  82. # thumbnail = urljoin(self._PACKT_BASE, metadata.get('filepath'))
  83. return {
  84. 'id': video_id,
  85. 'url': video_url,
  86. 'title': display_id or video_id, # title,
  87. # 'thumbnail': thumbnail,
  88. # 'timestamp': timestamp,
  89. }
  90. class PacktPubCourseIE(PacktPubBaseIE):
  91. _VALID_URL = r'(?P<url>https?://(?:(?:www\.)?packtpub\.com/mapt|subscription\.packtpub\.com)/video/[^/]+/(?P<id>\d+))'
  92. _TESTS = [{
  93. 'url': 'https://www.packtpub.com/mapt/video/web-development/9781787122215',
  94. 'info_dict': {
  95. 'id': '9781787122215',
  96. 'title': 'Learn Nodejs by building 12 projects [Video]',
  97. 'description': 'md5:489da8d953f416e51927b60a1c7db0aa',
  98. },
  99. 'playlist_count': 90,
  100. }, {
  101. 'url': 'https://subscription.packtpub.com/video/web_development/9781787122215',
  102. 'only_matching': True,
  103. }]
  104. @classmethod
  105. def suitable(cls, url):
  106. return False if PacktPubIE.suitable(url) else super(
  107. PacktPubCourseIE, cls).suitable(url)
  108. def _real_extract(self, url):
  109. mobj = re.match(self._VALID_URL, url)
  110. url, course_id = mobj.group('url', 'id')
  111. course = self._download_json(
  112. self._STATIC_PRODUCTS_BASE + '%s/toc' % course_id, course_id)
  113. metadata = self._download_json(
  114. self._STATIC_PRODUCTS_BASE + '%s/summary' % course_id,
  115. course_id, fatal=False) or {}
  116. entries = []
  117. for chapter_num, chapter in enumerate(course['chapters'], 1):
  118. chapter_id = str_or_none(chapter.get('id'))
  119. sections = chapter.get('sections')
  120. if not chapter_id or not isinstance(sections, list):
  121. continue
  122. chapter_info = {
  123. 'chapter': chapter.get('title'),
  124. 'chapter_number': chapter_num,
  125. 'chapter_id': chapter_id,
  126. }
  127. for section in sections:
  128. section_id = str_or_none(section.get('id'))
  129. if not section_id or section.get('contentType') != 'video':
  130. continue
  131. entry = {
  132. '_type': 'url_transparent',
  133. 'url': '/'.join([url, chapter_id, section_id]),
  134. 'title': strip_or_none(section.get('title')),
  135. 'description': clean_html(section.get('summary')),
  136. 'thumbnail': metadata.get('coverImage'),
  137. 'timestamp': unified_timestamp(metadata.get('publicationDate')),
  138. 'ie_key': PacktPubIE.ie_key(),
  139. }
  140. entry.update(chapter_info)
  141. entries.append(entry)
  142. return self.playlist_result(
  143. entries, course_id, metadata.get('title'),
  144. clean_html(metadata.get('about')))