PageRenderTime 70ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/yt_dlp/extractor/teachable.py

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