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