alura.py 6.3 KB

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