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