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.

171 lines
5.8 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import (
  5. compat_str,
  6. compat_HTTPError,
  7. )
  8. from ..utils import (
  9. clean_html,
  10. ExtractorError,
  11. remove_end,
  12. strip_or_none,
  13. unified_timestamp,
  14. urljoin,
  15. urlencode_postdata,
  16. )
  17. class PacktPubBaseIE(InfoExtractor):
  18. _PACKT_BASE = 'https://www.packtpub.com'
  19. _MAPT_REST = '%s/mapt-rest' % _PACKT_BASE
  20. class PacktPubIE(PacktPubBaseIE):
  21. _VALID_URL = r'https?://(?:www\.)?packtpub\.com/mapt/video/[^/]+/(?P<course_id>\d+)/(?P<chapter_id>\d+)/(?P<id>\d+)'
  22. _TEST = {
  23. 'url': 'https://www.packtpub.com/mapt/video/web-development/9781787122215/20528/20530/Project+Intro',
  24. 'md5': '1e74bd6cfd45d7d07666f4684ef58f70',
  25. 'info_dict': {
  26. 'id': '20530',
  27. 'ext': 'mp4',
  28. 'title': 'Project Intro',
  29. 'thumbnail': r're:(?i)^https?://.*\.jpg',
  30. 'timestamp': 1490918400,
  31. 'upload_date': '20170331',
  32. },
  33. }
  34. _NETRC_MACHINE = 'packtpub'
  35. _TOKEN = None
  36. def _real_initialize(self):
  37. (username, password) = self._get_login_info()
  38. if username is None:
  39. return
  40. webpage = self._download_webpage(self._PACKT_BASE, None)
  41. login_form = self._form_hidden_inputs(
  42. 'packt-user-login-form', webpage)
  43. login_form.update({
  44. 'email': username,
  45. 'password': password,
  46. })
  47. self._download_webpage(
  48. self._PACKT_BASE, None, 'Logging in as %s' % username,
  49. data=urlencode_postdata(login_form))
  50. try:
  51. self._TOKEN = self._download_json(
  52. '%s/users/tokens/sessions' % self._MAPT_REST, None,
  53. 'Downloading Authorization Token')['data']['token']
  54. except ExtractorError as e:
  55. if isinstance(e.cause, compat_HTTPError) and e.cause.code in (401, 404):
  56. message = self._parse_json(e.cause.read().decode(), None)['message']
  57. raise ExtractorError(message, expected=True)
  58. raise
  59. def _handle_error(self, response):
  60. if response.get('status') != 'success':
  61. raise ExtractorError(
  62. '% said: %s' % (self.IE_NAME, response['message']),
  63. expected=True)
  64. def _download_json(self, *args, **kwargs):
  65. response = super(PacktPubIE, self)._download_json(*args, **kwargs)
  66. self._handle_error(response)
  67. return response
  68. def _real_extract(self, url):
  69. mobj = re.match(self._VALID_URL, url)
  70. course_id, chapter_id, video_id = mobj.group(
  71. 'course_id', 'chapter_id', 'id')
  72. headers = {}
  73. if self._TOKEN:
  74. headers['Authorization'] = self._TOKEN
  75. video = self._download_json(
  76. '%s/users/me/products/%s/chapters/%s/sections/%s'
  77. % (self._MAPT_REST, course_id, chapter_id, video_id), video_id,
  78. 'Downloading JSON video', headers=headers)['data']
  79. content = video.get('content')
  80. if not content:
  81. self.raise_login_required('This video is locked')
  82. video_url = content['file']
  83. metadata = self._download_json(
  84. '%s/products/%s/chapters/%s/sections/%s/metadata'
  85. % (self._MAPT_REST, course_id, chapter_id, video_id),
  86. video_id)['data']
  87. title = metadata['pageTitle']
  88. course_title = metadata.get('title')
  89. if course_title:
  90. title = remove_end(title, ' - %s' % course_title)
  91. timestamp = unified_timestamp(metadata.get('publicationDate'))
  92. thumbnail = urljoin(self._PACKT_BASE, metadata.get('filepath'))
  93. return {
  94. 'id': video_id,
  95. 'url': video_url,
  96. 'title': title,
  97. 'thumbnail': thumbnail,
  98. 'timestamp': timestamp,
  99. }
  100. class PacktPubCourseIE(PacktPubBaseIE):
  101. _VALID_URL = r'(?P<url>https?://(?:www\.)?packtpub\.com/mapt/video/[^/]+/(?P<id>\d+))'
  102. _TEST = {
  103. 'url': 'https://www.packtpub.com/mapt/video/web-development/9781787122215',
  104. 'info_dict': {
  105. 'id': '9781787122215',
  106. 'title': 'Learn Nodejs by building 12 projects [Video]',
  107. },
  108. 'playlist_count': 90,
  109. }
  110. @classmethod
  111. def suitable(cls, url):
  112. return False if PacktPubIE.suitable(url) else super(
  113. PacktPubCourseIE, cls).suitable(url)
  114. def _real_extract(self, url):
  115. mobj = re.match(self._VALID_URL, url)
  116. url, course_id = mobj.group('url', 'id')
  117. course = self._download_json(
  118. '%s/products/%s/metadata' % (self._MAPT_REST, course_id),
  119. course_id)['data']
  120. entries = []
  121. for chapter_num, chapter in enumerate(course['tableOfContents'], 1):
  122. if chapter.get('type') != 'chapter':
  123. continue
  124. children = chapter.get('children')
  125. if not isinstance(children, list):
  126. continue
  127. chapter_info = {
  128. 'chapter': chapter.get('title'),
  129. 'chapter_number': chapter_num,
  130. 'chapter_id': chapter.get('id'),
  131. }
  132. for section in children:
  133. if section.get('type') != 'section':
  134. continue
  135. section_url = section.get('seoUrl')
  136. if not isinstance(section_url, compat_str):
  137. continue
  138. entry = {
  139. '_type': 'url_transparent',
  140. 'url': urljoin(url + '/', section_url),
  141. 'title': strip_or_none(section.get('title')),
  142. 'description': clean_html(section.get('summary')),
  143. 'ie_key': PacktPubIE.ie_key(),
  144. }
  145. entry.update(chapter_info)
  146. entries.append(entry)
  147. return self.playlist_result(entries, course_id, course.get('title'))