]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/safari.py
[ie/orf:on] Improve extraction (#9677)
[yt-dlp.git] / yt_dlp / extractor / safari.py
CommitLineData
a9e03736 1import json
32d687f5 2import re
32d687f5 3
4from .common import InfoExtractor
32d687f5 5
a9e03736
S
6from ..compat import (
7 compat_parse_qs,
a9e03736
S
8 compat_urlparse,
9)
32d687f5 10from ..utils import (
11 ExtractorError,
bcb668de 12 update_url_query,
32d687f5 13)
14
15
16class SafariBaseIE(InfoExtractor):
7f41a598 17 _LOGIN_URL = 'https://learning.oreilly.com/accounts/login/'
31c48098
S
18 _NETRC_MACHINE = 'safari'
19
7f41a598 20 _API_BASE = 'https://learning.oreilly.com/api/v1'
31c48098 21 _API_FORMAT = 'json'
32d687f5 22
23 LOGGED_IN = False
24
52efa4b3 25 def _perform_login(self, username, password):
a9e03736
S
26 _, urlh = self._download_webpage_handle(
27 'https://learning.oreilly.com/accounts/login-check/', None,
28 'Downloading login page')
4244a13a 29
a9e03736 30 def is_logged(urlh):
3d2623a8 31 return 'learning.oreilly.com/home/' in urlh.url
4244a13a 32
a9e03736 33 if is_logged(urlh):
4244a13a
S
34 self.LOGGED_IN = True
35 return
32d687f5 36
3d2623a8 37 redirect_url = urlh.url
a9e03736
S
38 parsed_url = compat_urlparse.urlparse(redirect_url)
39 qs = compat_parse_qs(parsed_url.query)
40 next_uri = compat_urlparse.urljoin(
41 'https://api.oreilly.com', qs['next'][0])
42
43 auth, urlh = self._download_json_handle(
44 'https://www.oreilly.com/member/auth/login/', None, 'Logging in',
45 data=json.dumps({
46 'email': username,
47 'password': password,
48 'redirect_uri': next_uri,
49 }).encode(), headers={
50 'Content-Type': 'application/json',
51 'Referer': redirect_url,
52 }, expected_status=400)
53
54 credentials = auth.get('credentials')
55 if (not auth.get('logged_in') and not auth.get('redirect_uri')
56 and credentials):
57 raise ExtractorError(
58 'Unable to login: %s' % credentials, expected=True)
32d687f5 59
d1fcf255 60 # oreilly serves two same instances of the following cookies
61 # in Set-Cookie header and expects first one to be actually set
62 for cookie in ('groot_sessionid', 'orm-jwt', 'orm-rt'):
63 self._apply_first_set_cookie_header(urlh, cookie)
32d687f5 64
a9e03736
S
65 _, urlh = self._download_webpage_handle(
66 auth.get('redirect_uri') or next_uri, None, 'Completing login',)
32d687f5 67
a9e03736
S
68 if is_logged(urlh):
69 self.LOGGED_IN = True
70 return
32d687f5 71
a9e03736 72 raise ExtractorError('Unable to log in')
e9c8999e 73
32d687f5 74
75class SafariIE(SafariBaseIE):
76 IE_NAME = 'safari'
77 IE_DESC = 'safaribooksonline.com online video'
003fe73c
S
78 _VALID_URL = r'''(?x)
79 https?://
a9e03736 80 (?:www\.)?(?:safaribooksonline|(?:learning\.)?oreilly)\.com/
003fe73c
S
81 (?:
82 library/view/[^/]+/(?P<course_id>[^/]+)/(?P<part>[^/?\#&]+)\.html|
83 videos/[^/]+/[^/]+/(?P<reference_id>[^-]+-[^/?\#&]+)
84 )
85 '''
31c48098
S
86
87 _TESTS = [{
88 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/part00.html',
bcb668de 89 'md5': 'dcc5a425e79f2564148652616af1f2a3',
32d687f5 90 'info_dict': {
bcb668de 91 'id': '0_qbqx90ic',
32d687f5 92 'ext': 'mp4',
bcb668de 93 'title': 'Introduction to Hadoop Fundamentals LiveLessons',
94 'timestamp': 1437758058,
95 'upload_date': '20150724',
96 'uploader_id': 'stork',
31c48098 97 },
4fd35ee0
S
98 }, {
99 # non-digits in course id
100 'url': 'https://www.safaribooksonline.com/library/view/create-a-nodejs/100000006A0210/part00.html',
101 'only_matching': True,
697655a7
S
102 }, {
103 'url': 'https://www.safaribooksonline.com/library/view/learning-path-red/9780134664057/RHCE_Introduction.html',
104 'only_matching': True,
003fe73c
S
105 }, {
106 'url': 'https://www.safaribooksonline.com/videos/python-programming-language/9780134217314/9780134217314-PYMC_13_00',
107 'only_matching': True,
7f41a598
S
108 }, {
109 'url': 'https://learning.oreilly.com/videos/hadoop-fundamentals-livelessons/9780133392838/9780133392838-00_SeriesIntro',
110 'only_matching': True,
a9e03736
S
111 }, {
112 'url': 'https://www.oreilly.com/library/view/hadoop-fundamentals-livelessons/9780133392838/00_SeriesIntro.html',
113 'only_matching': True,
31c48098 114 }]
32d687f5 115
003fe73c
S
116 _PARTNER_ID = '1926081'
117 _UICONF_ID = '29375172'
118
32d687f5 119 def _real_extract(self, url):
5ad28e7f 120 mobj = self._match_valid_url(url)
003fe73c
S
121
122 reference_id = mobj.group('reference_id')
123 if reference_id:
124 video_id = reference_id
125 partner_id = self._PARTNER_ID
126 ui_id = self._UICONF_ID
127 else:
128 video_id = '%s-%s' % (mobj.group('course_id'), mobj.group('part'))
129
130 webpage, urlh = self._download_webpage_handle(url, video_id)
131
3d2623a8 132 mobj = re.match(self._VALID_URL, urlh.url)
003fe73c
S
133 reference_id = mobj.group('reference_id')
134 if not reference_id:
135 reference_id = self._search_regex(
136 r'data-reference-id=(["\'])(?P<id>(?:(?!\1).)+)\1',
137 webpage, 'kaltura reference id', group='id')
138 partner_id = self._search_regex(
139 r'data-partner-id=(["\'])(?P<id>(?:(?!\1).)+)\1',
140 webpage, 'kaltura widget id', default=self._PARTNER_ID,
141 group='id')
142 ui_id = self._search_regex(
143 r'data-ui-id=(["\'])(?P<id>(?:(?!\1).)+)\1',
144 webpage, 'kaltura uiconf id', default=self._UICONF_ID,
145 group='id')
31c48098 146
73cbd709 147 query = {
bcb668de 148 'wid': '_%s' % partner_id,
149 'uiconf_id': ui_id,
150 'flashvars[referenceId]': reference_id,
73cbd709
S
151 }
152
153 if self.LOGGED_IN:
154 kaltura_session = self._download_json(
155 '%s/player/kaltura_session/?reference_id=%s' % (self._API_BASE, reference_id),
3aec7176 156 video_id, 'Downloading kaltura session JSON',
3fdf5731 157 'Unable to download kaltura session JSON', fatal=False,
158 headers={'Accept': 'application/json'})
73cbd709
S
159 if kaltura_session:
160 session = kaltura_session.get('session')
161 if session:
162 query['flashvars[ks]'] = session
163
164 return self.url_result(update_url_query(
165 'https://cdnapisec.kaltura.com/html5/html5lib/v2.37.1/mwEmbedFrame.php', query),
166 'Kaltura')
32d687f5 167
168
3aec7176
S
169class SafariApiIE(SafariBaseIE):
170 IE_NAME = 'safari:api'
a9e03736 171 _VALID_URL = r'https?://(?:www\.)?(?:safaribooksonline|(?:learning\.)?oreilly)\.com/api/v1/book/(?P<course_id>[^/]+)/chapter(?:-content)?/(?P<part>[^/?#&]+)\.html'
3aec7176 172
697655a7 173 _TESTS = [{
3aec7176
S
174 'url': 'https://www.safaribooksonline.com/api/v1/book/9780133392838/chapter/part00.html',
175 'only_matching': True,
697655a7
S
176 }, {
177 'url': 'https://www.safaribooksonline.com/api/v1/book/9780134664057/chapter/RHCE_Introduction.html',
178 'only_matching': True,
179 }]
3aec7176
S
180
181 def _real_extract(self, url):
5ad28e7f 182 mobj = self._match_valid_url(url)
3aec7176
S
183 part = self._download_json(
184 url, '%s/%s' % (mobj.group('course_id'), mobj.group('part')),
185 'Downloading part JSON')
7738bd32
MKA
186 web_url = part['web_url']
187 if 'library/view' in web_url:
188 web_url = web_url.replace('library/view', 'videos')
189 natural_keys = part['natural_key']
9c1c3ec0 190 web_url = f'{web_url.rsplit("/", 1)[0]}/{natural_keys[0]}-{natural_keys[1][:-5]}'
7738bd32 191 return self.url_result(web_url, SafariIE.ie_key())
3aec7176
S
192
193
32d687f5 194class SafariCourseIE(SafariBaseIE):
195 IE_NAME = 'safari:course'
196 IE_DESC = 'safaribooksonline.com online courses'
197
a26b174c
S
198 _VALID_URL = r'''(?x)
199 https?://
200 (?:
a9e03736 201 (?:www\.)?(?:safaribooksonline|(?:learning\.)?oreilly)\.com/
003fe73c
S
202 (?:
203 library/view/[^/]+|
204 api/v1/book|
205 videos/[^/]+
206 )|
a26b174c
S
207 techbus\.safaribooksonline\.com
208 )
003fe73c 209 /(?P<id>[^/]+)
a26b174c 210 '''
32d687f5 211
31c48098
S
212 _TESTS = [{
213 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
214 'info_dict': {
215 'id': '9780133392838',
216 'title': 'Hadoop Fundamentals LiveLessons',
217 },
218 'playlist_count': 22,
219 'skip': 'Requires safaribooksonline account credentials',
220 }, {
221 'url': 'https://www.safaribooksonline.com/api/v1/book/9781449396459/?override_format=json',
222 'only_matching': True,
a26b174c
S
223 }, {
224 'url': 'http://techbus.safaribooksonline.com/9780134426365',
225 'only_matching': True,
003fe73c
S
226 }, {
227 'url': 'https://www.safaribooksonline.com/videos/python-programming-language/9780134217314',
228 'only_matching': True,
7f41a598
S
229 }, {
230 'url': 'https://learning.oreilly.com/videos/hadoop-fundamentals-livelessons/9780133392838',
231 'only_matching': True,
a9e03736
S
232 }, {
233 'url': 'https://www.oreilly.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
234 'only_matching': True,
31c48098 235 }]
32d687f5 236
003fe73c
S
237 @classmethod
238 def suitable(cls, url):
239 return (False if SafariIE.suitable(url) or SafariApiIE.suitable(url)
240 else super(SafariCourseIE, cls).suitable(url))
241
32d687f5 242 def _real_extract(self, url):
31c48098 243 course_id = self._match_id(url)
32d687f5 244
31c48098 245 course_json = self._download_json(
73cbd709 246 '%s/book/%s/?override_format=%s' % (self._API_BASE, course_id, self._API_FORMAT),
31c48098 247 course_id, 'Downloading course JSON')
32d687f5 248
249 if 'chapters' not in course_json:
31c48098
S
250 raise ExtractorError(
251 'No chapters found for course %s' % course_id, expected=True)
32d687f5 252
253 entries = [
3aec7176 254 self.url_result(chapter, SafariApiIE.ie_key())
31c48098 255 for chapter in course_json['chapters']]
32d687f5 256
257 course_title = course_json['title']
258
259 return self.playlist_result(entries, course_id, course_title)