]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/packtpub.py
bb668c99953254a0cb5e36389d9b97b4e21c67d8
[youtubedl] / youtube_dl / extractor / packtpub.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import (
7 compat_str,
8 compat_HTTPError,
9 )
10 from ..utils import (
11 clean_html,
12 ExtractorError,
13 remove_end,
14 strip_or_none,
15 unified_timestamp,
16 urljoin,
17 urlencode_postdata,
18 )
19
20
21 class PacktPubBaseIE(InfoExtractor):
22 _PACKT_BASE = 'https://www.packtpub.com'
23 _MAPT_REST = '%s/mapt-rest' % _PACKT_BASE
24
25
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+)'
28
29 _TEST = {
30 'url': 'https://www.packtpub.com/mapt/video/web-development/9781787122215/20528/20530/Project+Intro',
31 'md5': '1e74bd6cfd45d7d07666f4684ef58f70',
32 'info_dict': {
33 'id': '20530',
34 'ext': 'mp4',
35 'title': 'Project Intro',
36 'thumbnail': r're:(?i)^https?://.*\.jpg',
37 'timestamp': 1490918400,
38 'upload_date': '20170331',
39 },
40 }
41 _NETRC_MACHINE = 'packtpub'
42 _TOKEN = None
43
44 def _real_initialize(self):
45 (username, password) = self._get_login_info()
46 if username is None:
47 return
48 webpage = self._download_webpage(self._PACKT_BASE, None)
49 login_form = self._form_hidden_inputs(
50 'packt-user-login-form', webpage)
51 login_form.update({
52 'email': username,
53 'password': password,
54 })
55 self._download_webpage(
56 self._PACKT_BASE, None, 'Logging in as %s' % username,
57 data=urlencode_postdata(login_form))
58 try:
59 self._TOKEN = self._download_json(
60 '%s/users/tokens/sessions' % self._MAPT_REST, None,
61 'Downloading Authorization Token')['data']['token']
62 except ExtractorError as e:
63 if isinstance(e.cause, compat_HTTPError) and e.cause.code in (401, 404):
64 message = self._parse_json(e.cause.read().decode(), None)['message']
65 raise ExtractorError(message, expected=True)
66 raise
67
68 def _handle_error(self, response):
69 if response.get('status') != 'success':
70 raise ExtractorError(
71 '% said: %s' % (self.IE_NAME, response['message']),
72 expected=True)
73
74 def _download_json(self, *args, **kwargs):
75 response = super(PacktPubIE, self)._download_json(*args, **kwargs)
76 self._handle_error(response)
77 return response
78
79 def _real_extract(self, url):
80 mobj = re.match(self._VALID_URL, url)
81 course_id, chapter_id, video_id = mobj.group(
82 'course_id', 'chapter_id', 'id')
83
84 headers = {}
85 if self._TOKEN:
86 headers['Authorization'] = self._TOKEN
87 video = self._download_json(
88 '%s/users/me/products/%s/chapters/%s/sections/%s'
89 % (self._MAPT_REST, course_id, chapter_id, video_id), video_id,
90 'Downloading JSON video', headers=headers)['data']
91
92 content = video.get('content')
93 if not content:
94 self.raise_login_required('This video is locked')
95
96 video_url = content['file']
97
98 metadata = self._download_json(
99 '%s/products/%s/chapters/%s/sections/%s/metadata'
100 % (self._MAPT_REST, course_id, chapter_id, video_id),
101 video_id)['data']
102
103 title = metadata['pageTitle']
104 course_title = metadata.get('title')
105 if course_title:
106 title = remove_end(title, ' - %s' % course_title)
107 timestamp = unified_timestamp(metadata.get('publicationDate'))
108 thumbnail = urljoin(self._PACKT_BASE, metadata.get('filepath'))
109
110 return {
111 'id': video_id,
112 'url': video_url,
113 'title': title,
114 'thumbnail': thumbnail,
115 'timestamp': timestamp,
116 }
117
118
119 class PacktPubCourseIE(PacktPubBaseIE):
120 _VALID_URL = r'(?P<url>https?://(?:www\.)?packtpub\.com/mapt/video/[^/]+/(?P<id>\d+))'
121 _TEST = {
122 'url': 'https://www.packtpub.com/mapt/video/web-development/9781787122215',
123 'info_dict': {
124 'id': '9781787122215',
125 'title': 'Learn Nodejs by building 12 projects [Video]',
126 },
127 'playlist_count': 90,
128 }
129
130 @classmethod
131 def suitable(cls, url):
132 return False if PacktPubIE.suitable(url) else super(
133 PacktPubCourseIE, cls).suitable(url)
134
135 def _real_extract(self, url):
136 mobj = re.match(self._VALID_URL, url)
137 url, course_id = mobj.group('url', 'id')
138
139 course = self._download_json(
140 '%s/products/%s/metadata' % (self._MAPT_REST, course_id),
141 course_id)['data']
142
143 entries = []
144 for chapter_num, chapter in enumerate(course['tableOfContents'], 1):
145 if chapter.get('type') != 'chapter':
146 continue
147 children = chapter.get('children')
148 if not isinstance(children, list):
149 continue
150 chapter_info = {
151 'chapter': chapter.get('title'),
152 'chapter_number': chapter_num,
153 'chapter_id': chapter.get('id'),
154 }
155 for section in children:
156 if section.get('type') != 'section':
157 continue
158 section_url = section.get('seoUrl')
159 if not isinstance(section_url, compat_str):
160 continue
161 entry = {
162 '_type': 'url_transparent',
163 'url': urljoin(url + '/', section_url),
164 'title': strip_or_none(section.get('title')),
165 'description': clean_html(section.get('summary')),
166 'ie_key': PacktPubIE.ie_key(),
167 }
168 entry.update(chapter_info)
169 entries.append(entry)
170
171 return self.playlist_result(entries, course_id, course.get('title'))