]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/packtpub.py
1 from __future__
import unicode_literals
6 from .common
import InfoExtractor
22 class PacktPubBaseIE(InfoExtractor
):
23 # _PACKT_BASE = 'https://www.packtpub.com'
24 _STATIC_PRODUCTS_BASE
= 'https://static.packt-cdn.com/products/'
27 class PacktPubIE(PacktPubBaseIE
):
28 _VALID_URL
= r
'https?://(?:(?:www\.)?packtpub\.com/mapt|subscription\.packtpub\.com)/video/[^/]+/(?P<course_id>\d+)/(?P<chapter_id>[^/]+)/(?P<id>[^/]+)(?:/(?P<display_id>[^/?&#]+))?'
31 'url': 'https://www.packtpub.com/mapt/video/web-development/9781787122215/20528/20530/Project+Intro',
32 'md5': '1e74bd6cfd45d7d07666f4684ef58f70',
36 'title': 'Project Intro',
37 'thumbnail': r
're:(?i)^https?://.*\.jpg',
38 'timestamp': 1490918400,
39 'upload_date': '20170331',
42 'url': 'https://subscription.packtpub.com/video/web_development/9781787122215/20528/20530/project-intro',
43 'only_matching': True,
45 'url': 'https://subscription.packtpub.com/video/programming/9781838988906/p1/video1_1/business-card-project',
46 'only_matching': True,
48 _NETRC_MACHINE
= 'packtpub'
51 def _real_initialize(self
):
52 username
, password
= self
._get
_login
_info
()
56 self
._TOKEN
= self
._download
_json
(
57 'https://services.packtpub.com/auth-v1/users/tokens', None,
58 'Downloading Authorization Token', data
=json
.dumps({
61 }).encode())['data']['access']
62 except ExtractorError
as e
:
63 if isinstance(e
.cause
, compat_HTTPError
) and e
.cause
.code
in (400, 401, 404):
64 message
= self
._parse
_json
(e
.cause
.read().decode(), None)['message']
65 raise ExtractorError(message
, expected
=True)
68 def _real_extract(self
, url
):
69 course_id
, chapter_id
, video_id
, display_id
= re
.match(self
._VALID
_URL
, url
).groups()
73 headers
['Authorization'] = 'Bearer ' + self
._TOKEN
75 video_url
= self
._download
_json
(
76 'https://services.packtpub.com/products-v1/products/%s/%s/%s' % (course_id
, chapter_id
, video_id
), video_id
,
77 'Downloading JSON video', headers
=headers
)['data']
78 except ExtractorError
as e
:
79 if isinstance(e
.cause
, compat_HTTPError
) and e
.cause
.code
== 400:
80 self
.raise_login_required('This video is locked')
83 # TODO: find a better way to avoid duplicating course requests
84 # metadata = self._download_json(
85 # '%s/products/%s/chapters/%s/sections/%s/metadata'
86 # % (self._MAPT_REST, course_id, chapter_id, video_id),
89 # title = metadata['pageTitle']
90 # course_title = metadata.get('title')
92 # title = remove_end(title, ' - %s' % course_title)
93 # timestamp = unified_timestamp(metadata.get('publicationDate'))
94 # thumbnail = urljoin(self._PACKT_BASE, metadata.get('filepath'))
99 'title': display_id
or video_id
, # title,
100 # 'thumbnail': thumbnail,
101 # 'timestamp': timestamp,
105 class PacktPubCourseIE(PacktPubBaseIE
):
106 _VALID_URL
= r
'(?P<url>https?://(?:(?:www\.)?packtpub\.com/mapt|subscription\.packtpub\.com)/video/[^/]+/(?P<id>\d+))'
108 'url': 'https://www.packtpub.com/mapt/video/web-development/9781787122215',
110 'id': '9781787122215',
111 'title': 'Learn Nodejs by building 12 projects [Video]',
112 'description': 'md5:489da8d953f416e51927b60a1c7db0aa',
114 'playlist_count': 90,
116 'url': 'https://subscription.packtpub.com/video/web_development/9781787122215',
117 'only_matching': True,
121 def suitable(cls
, url
):
122 return False if PacktPubIE
.suitable(url
) else super(
123 PacktPubCourseIE
, cls
).suitable(url
)
125 def _real_extract(self
, url
):
126 mobj
= re
.match(self
._VALID
_URL
, url
)
127 url
, course_id
= mobj
.group('url', 'id')
129 course
= self
._download
_json
(
130 self
._STATIC
_PRODUCTS
_BASE
+ '%s/toc' % course_id
, course_id
)
131 metadata
= self
._download
_json
(
132 self
._STATIC
_PRODUCTS
_BASE
+ '%s/summary' % course_id
,
133 course_id
, fatal
=False) or {}
136 for chapter_num
, chapter
in enumerate(course
['chapters'], 1):
137 chapter_id
= str_or_none(chapter
.get('id'))
138 sections
= chapter
.get('sections')
139 if not chapter_id
or not isinstance(sections
, list):
142 'chapter': chapter
.get('title'),
143 'chapter_number': chapter_num
,
144 'chapter_id': chapter_id
,
146 for section
in sections
:
147 section_id
= str_or_none(section
.get('id'))
148 if not section_id
or section
.get('contentType') != 'video':
151 '_type': 'url_transparent',
152 'url': '/'.join([url
, chapter_id
, section_id
]),
153 'title': strip_or_none(section
.get('title')),
154 'description': clean_html(section
.get('summary')),
155 'thumbnail': metadata
.get('coverImage'),
156 'timestamp': unified_timestamp(metadata
.get('publicationDate')),
157 'ie_key': PacktPubIE
.ie_key(),
159 entry
.update(chapter_info
)
160 entries
.append(entry
)
162 return self
.playlist_result(
163 entries
, course_id
, metadata
.get('title'),
164 clean_html(metadata
.get('about')))