]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/atresplayer.py
Prepare for release.
[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 _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('Youtubedl-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 _, video_url in fmt_json['resultObject'].items():
109 if video_url.endswith('/Manifest'):
110 formats.extend(self._extract_f4m_formats(video_url[:-9] + '/manifest.f4m', video_id))
111 else:
112 formats.append({
113 'url': video_url,
114 'format_id': 'android',
115 'preference': 1,
116 })
117 self._sort_formats(formats)
118
119 player = self._download_json(
120 self._PLAYER_URL_TEMPLATE % episode_id,
121 episode_id)
122
123 path_data = player.get('pathData')
124
125 episode = self._download_xml(
126 self._EPISODE_URL_TEMPLATE % path_data,
127 video_id, 'Downloading episode XML')
128
129 duration = float_or_none(xpath_text(
130 episode, './media/asset/info/technical/contentDuration', 'duration'))
131
132 art = episode.find('./media/asset/info/art')
133 title = xpath_text(art, './name', 'title')
134 description = xpath_text(art, './description', 'description')
135 thumbnail = xpath_text(episode, './media/asset/files/background', 'thumbnail')
136
137 return {
138 'id': video_id,
139 'title': title,
140 'description': description,
141 'thumbnail': thumbnail,
142 'duration': duration,
143 'formats': formats,
144 }