atresplayer.py 4.3 KB

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