]>
jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/packtpub.py
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 '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
, 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(
113 PacktPubCourseIE
, cls
).suitable(url
)
115 def _real_extract(self
, url
):
116 mobj
= self
._match
_valid
_url
(url
)
117 url
, course_id
= mobj
.group('url', 'id')
119 course
= self
._download
_json
(
120 self
._STATIC
_PRODUCTS
_BASE
+ '%s/toc' % course_id
, course_id
)
121 metadata
= self
._download
_json
(
122 self
._STATIC
_PRODUCTS
_BASE
+ '%s/summary' % course_id
,
123 course_id
, fatal
=False) or {}
126 for chapter_num
, chapter
in enumerate(course
['chapters'], 1):
127 chapter_id
= str_or_none(chapter
.get('id'))
128 sections
= chapter
.get('sections')
129 if not chapter_id
or not isinstance(sections
, list):
132 'chapter': chapter
.get('title'),
133 'chapter_number': chapter_num
,
134 'chapter_id': chapter_id
,
136 for section
in sections
:
137 section_id
= str_or_none(section
.get('id'))
138 if not section_id
or section
.get('contentType') != 'video':
141 '_type': 'url_transparent',
142 'url': '/'.join([url
, chapter_id
, section_id
]),
143 'title': strip_or_none(section
.get('title')),
144 'description': clean_html(section
.get('summary')),
145 'thumbnail': metadata
.get('coverImage'),
146 'timestamp': unified_timestamp(metadata
.get('publicationDate')),
147 'ie_key': PacktPubIE
.ie_key(),
149 entry
.update(chapter_info
)
150 entries
.append(entry
)
152 return self
.playlist_result(
153 entries
, course_id
, metadata
.get('title'),
154 clean_html(metadata
.get('about')))