platzi.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import base64
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. ExtractorError,
  5. clean_html,
  6. int_or_none,
  7. str_or_none,
  8. try_get,
  9. url_or_none,
  10. urlencode_postdata,
  11. urljoin,
  12. )
  13. class PlatziBaseIE(InfoExtractor):
  14. _LOGIN_URL = 'https://platzi.com/login/'
  15. _NETRC_MACHINE = 'platzi'
  16. def _perform_login(self, username, password):
  17. login_page = self._download_webpage(
  18. self._LOGIN_URL, None, 'Downloading login page')
  19. login_form = self._hidden_inputs(login_page)
  20. login_form.update({
  21. 'email': username,
  22. 'password': password,
  23. })
  24. urlh = self._request_webpage(
  25. self._LOGIN_URL, None, 'Logging in',
  26. data=urlencode_postdata(login_form),
  27. headers={'Referer': self._LOGIN_URL})
  28. # login succeeded
  29. if 'platzi.com/login' not in urlh.url:
  30. return
  31. login_error = self._webpage_read_content(
  32. urlh, self._LOGIN_URL, None, 'Downloading login error page')
  33. login = self._parse_json(
  34. self._search_regex(
  35. r'login\s*=\s*({.+?})(?:\s*;|\s*</script)', login_error, 'login'),
  36. None)
  37. for kind in ('error', 'password', 'nonFields'):
  38. error = str_or_none(login.get(f'{kind}Error'))
  39. if error:
  40. raise ExtractorError(
  41. f'Unable to login: {error}', expected=True)
  42. raise ExtractorError('Unable to log in')
  43. class PlatziIE(PlatziBaseIE):
  44. _VALID_URL = r'''(?x)
  45. https?://
  46. (?:
  47. platzi\.com/clases| # es version
  48. courses\.platzi\.com/classes # en version
  49. )/[^/]+/(?P<id>\d+)-[^/?\#&]+
  50. '''
  51. _TESTS = [{
  52. 'url': 'https://platzi.com/clases/1311-next-js/12074-creando-nuestra-primera-pagina/',
  53. 'md5': '8f56448241005b561c10f11a595b37e3',
  54. 'info_dict': {
  55. 'id': '12074',
  56. 'ext': 'mp4',
  57. 'title': 'Creando nuestra primera página',
  58. 'description': 'md5:4c866e45034fc76412fbf6e60ae008bc',
  59. 'duration': 420,
  60. },
  61. 'skip': 'Requires platzi account credentials',
  62. }, {
  63. 'url': 'https://courses.platzi.com/classes/1367-communication-codestream/13430-background/',
  64. 'info_dict': {
  65. 'id': '13430',
  66. 'ext': 'mp4',
  67. 'title': 'Background',
  68. 'description': 'md5:49c83c09404b15e6e71defaf87f6b305',
  69. 'duration': 360,
  70. },
  71. 'skip': 'Requires platzi account credentials',
  72. 'params': {
  73. 'skip_download': True,
  74. },
  75. }]
  76. def _real_extract(self, url):
  77. lecture_id = self._match_id(url)
  78. webpage = self._download_webpage(url, lecture_id)
  79. data = self._parse_json(
  80. self._search_regex(
  81. # client_data may contain "};" so that we have to try more
  82. # strict regex first
  83. (r'client_data\s*=\s*({.+?})\s*;\s*\n',
  84. r'client_data\s*=\s*({.+?})\s*;'),
  85. webpage, 'client data'),
  86. lecture_id)
  87. material = data['initialState']['material']
  88. desc = material['description']
  89. title = desc['title']
  90. formats = []
  91. for server_id, server in material['videos'].items():
  92. if not isinstance(server, dict):
  93. continue
  94. for format_id in ('hls', 'dash'):
  95. format_url = url_or_none(server.get(format_id))
  96. if not format_url:
  97. continue
  98. if format_id == 'hls':
  99. formats.extend(self._extract_m3u8_formats(
  100. format_url, lecture_id, 'mp4',
  101. entry_protocol='m3u8_native', m3u8_id=format_id,
  102. note=f'Downloading {server_id} m3u8 information',
  103. fatal=False))
  104. elif format_id == 'dash':
  105. formats.extend(self._extract_mpd_formats(
  106. format_url, lecture_id, mpd_id=format_id,
  107. note=f'Downloading {server_id} MPD manifest',
  108. fatal=False))
  109. content = str_or_none(desc.get('content'))
  110. description = (clean_html(base64.b64decode(content).decode('utf-8'))
  111. if content else None)
  112. duration = int_or_none(material.get('duration'), invscale=60)
  113. return {
  114. 'id': lecture_id,
  115. 'title': title,
  116. 'description': description,
  117. 'duration': duration,
  118. 'formats': formats,
  119. }
  120. class PlatziCourseIE(PlatziBaseIE):
  121. _VALID_URL = r'''(?x)
  122. https?://
  123. (?:
  124. platzi\.com/clases| # es version
  125. courses\.platzi\.com/classes # en version
  126. )/(?P<id>[^/?\#&]+)
  127. '''
  128. _TESTS = [{
  129. 'url': 'https://platzi.com/clases/next-js/',
  130. 'info_dict': {
  131. 'id': '1311',
  132. 'title': 'Curso de Next.js',
  133. },
  134. 'playlist_count': 22,
  135. }, {
  136. 'url': 'https://courses.platzi.com/classes/communication-codestream/',
  137. 'info_dict': {
  138. 'id': '1367',
  139. 'title': 'Codestream Course',
  140. },
  141. 'playlist_count': 14,
  142. }]
  143. @classmethod
  144. def suitable(cls, url):
  145. return False if PlatziIE.suitable(url) else super().suitable(url)
  146. def _real_extract(self, url):
  147. course_name = self._match_id(url)
  148. webpage = self._download_webpage(url, course_name)
  149. props = self._parse_json(
  150. self._search_regex(r'data\s*=\s*({.+?})\s*;', webpage, 'data'),
  151. course_name)['initialProps']
  152. entries = []
  153. for chapter_num, chapter in enumerate(props['concepts'], 1):
  154. if not isinstance(chapter, dict):
  155. continue
  156. materials = chapter.get('materials')
  157. if not materials or not isinstance(materials, list):
  158. continue
  159. chapter_title = chapter.get('title')
  160. chapter_id = str_or_none(chapter.get('id'))
  161. for material in materials:
  162. if not isinstance(material, dict):
  163. continue
  164. if material.get('material_type') != 'video':
  165. continue
  166. video_url = urljoin(url, material.get('url'))
  167. if not video_url:
  168. continue
  169. entries.append({
  170. '_type': 'url_transparent',
  171. 'url': video_url,
  172. 'title': str_or_none(material.get('name')),
  173. 'id': str_or_none(material.get('id')),
  174. 'ie_key': PlatziIE.ie_key(),
  175. 'chapter': chapter_title,
  176. 'chapter_number': chapter_num,
  177. 'chapter_id': chapter_id,
  178. })
  179. course_id = str(try_get(props, lambda x: x['course']['id']))
  180. course_title = try_get(props, lambda x: x['course']['name'], str)
  181. return self.playlist_result(entries, course_id, course_title)