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.

162 lines
6.0 KiB

  1. from __future__ import unicode_literals
  2. import time
  3. import hmac
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_str,
  7. compat_urllib_parse,
  8. compat_urllib_request,
  9. )
  10. from ..utils import (
  11. int_or_none,
  12. float_or_none,
  13. xpath_text,
  14. ExtractorError,
  15. )
  16. class AtresPlayerIE(InfoExtractor):
  17. _VALID_URL = r'https?://(?:www\.)?atresplayer\.com/television/[^/]+/[^/]+/[^/]+/(?P<id>.+?)_\d+\.html'
  18. _TESTS = [
  19. {
  20. 'url': 'http://www.atresplayer.com/television/programas/el-club-de-la-comedia/temporada-4/capitulo-10-especial-solidario-nochebuena_2014122100174.html',
  21. 'md5': 'efd56753cda1bb64df52a3074f62e38a',
  22. 'info_dict': {
  23. 'id': 'capitulo-10-especial-solidario-nochebuena',
  24. 'ext': 'mp4',
  25. 'title': 'Especial Solidario de Nochebuena',
  26. 'description': 'md5:e2d52ff12214fa937107d21064075bf1',
  27. 'duration': 5527.6,
  28. 'thumbnail': 're:^https?://.*\.jpg$',
  29. },
  30. },
  31. {
  32. 'url': 'http://www.atresplayer.com/television/series/el-secreto-de-puente-viejo/el-chico-de-los-tres-lunares/capitulo-977-29-12-14_2014122400174.html',
  33. 'only_matching': True,
  34. },
  35. ]
  36. _USER_AGENT = 'Dalvik/1.6.0 (Linux; U; Android 4.3; GT-I9300 Build/JSS15J'
  37. _MAGIC = 'QWtMLXs414Yo+c#_+Q#K@NN)'
  38. _TIMESTAMP_SHIFT = 30000
  39. _TIME_API_URL = 'http://servicios.atresplayer.com/api/admin/time.json'
  40. _URL_VIDEO_TEMPLATE = 'https://servicios.atresplayer.com/api/urlVideo/{1}/{0}/{1}|{2}|{3}.json'
  41. _PLAYER_URL_TEMPLATE = 'https://servicios.atresplayer.com/episode/getplayer.json?episodePk=%s'
  42. _EPISODE_URL_TEMPLATE = 'http://www.atresplayer.com/episodexml/%s'
  43. _LOGIN_URL = 'https://servicios.atresplayer.com/j_spring_security_check'
  44. def _real_initialize(self):
  45. self._login()
  46. def _login(self):
  47. (username, password) = self._get_login_info()
  48. if username is None:
  49. return
  50. login_form = {
  51. 'j_username': username,
  52. 'j_password': password,
  53. }
  54. request = compat_urllib_request.Request(
  55. self._LOGIN_URL, compat_urllib_parse.urlencode(login_form).encode('utf-8'))
  56. request.add_header('Content-Type', 'application/x-www-form-urlencoded')
  57. response = self._download_webpage(
  58. request, None, 'Logging in as %s' % username)
  59. error = self._html_search_regex(
  60. r'(?s)<ul class="list_error">(.+?)</ul>', response, 'error', default=None)
  61. if error:
  62. raise ExtractorError(
  63. 'Unable to login: %s' % error, expected=True)
  64. def _real_extract(self, url):
  65. video_id = self._match_id(url)
  66. webpage = self._download_webpage(url, video_id)
  67. episode_id = self._search_regex(
  68. r'episode="([^"]+)"', webpage, 'episode id')
  69. timestamp = int_or_none(self._download_webpage(
  70. self._TIME_API_URL,
  71. video_id, 'Downloading timestamp', fatal=False), 1000, time.time())
  72. timestamp_shifted = compat_str(timestamp + self._TIMESTAMP_SHIFT)
  73. token = hmac.new(
  74. self._MAGIC.encode('ascii'),
  75. (episode_id + timestamp_shifted).encode('utf-8')
  76. ).hexdigest()
  77. formats = []
  78. for fmt in ['windows', 'android_tablet']:
  79. request = compat_urllib_request.Request(
  80. self._URL_VIDEO_TEMPLATE.format(fmt, episode_id, timestamp_shifted, token))
  81. request.add_header('User-Agent', self._USER_AGENT)
  82. fmt_json = self._download_json(
  83. request, video_id, 'Downloading %s video JSON' % fmt)
  84. result = fmt_json.get('resultDes')
  85. if result.lower() != 'ok':
  86. raise ExtractorError(
  87. '%s returned error: %s' % (self.IE_NAME, result), expected=True)
  88. for format_id, video_url in fmt_json['resultObject'].items():
  89. if format_id == 'token' or not video_url.startswith('http'):
  90. continue
  91. if video_url.endswith('/Manifest'):
  92. if 'geodeswowsmpra3player' in video_url:
  93. f4m_path = video_url.split('smil:', 1)[-1].split('free_', 1)[0]
  94. f4m_url = 'http://drg.antena3.com/{0}hds/es/sd.f4m'.format(f4m_path)
  95. # this videos are protected by DRM, the f4m downloader doesn't support them
  96. continue
  97. else:
  98. f4m_url = video_url[:-9] + '/manifest.f4m'
  99. formats.extend(self._extract_f4m_formats(f4m_url, video_id))
  100. else:
  101. formats.append({
  102. 'url': video_url,
  103. 'format_id': 'android-%s' % format_id,
  104. 'preference': 1,
  105. })
  106. self._sort_formats(formats)
  107. player = self._download_json(
  108. self._PLAYER_URL_TEMPLATE % episode_id,
  109. episode_id)
  110. path_data = player.get('pathData')
  111. episode = self._download_xml(
  112. self._EPISODE_URL_TEMPLATE % path_data,
  113. video_id, 'Downloading episode XML')
  114. duration = float_or_none(xpath_text(
  115. episode, './media/asset/info/technical/contentDuration', 'duration'))
  116. art = episode.find('./media/asset/info/art')
  117. title = xpath_text(art, './name', 'title')
  118. description = xpath_text(art, './description', 'description')
  119. thumbnail = xpath_text(episode, './media/asset/files/background', 'thumbnail')
  120. subtitles = {}
  121. subtitle_url = xpath_text(episode, './media/asset/files/subtitle', 'subtitle')
  122. if subtitle_url:
  123. subtitles['es'] = [{
  124. 'ext': 'srt',
  125. 'url': subtitle_url,
  126. }]
  127. return {
  128. 'id': video_id,
  129. 'title': title,
  130. 'description': description,
  131. 'thumbnail': thumbnail,
  132. 'duration': duration,
  133. 'formats': formats,
  134. 'subtitles': subtitles,
  135. }