You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
7.2 KiB

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