logo

youtube-dl

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

atresplayer.py (4382B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_HTTPError
  6. from ..utils import (
  7. ExtractorError,
  8. int_or_none,
  9. urlencode_postdata,
  10. )
  11. class AtresPlayerIE(InfoExtractor):
  12. _VALID_URL = r'https?://(?:www\.)?atresplayer\.com/[^/]+/[^/]+/[^/]+/[^/]+/(?P<display_id>.+?)_(?P<id>[0-9a-f]{24})'
  13. _NETRC_MACHINE = 'atresplayer'
  14. _TESTS = [
  15. {
  16. 'url': 'https://www.atresplayer.com/antena3/series/pequenas-coincidencias/temporada-1/capitulo-7-asuntos-pendientes_5d4aa2c57ed1a88fc715a615/',
  17. 'info_dict': {
  18. 'id': '5d4aa2c57ed1a88fc715a615',
  19. 'ext': 'mp4',
  20. 'title': 'CapĂ­tulo 7: Asuntos pendientes',
  21. 'description': 'md5:7634cdcb4d50d5381bedf93efb537fbc',
  22. 'duration': 3413,
  23. },
  24. 'params': {
  25. 'format': 'bestvideo',
  26. },
  27. 'skip': 'This video is only available for registered users'
  28. },
  29. {
  30. 'url': 'https://www.atresplayer.com/lasexta/programas/el-club-de-la-comedia/temporada-4/capitulo-10-especial-solidario-nochebuena_5ad08edf986b2855ed47adc4/',
  31. 'only_matching': True,
  32. },
  33. {
  34. 'url': 'https://www.atresplayer.com/antena3/series/el-secreto-de-puente-viejo/el-chico-de-los-tres-lunares/capitulo-977-29-12-14_5ad51046986b2886722ccdea/',
  35. 'only_matching': True,
  36. },
  37. ]
  38. _API_BASE = 'https://api.atresplayer.com/'
  39. def _real_initialize(self):
  40. self._login()
  41. def _handle_error(self, e, code):
  42. if isinstance(e.cause, compat_HTTPError) and e.cause.code == code:
  43. error = self._parse_json(e.cause.read(), None)
  44. if error.get('error') == 'required_registered':
  45. self.raise_login_required()
  46. raise ExtractorError(error['error_description'], expected=True)
  47. raise
  48. def _login(self):
  49. username, password = self._get_login_info()
  50. if username is None:
  51. return
  52. self._request_webpage(
  53. self._API_BASE + 'login', None, 'Downloading login page')
  54. try:
  55. target_url = self._download_json(
  56. 'https://account.atresmedia.com/api/login', None,
  57. 'Logging in', headers={
  58. 'Content-Type': 'application/x-www-form-urlencoded'
  59. }, data=urlencode_postdata({
  60. 'username': username,
  61. 'password': password,
  62. }))['targetUrl']
  63. except ExtractorError as e:
  64. self._handle_error(e, 400)
  65. self._request_webpage(target_url, None, 'Following Target URL')
  66. def _real_extract(self, url):
  67. display_id, video_id = re.match(self._VALID_URL, url).groups()
  68. try:
  69. episode = self._download_json(
  70. self._API_BASE + 'client/v1/player/episode/' + video_id, video_id)
  71. except ExtractorError as e:
  72. self._handle_error(e, 403)
  73. title = episode['titulo']
  74. formats = []
  75. for source in episode.get('sources', []):
  76. src = source.get('src')
  77. if not src:
  78. continue
  79. src_type = source.get('type')
  80. if src_type == 'application/vnd.apple.mpegurl':
  81. formats.extend(self._extract_m3u8_formats(
  82. src, video_id, 'mp4', 'm3u8_native',
  83. m3u8_id='hls', fatal=False))
  84. elif src_type == 'application/dash+xml':
  85. formats.extend(self._extract_mpd_formats(
  86. src, video_id, mpd_id='dash', fatal=False))
  87. self._sort_formats(formats)
  88. heartbeat = episode.get('heartbeat') or {}
  89. omniture = episode.get('omniture') or {}
  90. get_meta = lambda x: heartbeat.get(x) or omniture.get(x)
  91. return {
  92. 'display_id': display_id,
  93. 'id': video_id,
  94. 'title': title,
  95. 'description': episode.get('descripcion'),
  96. 'thumbnail': episode.get('imgPoster'),
  97. 'duration': int_or_none(episode.get('duration')),
  98. 'formats': formats,
  99. 'channel': get_meta('channel'),
  100. 'season': get_meta('season'),
  101. 'episode_number': int_or_none(get_meta('episodeNumber')),
  102. }