]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/packtpub.py
8ed3c6347c90a6bfabe18d51e796a2df4737a804
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/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
()
49 self
._TOKEN
= self
._download
_json
(
50 self
._MAPT
_REST
+ '/users/tokens', None,
51 'Downloading Authorization Token', data
=json
.dumps({
54 }).encode())['data']['access']
55 except ExtractorError
as e
:
56 if isinstance(e
.cause
, compat_HTTPError
) and e
.cause
.code
in (400, 401, 404):
57 message
= self
._parse
_json
(e
.cause
.read().decode(), None)['message']
58 raise ExtractorError(message
, expected
=True)
61 def _handle_error(self
, response
):
62 if response
.get('status') != 'success':
64 '% said: %s' % (self
.IE_NAME
, response
['message']),
67 def _download_json(self
, *args
, **kwargs
):
68 response
= super(PacktPubIE
, self
)._download
_json
(*args
, **kwargs
)
69 self
._handle
_error
(response
)
72 def _real_extract(self
, url
):
73 mobj
= re
.match(self
._VALID
_URL
, url
)
74 course_id
, chapter_id
, video_id
= mobj
.group(
75 'course_id', 'chapter_id', 'id')
79 headers
['Authorization'] = 'Bearer ' + self
._TOKEN
80 video
= self
._download
_json
(
81 '%s/users/me/products/%s/chapters/%s/sections/%s'
82 % (self
._MAPT
_REST
, course_id
, chapter_id
, video_id
), video_id
,
83 'Downloading JSON video', headers
=headers
)['data']
85 content
= video
.get('content')
87 self
.raise_login_required('This video is locked')
89 video_url
= content
['file']
91 metadata
= self
._download
_json
(
92 '%s/products/%s/chapters/%s/sections/%s/metadata'
93 % (self
._MAPT
_REST
, course_id
, chapter_id
, video_id
),
96 title
= metadata
['pageTitle']
97 course_title
= metadata
.get('title')
99 title
= remove_end(title
, ' - %s' % course_title
)
100 timestamp
= unified_timestamp(metadata
.get('publicationDate'))
101 thumbnail
= urljoin(self
._PACKT
_BASE
, metadata
.get('filepath'))
107 'thumbnail': thumbnail
,
108 'timestamp': timestamp
,
112 class PacktPubCourseIE(PacktPubBaseIE
):
113 _VALID_URL
= r
'(?P<url>https?://(?:www\.)?packtpub\.com/mapt/video/[^/]+/(?P<id>\d+))'
115 'url': 'https://www.packtpub.com/mapt/video/web-development/9781787122215',
117 'id': '9781787122215',
118 'title': 'Learn Nodejs by building 12 projects [Video]',
120 'playlist_count': 90,
124 def suitable(cls
, url
):
125 return False if PacktPubIE
.suitable(url
) else super(
126 PacktPubCourseIE
, cls
).suitable(url
)
128 def _real_extract(self
, url
):
129 mobj
= re
.match(self
._VALID
_URL
, url
)
130 url
, course_id
= mobj
.group('url', 'id')
132 course
= self
._download
_json
(
133 '%s/products/%s/metadata' % (self
._MAPT
_REST
, course_id
),
137 for chapter_num
, chapter
in enumerate(course
['tableOfContents'], 1):
138 if chapter
.get('type') != 'chapter':
140 children
= chapter
.get('children')
141 if not isinstance(children
, list):
144 'chapter': chapter
.get('title'),
145 'chapter_number': chapter_num
,
146 'chapter_id': chapter
.get('id'),
148 for section
in children
:
149 if section
.get('type') != 'section':
151 section_url
= section
.get('seoUrl')
152 if not isinstance(section_url
, compat_str
):
155 '_type': 'url_transparent',
156 'url': urljoin(url
+ '/', section_url
),
157 'title': strip_or_none(section
.get('title')),
158 'description': clean_html(section
.get('summary')),
159 'ie_key': PacktPubIE
.ie_key(),
161 entry
.update(chapter_info
)
162 entries
.append(entry
)
164 return self
.playlist_result(entries
, course_id
, course
.get('title'))