]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/packtpub.py
1 from __future__
import unicode_literals
6 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|subscription\.packtpub\.com)/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 'url': 'https://subscription.packtpub.com/video/web_development/9781787122215/20528/20530/project-intro',
42 'only_matching': True,
44 _NETRC_MACHINE
= 'packtpub'
47 def _real_initialize(self
):
48 username
, password
= self
._get
_login
_info
()
52 self
._TOKEN
= self
._download
_json
(
53 self
._MAPT
_REST
+ '/users/tokens', None,
54 'Downloading Authorization Token', data
=json
.dumps({
57 }).encode())['data']['access']
58 except ExtractorError
as e
:
59 if isinstance(e
.cause
, compat_HTTPError
) and e
.cause
.code
in (400, 401, 404):
60 message
= self
._parse
_json
(e
.cause
.read().decode(), None)['message']
61 raise ExtractorError(message
, expected
=True)
64 def _handle_error(self
, response
):
65 if response
.get('status') != 'success':
67 '% said: %s' % (self
.IE_NAME
, response
['message']),
70 def _download_json(self
, *args
, **kwargs
):
71 response
= super(PacktPubIE
, self
)._download
_json
(*args
, **kwargs
)
72 self
._handle
_error
(response
)
75 def _real_extract(self
, url
):
76 mobj
= re
.match(self
._VALID
_URL
, url
)
77 course_id
, chapter_id
, video_id
= mobj
.group(
78 'course_id', 'chapter_id', 'id')
82 headers
['Authorization'] = 'Bearer ' + self
._TOKEN
83 video
= self
._download
_json
(
84 '%s/users/me/products/%s/chapters/%s/sections/%s'
85 % (self
._MAPT
_REST
, course_id
, chapter_id
, video_id
), video_id
,
86 'Downloading JSON video', headers
=headers
)['data']
88 content
= video
.get('content')
90 self
.raise_login_required('This video is locked')
92 video_url
= content
['file']
94 metadata
= self
._download
_json
(
95 '%s/products/%s/chapters/%s/sections/%s/metadata'
96 % (self
._MAPT
_REST
, course_id
, chapter_id
, video_id
),
99 title
= metadata
['pageTitle']
100 course_title
= metadata
.get('title')
102 title
= remove_end(title
, ' - %s' % course_title
)
103 timestamp
= unified_timestamp(metadata
.get('publicationDate'))
104 thumbnail
= urljoin(self
._PACKT
_BASE
, metadata
.get('filepath'))
110 'thumbnail': thumbnail
,
111 'timestamp': timestamp
,
115 class PacktPubCourseIE(PacktPubBaseIE
):
116 _VALID_URL
= r
'(?P<url>https?://(?:(?:www\.)?packtpub\.com/mapt|subscription\.packtpub\.com)/video/[^/]+/(?P<id>\d+))'
118 'url': 'https://www.packtpub.com/mapt/video/web-development/9781787122215',
120 'id': '9781787122215',
121 'title': 'Learn Nodejs by building 12 projects [Video]',
123 'playlist_count': 90,
125 'url': 'https://subscription.packtpub.com/video/web_development/9781787122215',
126 'only_matching': True,
130 def suitable(cls
, url
):
131 return False if PacktPubIE
.suitable(url
) else super(
132 PacktPubCourseIE
, cls
).suitable(url
)
134 def _real_extract(self
, url
):
135 mobj
= re
.match(self
._VALID
_URL
, url
)
136 url
, course_id
= mobj
.group('url', 'id')
138 course
= self
._download
_json
(
139 '%s/products/%s/metadata' % (self
._MAPT
_REST
, course_id
),
143 for chapter_num
, chapter
in enumerate(course
['tableOfContents'], 1):
144 if chapter
.get('type') != 'chapter':
146 children
= chapter
.get('children')
147 if not isinstance(children
, list):
150 'chapter': chapter
.get('title'),
151 'chapter_number': chapter_num
,
152 'chapter_id': chapter
.get('id'),
154 for section
in children
:
155 if section
.get('type') != 'section':
157 section_url
= section
.get('seoUrl')
158 if not isinstance(section_url
, compat_str
):
161 '_type': 'url_transparent',
162 'url': urljoin(url
+ '/', section_url
),
163 'title': strip_or_none(section
.get('title')),
164 'description': clean_html(section
.get('summary')),
165 'ie_key': PacktPubIE
.ie_key(),
167 entry
.update(chapter_info
)
168 entries
.append(entry
)
170 return self
.playlist_result(entries
, course_id
, course
.get('title'))