atresplayer.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  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. 'skip': 'This video is only available for registered users'
  24. },
  25. {
  26. 'url': 'https://www.atresplayer.com/lasexta/programas/el-club-de-la-comedia/temporada-4/capitulo-10-especial-solidario-nochebuena_5ad08edf986b2855ed47adc4/',
  27. 'only_matching': True,
  28. },
  29. {
  30. 'url': 'https://www.atresplayer.com/antena3/series/el-secreto-de-puente-viejo/el-chico-de-los-tres-lunares/capitulo-977-29-12-14_5ad51046986b2886722ccdea/',
  31. 'only_matching': True,
  32. },
  33. ]
  34. _API_BASE = 'https://api.atresplayer.com/'
  35. def _handle_error(self, e, code):
  36. if isinstance(e.cause, compat_HTTPError) and e.cause.code == code:
  37. error = self._parse_json(e.cause.read(), None)
  38. if error.get('error') == 'required_registered':
  39. self.raise_login_required()
  40. raise ExtractorError(error['error_description'], expected=True)
  41. raise
  42. def _perform_login(self, username, password):
  43. self._request_webpage(
  44. self._API_BASE + 'login', None, 'Downloading login page')
  45. try:
  46. target_url = self._download_json(
  47. 'https://account.atresmedia.com/api/login', None,
  48. 'Logging in', headers={
  49. 'Content-Type': 'application/x-www-form-urlencoded'
  50. }, data=urlencode_postdata({
  51. 'username': username,
  52. 'password': password,
  53. }))['targetUrl']
  54. except ExtractorError as e:
  55. self._handle_error(e, 400)
  56. self._request_webpage(target_url, None, 'Following Target URL')
  57. def _real_extract(self, url):
  58. display_id, video_id = self._match_valid_url(url).groups()
  59. try:
  60. episode = self._download_json(
  61. self._API_BASE + 'client/v1/player/episode/' + video_id, video_id)
  62. except ExtractorError as e:
  63. self._handle_error(e, 403)
  64. title = episode['titulo']
  65. formats = []
  66. subtitles = {}
  67. for source in episode.get('sources', []):
  68. src = source.get('src')
  69. if not src:
  70. continue
  71. src_type = source.get('type')
  72. if src_type == 'application/vnd.apple.mpegurl':
  73. formats, subtitles = self._extract_m3u8_formats(
  74. src, video_id, 'mp4', 'm3u8_native',
  75. m3u8_id='hls', fatal=False)
  76. elif src_type == 'application/dash+xml':
  77. formats, subtitles = self._extract_mpd_formats(
  78. src, video_id, mpd_id='dash', fatal=False)
  79. self._sort_formats(formats)
  80. heartbeat = episode.get('heartbeat') or {}
  81. omniture = episode.get('omniture') or {}
  82. get_meta = lambda x: heartbeat.get(x) or omniture.get(x)
  83. return {
  84. 'display_id': display_id,
  85. 'id': video_id,
  86. 'title': title,
  87. 'description': episode.get('descripcion'),
  88. 'thumbnail': episode.get('imgPoster'),
  89. 'duration': int_or_none(episode.get('duration')),
  90. 'formats': formats,
  91. 'channel': get_meta('channel'),
  92. 'season': get_meta('season'),
  93. 'episode_number': int_or_none(get_meta('episodeNumber')),
  94. 'subtitles': subtitles,
  95. }