]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/safari.py
a602af6928d2a9d054fc8670342a6ddf7d9ef4da
[youtubedl] / youtube_dl / extractor / safari.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from .brightcove import BrightcoveIE
8
9 from ..compat import (
10 compat_urllib_parse,
11 compat_urllib_request,
12 )
13 from ..utils import (
14 ExtractorError,
15 smuggle_url,
16 std_headers,
17 )
18
19
20 class SafariBaseIE(InfoExtractor):
21 _LOGIN_URL = 'https://www.safaribooksonline.com/accounts/login/'
22 _SUCCESSFUL_LOGIN_REGEX = r'<a href="/accounts/logout/"[^>]*>Sign Out</a>'
23 _NETRC_MACHINE = 'safari'
24
25 _API_BASE = 'https://www.safaribooksonline.com/api/v1/book'
26 _API_FORMAT = 'json'
27
28 LOGGED_IN = False
29
30 def _real_initialize(self):
31 # We only need to log in once for courses or individual videos
32 if not self.LOGGED_IN:
33 self._login()
34 SafariBaseIE.LOGGED_IN = True
35
36 def _login(self):
37 (username, password) = self._get_login_info()
38 if username is None:
39 self.raise_login_required('safaribooksonline.com account is required')
40
41 headers = std_headers
42 if 'Referer' not in headers:
43 headers['Referer'] = self._LOGIN_URL
44
45 login_page = self._download_webpage(
46 self._LOGIN_URL, None,
47 'Downloading login form')
48
49 csrf = self._html_search_regex(
50 r"name='csrfmiddlewaretoken'\s+value='([^']+)'",
51 login_page, 'csrf token')
52
53 login_form = {
54 'csrfmiddlewaretoken': csrf,
55 'email': username,
56 'password1': password,
57 'login': 'Sign In',
58 'next': '',
59 }
60
61 request = compat_urllib_request.Request(
62 self._LOGIN_URL, compat_urllib_parse.urlencode(login_form), headers=headers)
63 login_page = self._download_webpage(
64 request, None, 'Logging in as %s' % username)
65
66 if re.search(self._SUCCESSFUL_LOGIN_REGEX, login_page) is None:
67 raise ExtractorError(
68 'Login failed; make sure your credentials are correct and try again.',
69 expected=True)
70
71 self.to_screen('Login successful')
72
73
74 class SafariIE(SafariBaseIE):
75 IE_NAME = 'safari'
76 IE_DESC = 'safaribooksonline.com online video'
77 _VALID_URL = r'''(?x)https?://
78 (?:www\.)?safaribooksonline\.com/
79 (?:
80 library/view/[^/]+|
81 api/v1/book
82 )/
83 (?P<course_id>[^/]+)/
84 (?:chapter(?:-content)?/)?
85 (?P<part>part\d+)\.html
86 '''
87
88 _TESTS = [{
89 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/part00.html',
90 'md5': '5b0c4cc1b3c1ba15dda7344085aa5592',
91 'info_dict': {
92 'id': '2842601850001',
93 'ext': 'mp4',
94 'title': 'Introduction',
95 },
96 'skip': 'Requires safaribooksonline account credentials',
97 }, {
98 'url': 'https://www.safaribooksonline.com/api/v1/book/9780133392838/chapter/part00.html',
99 'only_matching': True,
100 }, {
101 # non-digits in course id
102 'url': 'https://www.safaribooksonline.com/library/view/create-a-nodejs/100000006A0210/part00.html',
103 'only_matching': True,
104 }]
105
106 def _real_extract(self, url):
107 mobj = re.match(self._VALID_URL, url)
108 course_id = mobj.group('course_id')
109 part = mobj.group('part')
110
111 webpage = self._download_webpage(
112 '%s/%s/chapter-content/%s.html' % (self._API_BASE, course_id, part),
113 part)
114
115 bc_url = BrightcoveIE._extract_brightcove_url(webpage)
116 if not bc_url:
117 raise ExtractorError('Could not extract Brightcove URL from %s' % url, expected=True)
118
119 return self.url_result(smuggle_url(bc_url, {'Referer': url}), 'Brightcove')
120
121
122 class SafariCourseIE(SafariBaseIE):
123 IE_NAME = 'safari:course'
124 IE_DESC = 'safaribooksonline.com online courses'
125
126 _VALID_URL = r'https?://(?:www\.)?safaribooksonline\.com/(?:library/view/[^/]+|api/v1/book)/(?P<id>[^/]+)/?(?:[#?]|$)'
127
128 _TESTS = [{
129 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
130 'info_dict': {
131 'id': '9780133392838',
132 'title': 'Hadoop Fundamentals LiveLessons',
133 },
134 'playlist_count': 22,
135 'skip': 'Requires safaribooksonline account credentials',
136 }, {
137 'url': 'https://www.safaribooksonline.com/api/v1/book/9781449396459/?override_format=json',
138 'only_matching': True,
139 }]
140
141 def _real_extract(self, url):
142 course_id = self._match_id(url)
143
144 course_json = self._download_json(
145 '%s/%s/?override_format=%s' % (self._API_BASE, course_id, self._API_FORMAT),
146 course_id, 'Downloading course JSON')
147
148 if 'chapters' not in course_json:
149 raise ExtractorError(
150 'No chapters found for course %s' % course_id, expected=True)
151
152 entries = [
153 self.url_result(chapter, 'Safari')
154 for chapter in course_json['chapters']]
155
156 course_title = course_json['title']
157
158 return self.playlist_result(entries, course_id, course_title)