3 from .common
import InfoExtractor
4 from ..networking
.exceptions
import HTTPError
15 class PacktPubBaseIE(InfoExtractor
):
16 # _PACKT_BASE = 'https://www.packtpub.com'
17 _STATIC_PRODUCTS_BASE
= 'https://static.packt-cdn.com/products/'
20 class PacktPubIE(PacktPubBaseIE
):
21 _VALID_URL
= r
'https?://(?:(?:www\.)?packtpub\.com/mapt|subscription\.packtpub\.com)/video/[^/]+/(?P<course_id>\d+)/(?P<chapter_id>[^/]+)/(?P<id>[^/]+)(?:/(?P<display_id>[^/?&#]+))?'
24 'url': 'https://www.packtpub.com/mapt/video/web-development/9781787122215/20528/20530/Project+Intro',
25 'md5': '1e74bd6cfd45d7d07666f4684ef58f70',
29 'title': 'Project Intro',
30 'thumbnail': r
're:(?i)^https?://.*\.jpg',
31 'timestamp': 1490918400,
32 'upload_date': '20170331',
35 'url': 'https://subscription.packtpub.com/video/web_development/9781787122215/20528/20530/project-intro',
36 'only_matching': True,
38 'url': 'https://subscription.packtpub.com/video/programming/9781838988906/p1/video1_1/business-card-project',
39 'only_matching': True,
41 _NETRC_MACHINE
= 'packtpub'
44 def _perform_login(self
, username
, password
):
46 self
._TOKEN
= self
._download
_json
(
47 'https://services.packtpub.com/auth-v1/users/tokens', None,
48 'Downloading Authorization Token', data
=json
.dumps({
51 }).encode())['data']['access']
52 except ExtractorError
as e
:
53 if isinstance(e
.cause
, HTTPError
) and e
.cause
.status
in (400, 401, 404):
54 message
= self
._parse
_json
(e
.cause
.response
.read().decode(), None)['message']
55 raise ExtractorError(message
, expected
=True)
58 def _real_extract(self
, url
):
59 course_id
, chapter_id
, video_id
, display_id
= self
._match
_valid
_url
(url
).groups()
63 headers
['Authorization'] = 'Bearer ' + self
._TOKEN
65 video_url
= self
._download
_json
(
66 f
'https://services.packtpub.com/products-v1/products/{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
, HTTPError
) and e
.cause
.status
== 400:
70 self
.raise_login_required('This video is locked')
73 # TODO: find a better way to avoid duplicating course requests
74 # metadata = self._download_json(
75 # '%s/products/%s/chapters/%s/sections/%s/metadata'
76 # % (self._MAPT_REST, course_id, chapter_id, video_id),
79 # title = metadata['pageTitle']
80 # course_title = metadata.get('title')
82 # title = remove_end(title, ' - %s' % course_title)
83 # timestamp = unified_timestamp(metadata.get('publicationDate'))
84 # thumbnail = urljoin(self._PACKT_BASE, metadata.get('filepath'))
89 'title': display_id
or video_id
, # title,
90 # 'thumbnail': thumbnail,
91 # 'timestamp': timestamp,
95 class PacktPubCourseIE(PacktPubBaseIE
):
96 _VALID_URL
= r
'(?P<url>https?://(?:(?:www\.)?packtpub\.com/mapt|subscription\.packtpub\.com)/video/[^/]+/(?P<id>\d+))'
98 'url': 'https://www.packtpub.com/mapt/video/web-development/9781787122215',
100 'id': '9781787122215',
101 'title': 'Learn Nodejs by building 12 projects [Video]',
102 'description': 'md5:489da8d953f416e51927b60a1c7db0aa',
104 'playlist_count': 90,
106 'url': 'https://subscription.packtpub.com/video/web_development/9781787122215',
107 'only_matching': True,
111 def suitable(cls
, url
):
112 return False if PacktPubIE
.suitable(url
) else super().suitable(url
)
114 def _real_extract(self
, url
):
115 mobj
= self
._match
_valid
_url
(url
)
116 url
, course_id
= mobj
.group('url', 'id')
118 course
= self
._download
_json
(
119 self
._STATIC
_PRODUCTS
_BASE
+ f
'{course_id}/toc', course_id
)
120 metadata
= self
._download
_json
(
121 self
._STATIC
_PRODUCTS
_BASE
+ f
'{course_id}/summary',
122 course_id
, fatal
=False) or {}
125 for chapter_num
, chapter
in enumerate(course
['chapters'], 1):
126 chapter_id
= str_or_none(chapter
.get('id'))
127 sections
= chapter
.get('sections')
128 if not chapter_id
or not isinstance(sections
, list):
131 'chapter': chapter
.get('title'),
132 'chapter_number': chapter_num
,
133 'chapter_id': chapter_id
,
135 for section
in sections
:
136 section_id
= str_or_none(section
.get('id'))
137 if not section_id
or section
.get('contentType') != 'video':
140 '_type': 'url_transparent',
141 'url': '/'.join([url
, chapter_id
, section_id
]),
142 'title': strip_or_none(section
.get('title')),
143 'description': clean_html(section
.get('summary')),
144 'thumbnail': metadata
.get('coverImage'),
145 'timestamp': unified_timestamp(metadata
.get('publicationDate')),
146 'ie_key': PacktPubIE
.ie_key(),
148 entry
.update(chapter_info
)
149 entries
.append(entry
)
151 return self
.playlist_result(
152 entries
, course_id
, metadata
.get('title'),
153 clean_html(metadata
.get('about')))