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.

114 lines
4.2 KiB

  1. from __future__ import unicode_literals
  2. import time
  3. import hmac
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. compat_str,
  7. compat_urllib_request,
  8. int_or_none,
  9. float_or_none,
  10. xpath_text,
  11. ExtractorError,
  12. )
  13. class AtresPlayerIE(InfoExtractor):
  14. _VALID_URL = r'https?://(?:www\.)?atresplayer\.com/television/[^/]+/[^/]+/[^/]+/(?P<id>.+?)_\d+\.html'
  15. _TESTS = [
  16. {
  17. 'url': 'http://www.atresplayer.com/television/programas/el-club-de-la-comedia/temporada-4/capitulo-10-especial-solidario-nochebuena_2014122100174.html',
  18. 'md5': 'efd56753cda1bb64df52a3074f62e38a',
  19. 'info_dict': {
  20. 'id': 'capitulo-10-especial-solidario-nochebuena',
  21. 'ext': 'mp4',
  22. 'title': 'Especial Solidario de Nochebuena',
  23. 'description': 'md5:e2d52ff12214fa937107d21064075bf1',
  24. 'duration': 5527.6,
  25. 'thumbnail': 're:^https?://.*\.jpg$',
  26. },
  27. },
  28. {
  29. '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',
  30. 'only_matching': True,
  31. },
  32. ]
  33. _USER_AGENT = 'Dalvik/1.6.0 (Linux; U; Android 4.3; GT-I9300 Build/JSS15J'
  34. _MAGIC = 'QWtMLXs414Yo+c#_+Q#K@NN)'
  35. _TIMESTAMP_SHIFT = 30000
  36. _TIME_API_URL = 'http://servicios.atresplayer.com/api/admin/time.json'
  37. _URL_VIDEO_TEMPLATE = 'https://servicios.atresplayer.com/api/urlVideo/{1}/{0}/{1}|{2}|{3}.json'
  38. _PLAYER_URL_TEMPLATE = 'https://servicios.atresplayer.com/episode/getplayer.json?episodePk=%s'
  39. _EPISODE_URL_TEMPLATE = 'http://www.atresplayer.com/episodexml/%s'
  40. def _real_extract(self, url):
  41. video_id = self._match_id(url)
  42. webpage = self._download_webpage(url, video_id)
  43. episode_id = self._search_regex(
  44. r'episode="([^"]+)"', webpage, 'episode id')
  45. timestamp = int_or_none(self._download_webpage(
  46. self._TIME_API_URL,
  47. video_id, 'Downloading timestamp', fatal=False), 1000, time.time())
  48. timestamp_shifted = compat_str(timestamp + self._TIMESTAMP_SHIFT)
  49. token = hmac.new(
  50. self._MAGIC.encode('ascii'),
  51. (episode_id + timestamp_shifted).encode('utf-8')
  52. ).hexdigest()
  53. formats = []
  54. for fmt in ['windows', 'android_tablet']:
  55. request = compat_urllib_request.Request(
  56. self._URL_VIDEO_TEMPLATE.format(fmt, episode_id, timestamp_shifted, token))
  57. request.add_header('Youtubedl-user-agent', self._USER_AGENT)
  58. fmt_json = self._download_json(
  59. request, video_id, 'Downloading %s video JSON' % fmt)
  60. result = fmt_json.get('resultDes')
  61. if result.lower() != 'ok':
  62. raise ExtractorError(
  63. '%s returned error: %s' % (self.IE_NAME, result), expected=True)
  64. for _, video_url in fmt_json['resultObject'].items():
  65. if video_url.endswith('/Manifest'):
  66. formats.extend(self._extract_f4m_formats(video_url[:-9] + '/manifest.f4m', video_id))
  67. else:
  68. formats.append({
  69. 'url': video_url,
  70. 'format_id': 'android',
  71. 'preference': 1,
  72. })
  73. self._sort_formats(formats)
  74. player = self._download_json(
  75. self._PLAYER_URL_TEMPLATE % episode_id,
  76. episode_id)
  77. path_data = player.get('pathData')
  78. episode = self._download_xml(
  79. self._EPISODE_URL_TEMPLATE % path_data,
  80. video_id, 'Downloading episode XML')
  81. duration = float_or_none(xpath_text(
  82. episode, './media/asset/info/technical/contentDuration', 'duration'))
  83. art = episode.find('./media/asset/info/art')
  84. title = xpath_text(art, './name', 'title')
  85. description = xpath_text(art, './description', 'description')
  86. thumbnail = xpath_text(episode, './media/asset/files/background', 'thumbnail')
  87. return {
  88. 'id': video_id,
  89. 'title': title,
  90. 'description': description,
  91. 'thumbnail': thumbnail,
  92. 'duration': duration,
  93. 'formats': formats,
  94. }