alura.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_urlparse,
  7. )
  8. from ..utils import (
  9. urlencode_postdata,
  10. urljoin,
  11. int_or_none,
  12. clean_html,
  13. ExtractorError
  14. )
  15. class AluraIE(InfoExtractor):
  16. _VALID_URL = r'https?://(?:cursos\.)?alura\.com\.br/course/(?P<course_name>[^/]+)/task/(?P<id>\d+)'
  17. _LOGIN_URL = 'https://cursos.alura.com.br/loginForm?urlAfterLogin=/loginForm'
  18. _VIDEO_URL = 'https://cursos.alura.com.br/course/%s/task/%s/video'
  19. _NETRC_MACHINE = 'alura'
  20. _TESTS = [{
  21. 'url': 'https://cursos.alura.com.br/course/clojure-mutabilidade-com-atoms-e-refs/task/60095',
  22. 'info_dict': {
  23. 'id': '60095',
  24. 'ext': 'mp4',
  25. 'title': 'Referências, ref-set e alter'
  26. },
  27. 'skip': 'Requires alura account credentials'},
  28. {
  29. # URL without video
  30. 'url': 'https://cursos.alura.com.br/course/clojure-mutabilidade-com-atoms-e-refs/task/60098',
  31. 'only_matching': True},
  32. {
  33. 'url': 'https://cursos.alura.com.br/course/fundamentos-market-digital/task/55219',
  34. 'only_matching': True}
  35. ]
  36. def _real_extract(self, url):
  37. course, video_id = self._match_valid_url(url)
  38. video_url = self._VIDEO_URL % (course, video_id)
  39. video_dict = self._download_json(video_url, video_id, 'Searching for videos')
  40. if video_dict:
  41. webpage = self._download_webpage(url, video_id)
  42. video_title = clean_html(self._search_regex(
  43. r'<span[^>]+class=(["\'])task-body-header-title-text\1[^>]*>(?P<title>[^<]+)',
  44. webpage, 'title', group='title'))
  45. formats = []
  46. for video_obj in video_dict:
  47. video_url_m3u8 = video_obj.get('link')
  48. video_format = self._extract_m3u8_formats(
  49. video_url_m3u8, None, 'mp4', entry_protocol='m3u8_native',
  50. m3u8_id='hls', fatal=False)
  51. for f in video_format:
  52. m = re.search(r'^[\w \W]*-(?P<res>\w*).mp4[\W \w]*', f['url'])
  53. if m:
  54. if not f.get('height'):
  55. f['height'] = int('720' if m.group('res') == 'hd' else '480')
  56. formats.extend(video_format)
  57. self._sort_formats(formats)
  58. return {
  59. 'id': video_id,
  60. 'title': video_title,
  61. "formats": formats
  62. }
  63. def _perform_login(self, username, password):
  64. login_page = self._download_webpage(
  65. self._LOGIN_URL, None, 'Downloading login popup')
  66. def is_logged(webpage):
  67. return any(re.search(p, webpage) for p in (
  68. r'href=[\"|\']?/signout[\"|\']',
  69. r'>Logout<'))
  70. # already logged in
  71. if is_logged(login_page):
  72. return
  73. login_form = self._hidden_inputs(login_page)
  74. login_form.update({
  75. 'username': username,
  76. 'password': password,
  77. })
  78. post_url = self._search_regex(
  79. r'<form[^>]+class=["|\']signin-form["|\'] action=["|\'](?P<url>.+?)["|\']', login_page,
  80. 'post url', default=self._LOGIN_URL, group='url')
  81. if not post_url.startswith('http'):
  82. post_url = compat_urlparse.urljoin(self._LOGIN_URL, post_url)
  83. response = self._download_webpage(
  84. post_url, None, 'Logging in',
  85. data=urlencode_postdata(login_form),
  86. headers={'Content-Type': 'application/x-www-form-urlencoded'})
  87. if not is_logged(response):
  88. error = self._html_search_regex(
  89. r'(?s)<p[^>]+class="alert-message[^"]*">(.+?)</p>',
  90. response, 'error message', default=None)
  91. if error:
  92. raise ExtractorError('Unable to login: %s' % error, expected=True)
  93. raise ExtractorError('Unable to log in')
  94. class AluraCourseIE(AluraIE):
  95. _VALID_URL = r'https?://(?:cursos\.)?alura\.com\.br/course/(?P<id>[^/]+)'
  96. _LOGIN_URL = 'https://cursos.alura.com.br/loginForm?urlAfterLogin=/loginForm'
  97. _NETRC_MACHINE = 'aluracourse'
  98. _TESTS = [{
  99. 'url': 'https://cursos.alura.com.br/course/clojure-mutabilidade-com-atoms-e-refs',
  100. 'only_matching': True,
  101. }]
  102. @classmethod
  103. def suitable(cls, url):
  104. return False if AluraIE.suitable(url) else super(AluraCourseIE, cls).suitable(url)
  105. def _real_extract(self, url):
  106. course_path = self._match_id(url)
  107. webpage = self._download_webpage(url, course_path)
  108. course_title = self._search_regex(
  109. r'<h1.*?>(.*?)<strong>(?P<course_title>.*?)</strong></h[0-9]>', webpage,
  110. 'course title', default=course_path, group='course_title')
  111. entries = []
  112. if webpage:
  113. for path in re.findall(r'<a\b(?=[^>]* class="[^"]*(?<=[" ])courseSectionList-section[" ])(?=[^>]* href="([^"]*))', webpage):
  114. page_url = urljoin(url, path)
  115. section_path = self._download_webpage(page_url, course_path)
  116. for path_video in re.findall(r'<a\b(?=[^>]* class="[^"]*(?<=[" ])task-menu-nav-item-link-VIDEO[" ])(?=[^>]* href="([^"]*))', section_path):
  117. chapter = clean_html(
  118. self._search_regex(
  119. r'<h3[^>]+class=(["\'])task-menu-section-title-text\1[^>]*>(?P<chapter>[^<]+)',
  120. section_path,
  121. 'chapter',
  122. group='chapter'))
  123. chapter_number = int_or_none(
  124. self._search_regex(
  125. r'<span[^>]+class=(["\'])task-menu-section-title-number[^>]*>(.*?)<strong>(?P<chapter_number>[^<]+)</strong>',
  126. section_path,
  127. 'chapter number',
  128. group='chapter_number'))
  129. video_url = urljoin(url, path_video)
  130. entry = {
  131. '_type': 'url_transparent',
  132. 'id': self._match_id(video_url),
  133. 'url': video_url,
  134. 'id_key': self.ie_key(),
  135. 'chapter': chapter,
  136. 'chapter_number': chapter_number
  137. }
  138. entries.append(entry)
  139. return self.playlist_result(entries, course_path, course_title)