Merge remote-tracking branch 'Dineshs91/f4m-2.0'
[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(
60             self._MAGIC.encode('ascii'),
61             (episode_id + timestamp_shifted).encode('utf-8')
62         ).hexdigest()
63
64         formats = []
65         for fmt in ['windows', 'android_tablet']:
66             request = compat_urllib_request.Request(
67                 self._URL_VIDEO_TEMPLATE.format(fmt, episode_id, timestamp_shifted, token))
68             request.add_header('Youtubedl-user-agent', self._USER_AGENT)
69
70             fmt_json = self._download_json(
71                 request, video_id, 'Downloading %s video JSON' % fmt)
72
73             result = fmt_json.get('resultDes')
74             if result.lower() != 'ok':
75                 raise ExtractorError(
76                     '%s returned error: %s' % (self.IE_NAME, result), expected=True)
77
78             for _, video_url in fmt_json['resultObject'].items():
79                 if video_url.endswith('/Manifest'):
80                     formats.extend(self._extract_f4m_formats(video_url[:-9] + '/manifest.f4m', video_id))
81                 else:
82                     formats.append({
83                         'url': video_url,
84                         'format_id': 'android',
85                         'preference': 1,
86                     })
87         self._sort_formats(formats)
88
89         player = self._download_json(
90             self._PLAYER_URL_TEMPLATE % episode_id,
91             episode_id)
92
93         path_data = player.get('pathData')
94
95         episode = self._download_xml(
96             self._EPISODE_URL_TEMPLATE % path_data,
97             video_id, 'Downloading episode XML')
98
99         duration = float_or_none(xpath_text(
100             episode, './media/asset/info/technical/contentDuration', 'duration'))
101
102         art = episode.find('./media/asset/info/art')
103         title = xpath_text(art, './name', 'title')
104         description = xpath_text(art, './description', 'description')
105         thumbnail = xpath_text(episode, './media/asset/files/background', 'thumbnail')
106
107         return {
108             'id': video_id,
109             'title': title,
110             'description': description,
111             'thumbnail': thumbnail,
112             'duration': duration,
113             'formats': formats,
114         }