You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

259 lines
8.6 KiB

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