Merge pull request #8766 from yan12125/dash-detect-ext
[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 )
16
17
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'
22
23     _API_BASE = 'https://www.safaribooksonline.com/api/v1/book'
24     _API_FORMAT = 'json'
25
26     LOGGED_IN = False
27
28     def _real_initialize(self):
29         # We only need to log in once for courses or individual videos
30         if not self.LOGGED_IN:
31             self._login()
32             SafariBaseIE.LOGGED_IN = True
33
34     def _login(self):
35         (username, password) = self._get_login_info()
36         if username is None:
37             self.raise_login_required('safaribooksonline.com account is required')
38
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)
43
44         login_page = self._download_webpage(
45             login_page_request, None,
46             'Downloading login form')
47
48         csrf = self._html_search_regex(
49             r"name='csrfmiddlewaretoken'\s+value='([^']+)'",
50             login_page, 'csrf token')
51
52         login_form = {
53             'csrfmiddlewaretoken': csrf,
54             'email': username,
55             'password1': password,
56             'login': 'Sign In',
57             'next': '',
58         }
59
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)
64
65         if re.search(self._SUCCESSFUL_LOGIN_REGEX, login_page) is None:
66             raise ExtractorError(
67                 'Login failed; make sure your credentials are correct and try again.',
68                 expected=True)
69
70         self.to_screen('Login successful')
71
72
73 class SafariIE(SafariBaseIE):
74     IE_NAME = 'safari'
75     IE_DESC = 'safaribooksonline.com online video'
76     _VALID_URL = r'''(?x)https?://
77                             (?:www\.)?safaribooksonline\.com/
78                                 (?:
79                                     library/view/[^/]+|
80                                     api/v1/book
81                                 )/
82                                 (?P<course_id>[^/]+)/
83                                     (?:chapter(?:-content)?/)?
84                                 (?P<part>part\d+)\.html
85     '''
86
87     _TESTS = [{
88         'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/part00.html',
89         'md5': '5b0c4cc1b3c1ba15dda7344085aa5592',
90         'info_dict': {
91             'id': '2842601850001',
92             'ext': 'mp4',
93             'title': 'Introduction',
94         },
95         'skip': 'Requires safaribooksonline account credentials',
96     }, {
97         'url': 'https://www.safaribooksonline.com/api/v1/book/9780133392838/chapter/part00.html',
98         'only_matching': True,
99     }, {
100         # non-digits in course id
101         'url': 'https://www.safaribooksonline.com/library/view/create-a-nodejs/100000006A0210/part00.html',
102         'only_matching': True,
103     }]
104
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')
109
110         webpage = self._download_webpage(
111             '%s/%s/chapter-content/%s.html' % (self._API_BASE, course_id, part),
112             part)
113
114         bc_url = BrightcoveLegacyIE._extract_brightcove_url(webpage)
115         if not bc_url:
116             raise ExtractorError('Could not extract Brightcove URL from %s' % url, expected=True)
117
118         return self.url_result(smuggle_url(bc_url, {'Referer': url}), 'BrightcoveLegacy')
119
120
121 class SafariCourseIE(SafariBaseIE):
122     IE_NAME = 'safari:course'
123     IE_DESC = 'safaribooksonline.com online courses'
124
125     _VALID_URL = r'https?://(?:www\.)?safaribooksonline\.com/(?:library/view/[^/]+|api/v1/book)/(?P<id>[^/]+)/?(?:[#?]|$)'
126
127     _TESTS = [{
128         'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
129         'info_dict': {
130             'id': '9780133392838',
131             'title': 'Hadoop Fundamentals LiveLessons',
132         },
133         'playlist_count': 22,
134         'skip': 'Requires safaribooksonline account credentials',
135     }, {
136         'url': 'https://www.safaribooksonline.com/api/v1/book/9781449396459/?override_format=json',
137         'only_matching': True,
138     }]
139
140     def _real_extract(self, url):
141         course_id = self._match_id(url)
142
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')
146
147         if 'chapters' not in course_json:
148             raise ExtractorError(
149                 'No chapters found for course %s' % course_id, expected=True)
150
151         entries = [
152             self.url_result(chapter, 'Safari')
153             for chapter in course_json['chapters']]
154
155         course_title = course_json['title']
156
157         return self.playlist_result(entries, course_id, course_title)