alura.py 6.3 KB

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