1 from __future__ import unicode_literals
5 from .common import InfoExtractor
21 class PacktPubBaseIE(InfoExtractor):
22 _PACKT_BASE = 'https://www.packtpub.com'
23 _MAPT_REST = '%s/mapt-rest' % _PACKT_BASE
26 class PacktPubIE(PacktPubBaseIE):
27 _VALID_URL = r'https?://(?:www\.)?packtpub\.com/mapt/video/[^/]+/(?P<course_id>\d+)/(?P<chapter_id>\d+)/(?P<id>\d+)'
30 'url': 'https://www.packtpub.com/mapt/video/web-development/9781787122215/20528/20530/Project+Intro',
31 'md5': '1e74bd6cfd45d7d07666f4684ef58f70',
35 'title': 'Project Intro',
36 'thumbnail': r're:(?i)^https?://.*\.jpg',
37 'timestamp': 1490918400,
38 'upload_date': '20170331',
41 _NETRC_MACHINE = 'packtpub'
44 def _real_initialize(self):
45 (username, password) = self._get_login_info()
48 webpage = self._download_webpage(self._PACKT_BASE, None)
49 login_form = self._form_hidden_inputs(
50 'packt-user-login-form', webpage)
55 self._download_webpage(
56 self._PACKT_BASE, None, 'Logging in as %s' % username,
57 data=urlencode_postdata(login_form))
59 self._TOKEN = self._download_json(
60 '%s/users/tokens/sessions' % self._MAPT_REST, None,
61 'Downloading Authorization Token')['data']['token']
62 except ExtractorError as e:
63 if isinstance(e.cause, compat_HTTPError) and e.cause.code in (401, 404):
64 message = self._parse_json(e.cause.read().decode(), None)['message']
65 raise ExtractorError(message, expected=True)
68 def _handle_error(self, response):
69 if response.get('status') != 'success':
71 '% said: %s' % (self.IE_NAME, response['message']),
74 def _download_json(self, *args, **kwargs):
75 response = super(PacktPubIE, self)._download_json(*args, **kwargs)
76 self._handle_error(response)
79 def _real_extract(self, url):
80 mobj = re.match(self._VALID_URL, url)
81 course_id, chapter_id, video_id = mobj.group(
82 'course_id', 'chapter_id', 'id')
86 headers['Authorization'] = self._TOKEN
87 video = self._download_json(
88 '%s/users/me/products/%s/chapters/%s/sections/%s'
89 % (self._MAPT_REST, course_id, chapter_id, video_id), video_id,
90 'Downloading JSON video', headers=headers)['data']
92 content = video.get('content')
94 self.raise_login_required('This video is locked')
96 video_url = content['file']
98 metadata = self._download_json(
99 '%s/products/%s/chapters/%s/sections/%s/metadata'
100 % (self._MAPT_REST, course_id, chapter_id, video_id),
103 title = metadata['pageTitle']
104 course_title = metadata.get('title')
106 title = remove_end(title, ' - %s' % course_title)
107 timestamp = unified_timestamp(metadata.get('publicationDate'))
108 thumbnail = urljoin(self._PACKT_BASE, metadata.get('filepath'))
114 'thumbnail': thumbnail,
115 'timestamp': timestamp,
119 class PacktPubCourseIE(PacktPubBaseIE):
120 _VALID_URL = r'(?P<url>https?://(?:www\.)?packtpub\.com/mapt/video/[^/]+/(?P<id>\d+))'
122 'url': 'https://www.packtpub.com/mapt/video/web-development/9781787122215',
124 'id': '9781787122215',
125 'title': 'Learn Nodejs by building 12 projects [Video]',
127 'playlist_count': 90,
131 def suitable(cls, url):
132 return False if PacktPubIE.suitable(url) else super(
133 PacktPubCourseIE, cls).suitable(url)
135 def _real_extract(self, url):
136 mobj = re.match(self._VALID_URL, url)
137 url, course_id = mobj.group('url', 'id')
139 course = self._download_json(
140 '%s/products/%s/metadata' % (self._MAPT_REST, course_id),
144 for chapter_num, chapter in enumerate(course['tableOfContents'], 1):
145 if chapter.get('type') != 'chapter':
147 children = chapter.get('children')
148 if not isinstance(children, list):
151 'chapter': chapter.get('title'),
152 'chapter_number': chapter_num,
153 'chapter_id': chapter.get('id'),
155 for section in children:
156 if section.get('type') != 'section':
158 section_url = section.get('seoUrl')
159 if not isinstance(section_url, compat_str):
162 '_type': 'url_transparent',
163 'url': urljoin(url + '/', section_url),
164 'title': strip_or_none(section.get('title')),
165 'description': clean_html(section.get('summary')),
166 'ie_key': PacktPubIE.ie_key(),
168 entry.update(chapter_info)
169 entries.append(entry)
171 return self.playlist_result(entries, course_id, course.get('title'))