logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

teachable.py (10499B)


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