2 from __future__ import unicode_literals
6 from .common import InfoExtractor
7 from .brightcove import BrightcoveLegacyIE
18 class SafariBaseIE(InfoExtractor):
19 _LOGIN_URL = 'https://www.safaribooksonline.com/accounts/login/'
20 _SUCCESSFUL_LOGIN_REGEX = r'<a href="/accounts/logout/"[^>]*>Sign Out</a>'
21 _NETRC_MACHINE = 'safari'
23 _API_BASE = 'https://www.safaribooksonline.com/api/v1/book'
28 def _real_initialize(self):
29 # We only need to log in once for courses or individual videos
30 if not self.LOGGED_IN:
32 SafariBaseIE.LOGGED_IN = True
35 (username, password) = self._get_login_info()
37 self.raise_login_required('safaribooksonline.com account is required')
39 headers = std_headers.copy()
40 if 'Referer' not in headers:
41 headers['Referer'] = self._LOGIN_URL
42 login_page_request = sanitized_Request(self._LOGIN_URL, headers=headers)
44 login_page = self._download_webpage(
45 login_page_request, None,
46 'Downloading login form')
48 csrf = self._html_search_regex(
49 r"name='csrfmiddlewaretoken'\s+value='([^']+)'",
50 login_page, 'csrf token')
53 'csrfmiddlewaretoken': csrf,
55 'password1': password,
60 request = sanitized_Request(
61 self._LOGIN_URL, urlencode_postdata(login_form), headers=headers)
62 login_page = self._download_webpage(
63 request, None, 'Logging in as %s' % username)
65 if re.search(self._SUCCESSFUL_LOGIN_REGEX, login_page) is None:
67 'Login failed; make sure your credentials are correct and try again.',
70 self.to_screen('Login successful')
73 class SafariIE(SafariBaseIE):
75 IE_DESC = 'safaribooksonline.com online video'
76 _VALID_URL = r'''(?x)https?://
77 (?:www\.)?safaribooksonline\.com/
83 (?:chapter(?:-content)?/)?
84 (?P<part>part\d+)\.html
88 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/part00.html',
89 'md5': '5b0c4cc1b3c1ba15dda7344085aa5592',
91 'id': '2842601850001',
93 'title': 'Introduction',
95 'skip': 'Requires safaribooksonline account credentials',
97 'url': 'https://www.safaribooksonline.com/api/v1/book/9780133392838/chapter/part00.html',
98 'only_matching': True,
100 # non-digits in course id
101 'url': 'https://www.safaribooksonline.com/library/view/create-a-nodejs/100000006A0210/part00.html',
102 'only_matching': True,
105 def _real_extract(self, url):
106 mobj = re.match(self._VALID_URL, url)
107 course_id = mobj.group('course_id')
108 part = mobj.group('part')
110 webpage = self._download_webpage(
111 '%s/%s/chapter-content/%s.html' % (self._API_BASE, course_id, part),
114 bc_url = BrightcoveLegacyIE._extract_brightcove_url(webpage)
116 raise ExtractorError('Could not extract Brightcove URL from %s' % url, expected=True)
118 return self.url_result(smuggle_url(bc_url, {'Referer': url}), 'BrightcoveLegacy')
121 class SafariCourseIE(SafariBaseIE):
122 IE_NAME = 'safari:course'
123 IE_DESC = 'safaribooksonline.com online courses'
125 _VALID_URL = r'https?://(?:www\.)?safaribooksonline\.com/(?:library/view/[^/]+|api/v1/book)/(?P<id>[^/]+)/?(?:[#?]|$)'
128 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
130 'id': '9780133392838',
131 'title': 'Hadoop Fundamentals LiveLessons',
133 'playlist_count': 22,
134 'skip': 'Requires safaribooksonline account credentials',
136 'url': 'https://www.safaribooksonline.com/api/v1/book/9781449396459/?override_format=json',
137 'only_matching': True,
140 def _real_extract(self, url):
141 course_id = self._match_id(url)
143 course_json = self._download_json(
144 '%s/%s/?override_format=%s' % (self._API_BASE, course_id, self._API_FORMAT),
145 course_id, 'Downloading course JSON')
147 if 'chapters' not in course_json:
148 raise ExtractorError(
149 'No chapters found for course %s' % course_id, expected=True)
152 self.url_result(chapter, 'Safari')
153 for chapter in course_json['chapters']]
155 course_title = course_json['title']
157 return self.playlist_result(entries, course_id, course_title)