platzi.py 7.2 KB

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