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.

176 lines
5.7 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 UpskillBaseIE(InfoExtractor):
  14. _LOGIN_URL = 'http://upskillcourses.com/sign_in'
  15. _NETRC_MACHINE = 'upskill'
  16. def _real_initialize(self):
  17. self._login()
  18. def _login(self):
  19. username, password = self._get_login_info()
  20. if username is None:
  21. return
  22. login_page, urlh = self._download_webpage_handle(
  23. self._LOGIN_URL, None, 'Downloading login page')
  24. login_url = compat_str(urlh.geturl())
  25. login_form = self._hidden_inputs(login_page)
  26. login_form.update({
  27. 'user[email]': username,
  28. 'user[password]': password,
  29. })
  30. post_url = self._search_regex(
  31. r'<form[^>]+action=(["\'])(?P<url>(?:(?!\1).)+)\1', login_page,
  32. 'post url', default=login_url, group='url')
  33. if not post_url.startswith('http'):
  34. post_url = urljoin(login_url, post_url)
  35. response = self._download_webpage(
  36. post_url, None, 'Logging in',
  37. data=urlencode_postdata(login_form),
  38. headers={
  39. 'Content-Type': 'application/x-www-form-urlencoded',
  40. 'Referer': login_url,
  41. })
  42. # Successful login
  43. if any(re.search(p, response) for p in (
  44. r'class=["\']user-signout',
  45. r'<a[^>]+\bhref=["\']/sign_out',
  46. r'>\s*Log out\s*<')):
  47. return
  48. message = get_element_by_class('alert', response)
  49. if message is not None:
  50. raise ExtractorError(
  51. 'Unable to login: %s' % clean_html(message), expected=True)
  52. raise ExtractorError('Unable to log in')
  53. class UpskillIE(UpskillBaseIE):
  54. _VALID_URL = r'https?://(?:www\.)?upskillcourses\.com/courses/[^/]+/lectures/(?P<id>\d+)'
  55. _TESTS = [{
  56. 'url': 'http://upskillcourses.com/courses/essential-web-developer-course/lectures/1747100',
  57. 'info_dict': {
  58. 'id': 'uzw6zw58or',
  59. 'ext': 'mp4',
  60. 'title': 'Welcome to the Course!',
  61. 'description': 'md5:8d66c13403783370af62ca97a7357bdd',
  62. 'duration': 138.763,
  63. 'timestamp': 1479846621,
  64. 'upload_date': '20161122',
  65. },
  66. 'params': {
  67. 'skip_download': True,
  68. },
  69. }, {
  70. 'url': 'http://upskillcourses.com/courses/119763/lectures/1747100',
  71. 'only_matching': True,
  72. }]
  73. def _real_extract(self, url):
  74. video_id = self._match_id(url)
  75. webpage = self._download_webpage(url, video_id)
  76. wistia_url = WistiaIE._extract_url(webpage)
  77. if not wistia_url:
  78. if any(re.search(p, webpage) for p in (
  79. r'class=["\']lecture-contents-locked',
  80. r'>\s*Lecture contents locked',
  81. r'id=["\']lecture-locked')):
  82. self.raise_login_required('Lecture contents locked')
  83. title = self._og_search_title(webpage, default=None)
  84. return {
  85. '_type': 'url_transparent',
  86. 'url': wistia_url,
  87. 'ie_key': WistiaIE.ie_key(),
  88. 'title': title,
  89. }
  90. class UpskillCourseIE(UpskillBaseIE):
  91. _VALID_URL = r'https?://(?:www\.)?upskillcourses\.com/courses/(?:enrolled/)?(?P<id>[^/?#&]+)'
  92. _TESTS = [{
  93. 'url': 'http://upskillcourses.com/courses/essential-web-developer-course/',
  94. 'info_dict': {
  95. 'id': '119763',
  96. 'title': 'The Essential Web Developer Course (Free)',
  97. },
  98. 'playlist_count': 192,
  99. }, {
  100. 'url': 'http://upskillcourses.com/courses/119763/',
  101. 'only_matching': True,
  102. }, {
  103. 'url': 'http://upskillcourses.com/courses/enrolled/119763',
  104. 'only_matching': True,
  105. }]
  106. @classmethod
  107. def suitable(cls, url):
  108. return False if UpskillIE.suitable(url) else super(
  109. UpskillCourseIE, cls).suitable(url)
  110. def _real_extract(self, url):
  111. course_id = self._match_id(url)
  112. webpage = self._download_webpage(url, course_id)
  113. course_id = self._search_regex(
  114. r'data-course-id=["\'](\d+)', webpage, 'course id',
  115. default=course_id)
  116. entries = []
  117. for mobj in re.finditer(
  118. r'(?s)(?P<li><li[^>]+class=(["\'])(?:(?!\2).)*?section-item[^>]+>.+?</li>)',
  119. webpage):
  120. li = mobj.group('li')
  121. if 'fa-youtube-play' not in li:
  122. continue
  123. lecture_url = self._search_regex(
  124. r'<a[^>]+href=(["\'])(?P<url>(?:(?!\1).)+)\1', li,
  125. 'lecture url', default=None, group='url')
  126. if not lecture_url:
  127. continue
  128. lecture_id = self._search_regex(
  129. r'/lectures/(\d+)', lecture_url, 'lecture id', default=None)
  130. title = self._html_search_regex(
  131. r'<span[^>]+class=["\']lecture-name[^>]+>([^<]+)', li,
  132. 'title', default=None)
  133. entries.append(
  134. self.url_result(
  135. urljoin('http://upskillcourses.com/', lecture_url),
  136. ie=UpskillIE.ie_key(), video_id=lecture_id,
  137. video_title=clean_html(title)))
  138. course_title = self._html_search_regex(
  139. (r'(?s)<img[^>]+class=["\']course-image[^>]+>\s*<h\d>(.+?)</h',
  140. r'(?s)<h\d[^>]+class=["\']course-title[^>]+>(.+?)</h'),
  141. webpage, 'course title', fatal=False)
  142. return self.playlist_result(entries, course_id, course_title)