]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/atresplayer.py
Imported Upstream version 2015.05.15
[youtubedl] / 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 ..compat import (
8 compat_str,
9 compat_urllib_parse,
10 compat_urllib_request,
11 )
12 from ..utils import (
13 int_or_none,
14 float_or_none,
15 xpath_text,
16 ExtractorError,
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': 're:^https?://.*\.jpg$',
34 },
35 },
36 {
37 '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',
38 'only_matching': True,
39 },
40 ]
41
42 _USER_AGENT = 'Dalvik/1.6.0 (Linux; U; Android 4.3; GT-I9300 Build/JSS15J'
43 _MAGIC = 'QWtMLXs414Yo+c#_+Q#K@NN)'
44 _TIMESTAMP_SHIFT = 30000
45
46 _TIME_API_URL = 'http://servicios.atresplayer.com/api/admin/time.json'
47 _URL_VIDEO_TEMPLATE = 'https://servicios.atresplayer.com/api/urlVideo/{1}/{0}/{1}|{2}|{3}.json'
48 _PLAYER_URL_TEMPLATE = 'https://servicios.atresplayer.com/episode/getplayer.json?episodePk=%s'
49 _EPISODE_URL_TEMPLATE = 'http://www.atresplayer.com/episodexml/%s'
50
51 _LOGIN_URL = 'https://servicios.atresplayer.com/j_spring_security_check'
52
53 def _real_initialize(self):
54 self._login()
55
56 def _login(self):
57 (username, password) = self._get_login_info()
58 if username is None:
59 return
60
61 login_form = {
62 'j_username': username,
63 'j_password': password,
64 }
65
66 request = compat_urllib_request.Request(
67 self._LOGIN_URL, compat_urllib_parse.urlencode(login_form).encode('utf-8'))
68 request.add_header('Content-Type', 'application/x-www-form-urlencoded')
69 response = self._download_webpage(
70 request, None, 'Logging in as %s' % username)
71
72 error = self._html_search_regex(
73 r'(?s)<ul class="list_error">(.+?)</ul>', response, 'error', default=None)
74 if error:
75 raise ExtractorError(
76 'Unable to login: %s' % error, expected=True)
77
78 def _real_extract(self, url):
79 video_id = self._match_id(url)
80
81 webpage = self._download_webpage(url, video_id)
82
83 episode_id = self._search_regex(
84 r'episode="([^"]+)"', webpage, 'episode id')
85
86 timestamp = int_or_none(self._download_webpage(
87 self._TIME_API_URL,
88 video_id, 'Downloading timestamp', fatal=False), 1000, time.time())
89 timestamp_shifted = compat_str(timestamp + self._TIMESTAMP_SHIFT)
90 token = hmac.new(
91 self._MAGIC.encode('ascii'),
92 (episode_id + timestamp_shifted).encode('utf-8')
93 ).hexdigest()
94
95 formats = []
96 for fmt in ['windows', 'android_tablet']:
97 request = compat_urllib_request.Request(
98 self._URL_VIDEO_TEMPLATE.format(fmt, episode_id, timestamp_shifted, token))
99 request.add_header('User-Agent', self._USER_AGENT)
100
101 fmt_json = self._download_json(
102 request, video_id, 'Downloading %s video JSON' % fmt)
103
104 result = fmt_json.get('resultDes')
105 if result.lower() != 'ok':
106 raise ExtractorError(
107 '%s returned error: %s' % (self.IE_NAME, result), expected=True)
108
109 for format_id, video_url in fmt_json['resultObject'].items():
110 if format_id == 'token' or not video_url.startswith('http'):
111 continue
112 if video_url.endswith('/Manifest'):
113 if 'geodeswowsmpra3player' in video_url:
114 f4m_path = video_url.split('smil:', 1)[-1].split('free_', 1)[0]
115 f4m_url = 'http://drg.antena3.com/{0}hds/es/sd.f4m'.format(f4m_path)
116 # this videos are protected by DRM, the f4m downloader doesn't support them
117 continue
118 else:
119 f4m_url = video_url[:-9] + '/manifest.f4m'
120 formats.extend(self._extract_f4m_formats(f4m_url, video_id))
121 else:
122 formats.append({
123 'url': video_url,
124 'format_id': 'android-%s' % format_id,
125 'preference': 1,
126 })
127 self._sort_formats(formats)
128
129 player = self._download_json(
130 self._PLAYER_URL_TEMPLATE % episode_id,
131 episode_id)
132
133 path_data = player.get('pathData')
134
135 episode = self._download_xml(
136 self._EPISODE_URL_TEMPLATE % path_data,
137 video_id, 'Downloading episode XML')
138
139 duration = float_or_none(xpath_text(
140 episode, './media/asset/info/technical/contentDuration', 'duration'))
141
142 art = episode.find('./media/asset/info/art')
143 title = xpath_text(art, './name', 'title')
144 description = xpath_text(art, './description', 'description')
145 thumbnail = xpath_text(episode, './media/asset/files/background', 'thumbnail')
146
147 subtitles = {}
148 subtitle_url = xpath_text(episode, './media/asset/files/subtitle', 'subtitle')
149 if subtitle_url:
150 subtitles['es'] = [{
151 'ext': 'srt',
152 'url': subtitle_url,
153 }]
154
155 return {
156 'id': video_id,
157 'title': title,
158 'description': description,
159 'thumbnail': thumbnail,
160 'duration': duration,
161 'formats': formats,
162 'subtitles': subtitles,
163 }