teachable.py 10 KB

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