]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/teachable.py
[ChangeLog] Actualize
[youtube-dl.git] / youtube_dl / extractor / teachable.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from .wistia import WistiaIE
7 from ..utils import (
8     clean_html,
9     ExtractorError,
10     get_element_by_class,
11     urlencode_postdata,
12     urljoin,
13 )
14
15
16 class TeachableBaseIE(InfoExtractor):
17     _NETRC_MACHINE = 'teachable'
18     _URL_PREFIX = 'teachable:'
19
20     _SITES = {
21         # Only notable ones here
22         'upskillcourses.com': 'upskill',
23         'academy.gns3.com': 'gns3',
24         'academyhacker.com': 'academyhacker',
25         'stackskills.com': 'stackskills',
26         'market.saleshacker.com': 'saleshacker',
27         'learnability.org': 'learnability',
28         'edurila.com': 'edurila',
29         'courses.workitdaily.com': 'workitdaily',
30     }
31
32     _VALID_URL_SUB_TUPLE = (_URL_PREFIX, '|'.join(re.escape(site) for site in _SITES.keys()))
33
34     def _real_initialize(self):
35         self._logged_in = False
36
37     def _login(self, site):
38         if self._logged_in:
39             return
40
41         username, password = self._get_login_info(
42             netrc_machine=self._SITES.get(site, site))
43         if username is None:
44             return
45
46         login_page, urlh = self._download_webpage_handle(
47             'https://%s/sign_in' % site, None,
48             'Downloading %s login page' % site)
49
50         def is_logged(webpage):
51             return any(re.search(p, webpage) for p in (
52                 r'class=["\']user-signout',
53                 r'<a[^>]+\bhref=["\']/sign_out',
54                 r'Log\s+[Oo]ut\s*<'))
55
56         if is_logged(login_page):
57             self._logged_in = True
58             return
59
60         login_url = urlh.geturl()
61
62         login_form = self._hidden_inputs(login_page)
63
64         login_form.update({
65             'user[email]': username,
66             'user[password]': password,
67         })
68
69         post_url = self._search_regex(
70             r'<form[^>]+action=(["\'])(?P<url>(?:(?!\1).)+)\1', login_page,
71             'post url', default=login_url, group='url')
72
73         if not post_url.startswith('http'):
74             post_url = urljoin(login_url, post_url)
75
76         response = self._download_webpage(
77             post_url, None, 'Logging in to %s' % site,
78             data=urlencode_postdata(login_form),
79             headers={
80                 'Content-Type': 'application/x-www-form-urlencoded',
81                 'Referer': login_url,
82             })
83
84         if '>I accept the new Privacy Policy<' in response:
85             raise ExtractorError(
86                 'Unable to login: %s asks you to accept new Privacy Policy. '
87                 'Go to https://%s/ and accept.' % (site, site), expected=True)
88
89         # Successful login
90         if is_logged(response):
91             self._logged_in = True
92             return
93
94         message = get_element_by_class('alert', response)
95         if message is not None:
96             raise ExtractorError(
97                 'Unable to login: %s' % clean_html(message), expected=True)
98
99         raise ExtractorError('Unable to log in')
100
101
102 class TeachableIE(TeachableBaseIE):
103     _VALID_URL = r'''(?x)
104                     (?:
105                         %shttps?://(?P<site_t>[^/]+)|
106                         https?://(?:www\.)?(?P<site>%s)
107                     )
108                     /courses/[^/]+/lectures/(?P<id>\d+)
109                     ''' % TeachableBaseIE._VALID_URL_SUB_TUPLE
110
111     _TESTS = [{
112         'url': 'http://upskillcourses.com/courses/essential-web-developer-course/lectures/1747100',
113         'info_dict': {
114             'id': 'uzw6zw58or',
115             'ext': 'mp4',
116             'title': 'Welcome to the Course!',
117             'description': 'md5:65edb0affa582974de4625b9cdea1107',
118             'duration': 138.763,
119             'timestamp': 1479846621,
120             'upload_date': '20161122',
121         },
122         'params': {
123             'skip_download': True,
124         },
125     }, {
126         'url': 'http://upskillcourses.com/courses/119763/lectures/1747100',
127         'only_matching': True,
128     }, {
129         'url': 'https://academy.gns3.com/courses/423415/lectures/6885939',
130         'only_matching': True,
131     }, {
132         'url': 'teachable:https://upskillcourses.com/courses/essential-web-developer-course/lectures/1747100',
133         'only_matching': True,
134     }]
135
136     @staticmethod
137     def _is_teachable(webpage):
138         return 'teachableTracker.linker:autoLink' in webpage and re.search(
139             r'<link[^>]+href=["\']https?://process\.fs\.teachablecdn\.com',
140             webpage)
141
142     @staticmethod
143     def _extract_url(webpage, source_url):
144         if not TeachableIE._is_teachable(webpage):
145             return
146         if re.match(r'https?://[^/]+/(?:courses|p)', source_url):
147             return '%s%s' % (TeachableBaseIE._URL_PREFIX, source_url)
148
149     def _real_extract(self, url):
150         mobj = re.match(self._VALID_URL, url)
151         site = mobj.group('site') or mobj.group('site_t')
152         video_id = mobj.group('id')
153
154         self._login(site)
155
156         prefixed = url.startswith(self._URL_PREFIX)
157         if prefixed:
158             url = url[len(self._URL_PREFIX):]
159
160         webpage = self._download_webpage(url, video_id)
161
162         wistia_urls = WistiaIE._extract_urls(webpage)
163         if not wistia_urls:
164             if any(re.search(p, webpage) for p in (
165                     r'class=["\']lecture-contents-locked',
166                     r'>\s*Lecture contents locked',
167                     r'id=["\']lecture-locked',
168                     # https://academy.tailoredtutors.co.uk/courses/108779/lectures/1955313
169                     r'class=["\'](?:inner-)?lesson-locked',
170                     r'>LESSON LOCKED<')):
171                 self.raise_login_required('Lecture contents locked')
172             raise ExtractorError('Unable to find video URL')
173
174         title = self._og_search_title(webpage, default=None)
175
176         entries = [{
177             '_type': 'url_transparent',
178             'url': wistia_url,
179             'ie_key': WistiaIE.ie_key(),
180             'title': title,
181         } for wistia_url in wistia_urls]
182
183         return self.playlist_result(entries, video_id, title)
184
185
186 class TeachableCourseIE(TeachableBaseIE):
187     _VALID_URL = r'''(?x)
188                         (?:
189                             %shttps?://(?P<site_t>[^/]+)|
190                             https?://(?:www\.)?(?P<site>%s)
191                         )
192                         /(?:courses|p)/(?:enrolled/)?(?P<id>[^/?#&]+)
193                     ''' % TeachableBaseIE._VALID_URL_SUB_TUPLE
194     _TESTS = [{
195         'url': 'http://upskillcourses.com/courses/essential-web-developer-course/',
196         'info_dict': {
197             'id': 'essential-web-developer-course',
198             'title': 'The Essential Web Developer Course (Free)',
199         },
200         'playlist_count': 192,
201     }, {
202         'url': 'http://upskillcourses.com/courses/119763/',
203         'only_matching': True,
204     }, {
205         'url': 'http://upskillcourses.com/courses/enrolled/119763',
206         'only_matching': True,
207     }, {
208         'url': 'https://academy.gns3.com/courses/enrolled/423415',
209         'only_matching': True,
210     }, {
211         'url': 'teachable:https://learn.vrdev.school/p/gear-vr-developer-mini',
212         'only_matching': True,
213     }, {
214         'url': 'teachable:https://filmsimplified.com/p/davinci-resolve-15-crash-course',
215         'only_matching': True,
216     }]
217
218     @classmethod
219     def suitable(cls, url):
220         return False if TeachableIE.suitable(url) else super(
221             TeachableCourseIE, cls).suitable(url)
222
223     def _real_extract(self, url):
224         mobj = re.match(self._VALID_URL, url)
225         site = mobj.group('site') or mobj.group('site_t')
226         course_id = mobj.group('id')
227
228         self._login(site)
229
230         prefixed = url.startswith(self._URL_PREFIX)
231         if prefixed:
232             prefix = self._URL_PREFIX
233             url = url[len(prefix):]
234
235         webpage = self._download_webpage(url, course_id)
236
237         url_base = 'https://%s/' % site
238
239         entries = []
240
241         for mobj in re.finditer(
242                 r'(?s)(?P<li><li[^>]+class=(["\'])(?:(?!\2).)*?section-item[^>]+>.+?</li>)',
243                 webpage):
244             li = mobj.group('li')
245             if 'fa-youtube-play' not in li:
246                 continue
247             lecture_url = self._search_regex(
248                 r'<a[^>]+href=(["\'])(?P<url>(?:(?!\1).)+)\1', li,
249                 'lecture url', default=None, group='url')
250             if not lecture_url:
251                 continue
252             lecture_id = self._search_regex(
253                 r'/lectures/(\d+)', lecture_url, 'lecture id', default=None)
254             title = self._html_search_regex(
255                 r'<span[^>]+class=["\']lecture-name[^>]+>([^<]+)', li,
256                 'title', default=None)
257             entry_url = urljoin(url_base, lecture_url)
258             if prefixed:
259                 entry_url = self._URL_PREFIX + entry_url
260             entries.append(
261                 self.url_result(
262                     entry_url,
263                     ie=TeachableIE.ie_key(), video_id=lecture_id,
264                     video_title=clean_html(title)))
265
266         course_title = self._html_search_regex(
267             (r'(?s)<img[^>]+class=["\']course-image[^>]+>\s*<h\d>(.+?)</h',
268              r'(?s)<h\d[^>]+class=["\']course-title[^>]+>(.+?)</h'),
269             webpage, 'course title', fatal=False)
270
271         return self.playlist_result(entries, course_id, course_title)