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