[safari] extract free and preview videos(#7491)
[youtube-dl] / 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 BrightcoveLegacyIE
8
9 from ..utils import (
10     ExtractorError,
11     sanitized_Request,
12     smuggle_url,
13     std_headers,
14     urlencode_postdata,
15     update_url_query,
16 )
17
18
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'
23
24     _API_BASE = 'https://www.safaribooksonline.com/api/v1/book'
25     _API_FORMAT = 'json'
26
27     LOGGED_IN = False
28
29     def _real_initialize(self):
30         return
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.copy()
42         if 'Referer' not in headers:
43             headers['Referer'] = self._LOGIN_URL
44         login_page_request = sanitized_Request(self._LOGIN_URL, headers=headers)
45
46         login_page = self._download_webpage(
47             login_page_request, None,
48             'Downloading login form')
49
50         csrf = self._html_search_regex(
51             r"name='csrfmiddlewaretoken'\s+value='([^']+)'",
52             login_page, 'csrf token')
53
54         login_form = {
55             'csrfmiddlewaretoken': csrf,
56             'email': username,
57             'password1': password,
58             'login': 'Sign In',
59             'next': '',
60         }
61
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)
66
67         if re.search(self._SUCCESSFUL_LOGIN_REGEX, login_page) is None:
68             raise ExtractorError(
69                 'Login failed; make sure your credentials are correct and try again.',
70                 expected=True)
71
72         self.to_screen('Login successful')
73
74
75 class SafariIE(SafariBaseIE):
76     IE_NAME = 'safari'
77     IE_DESC = 'safaribooksonline.com online video'
78     _VALID_URL = r'''(?x)https?://
79                             (?:www\.)?safaribooksonline\.com/
80                                 (?:
81                                     library/view/[^/]+|
82                                     api/v1/book
83                                 )/
84                                 (?P<course_id>[^/]+)/
85                                     (?:chapter(?:-content)?/)?
86                                 (?P<part>part\d+)\.html
87     '''
88
89     _TESTS = [{
90         'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/part00.html',
91         'md5': 'dcc5a425e79f2564148652616af1f2a3',
92         'info_dict': {
93             'id': '0_qbqx90ic',
94             'ext': 'mp4',
95             'title': 'Introduction to Hadoop Fundamentals LiveLessons',
96             'timestamp': 1437758058,
97             'upload_date': '20150724',
98             'uploader_id': 'stork',
99         },
100     }, {
101         'url': 'https://www.safaribooksonline.com/api/v1/book/9780133392838/chapter/part00.html',
102         'only_matching': True,
103     }, {
104         # non-digits in course id
105         'url': 'https://www.safaribooksonline.com/library/view/create-a-nodejs/100000006A0210/part00.html',
106         'only_matching': True,
107     }]
108
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')
113
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')
118
119         return self.url_result(update_url_query('https://cdnapisec.kaltura.com/html5/html5lib/v2.37.1/mwEmbedFrame.php', {
120             'wid': '_%s' % partner_id,
121             'uiconf_id': ui_id,
122             'flashvars[referenceId]': reference_id,
123         }), 'Kaltura')
124
125
126 class SafariCourseIE(SafariBaseIE):
127     IE_NAME = 'safari:course'
128     IE_DESC = 'safaribooksonline.com online courses'
129
130     _VALID_URL = r'https?://(?:www\.)?safaribooksonline\.com/(?:library/view/[^/]+|api/v1/book)/(?P<id>[^/]+)/?(?:[#?]|$)'
131
132     _TESTS = [{
133         'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
134         'info_dict': {
135             'id': '9780133392838',
136             'title': 'Hadoop Fundamentals LiveLessons',
137         },
138         'playlist_count': 22,
139         'skip': 'Requires safaribooksonline account credentials',
140     }, {
141         'url': 'https://www.safaribooksonline.com/api/v1/book/9781449396459/?override_format=json',
142         'only_matching': True,
143     }]
144
145     def _real_extract(self, url):
146         course_id = self._match_id(url)
147
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')
151
152         if 'chapters' not in course_json:
153             raise ExtractorError(
154                 'No chapters found for course %s' % course_id, expected=True)
155
156         entries = [
157             self.url_result(chapter, 'Safari')
158             for chapter in course_json['chapters']]
159
160         course_title = course_json['title']
161
162         return self.playlist_result(entries, course_id, course_title)