logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

platzi.py (7630B)


  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 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. # client_data may contain "};" so that we have to try more
  92. # strict regex first
  93. (r'client_data\s*=\s*({.+?})\s*;\s*\n',
  94. r'client_data\s*=\s*({.+?})\s*;'),
  95. webpage, 'client data'),
  96. lecture_id)
  97. material = data['initialState']['material']
  98. desc = material['description']
  99. title = desc['title']
  100. formats = []
  101. for server_id, server in material['videos'].items():
  102. if not isinstance(server, dict):
  103. continue
  104. for format_id in ('hls', 'dash'):
  105. format_url = url_or_none(server.get(format_id))
  106. if not format_url:
  107. continue
  108. if format_id == 'hls':
  109. formats.extend(self._extract_m3u8_formats(
  110. format_url, lecture_id, 'mp4',
  111. entry_protocol='m3u8_native', m3u8_id=format_id,
  112. note='Downloading %s m3u8 information' % server_id,
  113. fatal=False))
  114. elif format_id == 'dash':
  115. formats.extend(self._extract_mpd_formats(
  116. format_url, lecture_id, mpd_id=format_id,
  117. note='Downloading %s MPD manifest' % server_id,
  118. fatal=False))
  119. self._sort_formats(formats)
  120. content = str_or_none(desc.get('content'))
  121. description = (clean_html(compat_b64decode(content).decode('utf-8'))
  122. if content else None)
  123. duration = int_or_none(material.get('duration'), invscale=60)
  124. return {
  125. 'id': lecture_id,
  126. 'title': title,
  127. 'description': description,
  128. 'duration': duration,
  129. 'formats': formats,
  130. }
  131. class PlatziCourseIE(PlatziBaseIE):
  132. _VALID_URL = r'''(?x)
  133. https?://
  134. (?:
  135. platzi\.com/clases| # es version
  136. courses\.platzi\.com/classes # en version
  137. )/(?P<id>[^/?\#&]+)
  138. '''
  139. _TESTS = [{
  140. 'url': 'https://platzi.com/clases/next-js/',
  141. 'info_dict': {
  142. 'id': '1311',
  143. 'title': 'Curso de Next.js',
  144. },
  145. 'playlist_count': 22,
  146. }, {
  147. 'url': 'https://courses.platzi.com/classes/communication-codestream/',
  148. 'info_dict': {
  149. 'id': '1367',
  150. 'title': 'Codestream Course',
  151. },
  152. 'playlist_count': 14,
  153. }]
  154. @classmethod
  155. def suitable(cls, url):
  156. return False if PlatziIE.suitable(url) else super(PlatziCourseIE, cls).suitable(url)
  157. def _real_extract(self, url):
  158. course_name = self._match_id(url)
  159. webpage = self._download_webpage(url, course_name)
  160. props = self._parse_json(
  161. self._search_regex(r'data\s*=\s*({.+?})\s*;', webpage, 'data'),
  162. course_name)['initialProps']
  163. entries = []
  164. for chapter_num, chapter in enumerate(props['concepts'], 1):
  165. if not isinstance(chapter, dict):
  166. continue
  167. materials = chapter.get('materials')
  168. if not materials or not isinstance(materials, list):
  169. continue
  170. chapter_title = chapter.get('title')
  171. chapter_id = str_or_none(chapter.get('id'))
  172. for material in materials:
  173. if not isinstance(material, dict):
  174. continue
  175. if material.get('material_type') != 'video':
  176. continue
  177. video_url = urljoin(url, material.get('url'))
  178. if not video_url:
  179. continue
  180. entries.append({
  181. '_type': 'url_transparent',
  182. 'url': video_url,
  183. 'title': str_or_none(material.get('name')),
  184. 'id': str_or_none(material.get('id')),
  185. 'ie_key': PlatziIE.ie_key(),
  186. 'chapter': chapter_title,
  187. 'chapter_number': chapter_num,
  188. 'chapter_id': chapter_id,
  189. })
  190. course_id = compat_str(try_get(props, lambda x: x['course']['id']))
  191. course_title = try_get(props, lambda x: x['course']['name'], compat_str)
  192. return self.playlist_result(entries, course_id, course_title)