platzi.py 7.3 KB

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