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