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.

138 lines
4.6 KiB

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