2 from __future__ import unicode_literals
6 from .common import InfoExtractor
7 from .brightcove import BrightcoveLegacyIE
19 class SafariBaseIE(InfoExtractor):
20 _LOGIN_URL = 'https://www.safaribooksonline.com/accounts/login/'
21 _SUCCESSFUL_LOGIN_REGEX = r'<a href="/accounts/logout/"[^>]*>Sign Out</a>'
22 _NETRC_MACHINE = 'safari'
24 _API_BASE = 'https://www.safaribooksonline.com/api/v1/book'
29 def _real_initialize(self):
31 # We only need to log in once for courses or individual videos
32 if not self.LOGGED_IN:
34 SafariBaseIE.LOGGED_IN = True
37 (username, password) = self._get_login_info()
39 self.raise_login_required('safaribooksonline.com account is required')
41 headers = std_headers.copy()
42 if 'Referer' not in headers:
43 headers['Referer'] = self._LOGIN_URL
44 login_page_request = sanitized_Request(self._LOGIN_URL, headers=headers)
46 login_page = self._download_webpage(
47 login_page_request, None,
48 'Downloading login form')
50 csrf = self._html_search_regex(
51 r"name='csrfmiddlewaretoken'\s+value='([^']+)'",
52 login_page, 'csrf token')
55 'csrfmiddlewaretoken': csrf,
57 'password1': password,
62 request = sanitized_Request(
63 self._LOGIN_URL, urlencode_postdata(login_form), headers=headers)
64 login_page = self._download_webpage(
65 request, None, 'Logging in as %s' % username)
67 if re.search(self._SUCCESSFUL_LOGIN_REGEX, login_page) is None:
69 'Login failed; make sure your credentials are correct and try again.',
72 self.to_screen('Login successful')
75 class SafariIE(SafariBaseIE):
77 IE_DESC = 'safaribooksonline.com online video'
78 _VALID_URL = r'''(?x)https?://
79 (?:www\.)?safaribooksonline\.com/
85 (?:chapter(?:-content)?/)?
86 (?P<part>part\d+)\.html
90 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/part00.html',
91 'md5': 'dcc5a425e79f2564148652616af1f2a3',
95 'title': 'Introduction to Hadoop Fundamentals LiveLessons',
96 'timestamp': 1437758058,
97 'upload_date': '20150724',
98 'uploader_id': 'stork',
101 'url': 'https://www.safaribooksonline.com/api/v1/book/9780133392838/chapter/part00.html',
102 'only_matching': True,
104 # non-digits in course id
105 'url': 'https://www.safaribooksonline.com/library/view/create-a-nodejs/100000006A0210/part00.html',
106 'only_matching': True,
109 def _real_extract(self, url):
110 mobj = re.match(self._VALID_URL, url)
111 course_id = mobj.group('course_id')
112 part = mobj.group('part')
114 webpage = self._download_webpage(url, '%s/%s' % (course_id, part))
115 reference_id = self._search_regex(r'data-reference-id="([^"]+)"', webpage, 'kaltura reference id')
116 partner_id = self._search_regex(r'data-partner-id="([^"]+)"', webpage, 'kaltura widget id')
117 ui_id = self._search_regex(r'data-ui-id="([^"]+)"', webpage, 'kaltura uiconf id')
119 return self.url_result(update_url_query('https://cdnapisec.kaltura.com/html5/html5lib/v2.37.1/mwEmbedFrame.php', {
120 'wid': '_%s' % partner_id,
122 'flashvars[referenceId]': reference_id,
126 class SafariCourseIE(SafariBaseIE):
127 IE_NAME = 'safari:course'
128 IE_DESC = 'safaribooksonline.com online courses'
130 _VALID_URL = r'https?://(?:www\.)?safaribooksonline\.com/(?:library/view/[^/]+|api/v1/book)/(?P<id>[^/]+)/?(?:[#?]|$)'
133 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
135 'id': '9780133392838',
136 'title': 'Hadoop Fundamentals LiveLessons',
138 'playlist_count': 22,
139 'skip': 'Requires safaribooksonline account credentials',
141 'url': 'https://www.safaribooksonline.com/api/v1/book/9781449396459/?override_format=json',
142 'only_matching': True,
145 def _real_extract(self, url):
146 course_id = self._match_id(url)
148 course_json = self._download_json(
149 '%s/%s/?override_format=%s' % (self._API_BASE, course_id, self._API_FORMAT),
150 course_id, 'Downloading course JSON')
152 if 'chapters' not in course_json:
153 raise ExtractorError(
154 'No chapters found for course %s' % course_id, expected=True)
157 self.url_result(chapter, 'Safari')
158 for chapter in course_json['chapters']]
160 course_title = course_json['title']
162 return self.playlist_result(entries, course_id, course_title)