]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/atresplayer.py
ae1c094277251b6bfe26298d3c8dd8dfd741a905
[youtubedl] / youtube_dl / extractor / atresplayer.py
1 from __future__ import unicode_literals
2
3 import time
4 import hmac
5 import hashlib
6 import re
7
8 from .common import InfoExtractor
9 from ..compat import compat_str
10 from ..utils import (
11 ExtractorError,
12 float_or_none,
13 int_or_none,
14 sanitized_Request,
15 urlencode_postdata,
16 xpath_text,
17 )
18
19
20 class AtresPlayerIE(InfoExtractor):
21 _VALID_URL = r'https?://(?:www\.)?atresplayer\.com/television/[^/]+/[^/]+/[^/]+/(?P<id>.+?)_\d+\.html'
22 _NETRC_MACHINE = 'atresplayer'
23 _TESTS = [
24 {
25 'url': 'http://www.atresplayer.com/television/programas/el-club-de-la-comedia/temporada-4/capitulo-10-especial-solidario-nochebuena_2014122100174.html',
26 'md5': 'efd56753cda1bb64df52a3074f62e38a',
27 'info_dict': {
28 'id': 'capitulo-10-especial-solidario-nochebuena',
29 'ext': 'mp4',
30 'title': 'Especial Solidario de Nochebuena',
31 'description': 'md5:e2d52ff12214fa937107d21064075bf1',
32 'duration': 5527.6,
33 'thumbnail': r're:^https?://.*\.jpg$',
34 },
35 'skip': 'This video is only available for registered users'
36 },
37 {
38 'url': 'http://www.atresplayer.com/television/especial/videoencuentros/temporada-1/capitulo-112-david-bustamante_2014121600375.html',
39 'md5': '6e52cbb513c405e403dbacb7aacf8747',
40 'info_dict': {
41 'id': 'capitulo-112-david-bustamante',
42 'ext': 'flv',
43 'title': 'David Bustamante',
44 'description': 'md5:f33f1c0a05be57f6708d4dd83a3b81c6',
45 'duration': 1439.0,
46 'thumbnail': r're:^https?://.*\.jpg$',
47 },
48 },
49 {
50 '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',
51 'only_matching': True,
52 },
53 ]
54
55 _USER_AGENT = 'Dalvik/1.6.0 (Linux; U; Android 4.3; GT-I9300 Build/JSS15J'
56 _MAGIC = 'QWtMLXs414Yo+c#_+Q#K@NN)'
57 _TIMESTAMP_SHIFT = 30000
58
59 _TIME_API_URL = 'http://servicios.atresplayer.com/api/admin/time.json'
60 _URL_VIDEO_TEMPLATE = 'https://servicios.atresplayer.com/api/urlVideo/{1}/{0}/{1}|{2}|{3}.json'
61 _PLAYER_URL_TEMPLATE = 'https://servicios.atresplayer.com/episode/getplayer.json?episodePk=%s'
62 _EPISODE_URL_TEMPLATE = 'http://www.atresplayer.com/episodexml/%s'
63
64 _LOGIN_URL = 'https://servicios.atresplayer.com/j_spring_security_check'
65
66 _ERRORS = {
67 'UNPUBLISHED': 'We\'re sorry, but this video is not yet available.',
68 'DELETED': 'This video has expired and is no longer available for online streaming.',
69 'GEOUNPUBLISHED': 'We\'re sorry, but this video is not available in your region due to right restrictions.',
70 # 'PREMIUM': 'PREMIUM',
71 }
72
73 def _real_initialize(self):
74 self._login()
75
76 def _login(self):
77 username, password = self._get_login_info()
78 if username is None:
79 return
80
81 login_form = {
82 'j_username': username,
83 'j_password': password,
84 }
85
86 request = sanitized_Request(
87 self._LOGIN_URL, urlencode_postdata(login_form))
88 request.add_header('Content-Type', 'application/x-www-form-urlencoded')
89 response = self._download_webpage(
90 request, None, 'Logging in')
91
92 error = self._html_search_regex(
93 r'(?s)<ul[^>]+class="[^"]*\blist_error\b[^"]*">(.+?)</ul>',
94 response, 'error', default=None)
95 if error:
96 raise ExtractorError(
97 'Unable to login: %s' % error, expected=True)
98
99 def _real_extract(self, url):
100 video_id = self._match_id(url)
101
102 webpage = self._download_webpage(url, video_id)
103
104 episode_id = self._search_regex(
105 r'episode="([^"]+)"', webpage, 'episode id')
106
107 request = sanitized_Request(
108 self._PLAYER_URL_TEMPLATE % episode_id,
109 headers={'User-Agent': self._USER_AGENT})
110 player = self._download_json(request, episode_id, 'Downloading player JSON')
111
112 episode_type = player.get('typeOfEpisode')
113 error_message = self._ERRORS.get(episode_type)
114 if error_message:
115 raise ExtractorError(
116 '%s returned error: %s' % (self.IE_NAME, error_message), expected=True)
117
118 formats = []
119 video_url = player.get('urlVideo')
120 if video_url:
121 format_info = {
122 'url': video_url,
123 'format_id': 'http',
124 }
125 mobj = re.search(r'(?P<bitrate>\d+)K_(?P<width>\d+)x(?P<height>\d+)', video_url)
126 if mobj:
127 format_info.update({
128 'width': int_or_none(mobj.group('width')),
129 'height': int_or_none(mobj.group('height')),
130 'tbr': int_or_none(mobj.group('bitrate')),
131 })
132 formats.append(format_info)
133
134 timestamp = int_or_none(self._download_webpage(
135 self._TIME_API_URL,
136 video_id, 'Downloading timestamp', fatal=False), 1000, time.time())
137 timestamp_shifted = compat_str(timestamp + self._TIMESTAMP_SHIFT)
138 token = hmac.new(
139 self._MAGIC.encode('ascii'),
140 (episode_id + timestamp_shifted).encode('utf-8'), hashlib.md5
141 ).hexdigest()
142
143 request = sanitized_Request(
144 self._URL_VIDEO_TEMPLATE.format('windows', episode_id, timestamp_shifted, token),
145 headers={'User-Agent': self._USER_AGENT})
146
147 fmt_json = self._download_json(
148 request, video_id, 'Downloading windows video JSON')
149
150 result = fmt_json.get('resultDes')
151 if result.lower() != 'ok':
152 raise ExtractorError(
153 '%s returned error: %s' % (self.IE_NAME, result), expected=True)
154
155 for format_id, video_url in fmt_json['resultObject'].items():
156 if format_id == 'token' or not video_url.startswith('http'):
157 continue
158 if 'geodeswowsmpra3player' in video_url:
159 # f4m_path = video_url.split('smil:', 1)[-1].split('free_', 1)[0]
160 # f4m_url = 'http://drg.antena3.com/{0}hds/es/sd.f4m'.format(f4m_path)
161 # this videos are protected by DRM, the f4m downloader doesn't support them
162 continue
163 video_url_hd = video_url.replace('free_es', 'es')
164 formats.extend(self._extract_f4m_formats(
165 video_url_hd[:-9] + '/manifest.f4m', video_id, f4m_id='hds',
166 fatal=False))
167 formats.extend(self._extract_mpd_formats(
168 video_url_hd[:-9] + '/manifest.mpd', video_id, mpd_id='dash',
169 fatal=False))
170 self._sort_formats(formats)
171
172 path_data = player.get('pathData')
173
174 episode = self._download_xml(
175 self._EPISODE_URL_TEMPLATE % path_data, video_id,
176 'Downloading episode XML')
177
178 duration = float_or_none(xpath_text(
179 episode, './media/asset/info/technical/contentDuration', 'duration'))
180
181 art = episode.find('./media/asset/info/art')
182 title = xpath_text(art, './name', 'title')
183 description = xpath_text(art, './description', 'description')
184 thumbnail = xpath_text(episode, './media/asset/files/background', 'thumbnail')
185
186 subtitles = {}
187 subtitle_url = xpath_text(episode, './media/asset/files/subtitle', 'subtitle')
188 if subtitle_url:
189 subtitles['es'] = [{
190 'ext': 'srt',
191 'url': subtitle_url,
192 }]
193
194 return {
195 'id': video_id,
196 'title': title,
197 'description': description,
198 'thumbnail': thumbnail,
199 'duration': duration,
200 'formats': formats,
201 'subtitles': subtitles,
202 }