[telecinco] Update test
[youtube-dl] / youtube_dl / extractor / telecinco.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5
6 from .common import InfoExtractor
7 from ..compat import (
8     compat_urllib_parse,
9     compat_urllib_parse_unquote,
10     compat_urlparse,
11 )
12 from ..utils import (
13     get_element_by_attribute,
14     parse_duration,
15     strip_jsonp,
16 )
17
18
19 class TelecincoIE(InfoExtractor):
20     IE_NAME = 'telecinco.es'
21     _VALID_URL = r'https?://www\.(?:telecinco\.es|cuatro\.com)/(?:[^/]+/)+(?P<id>.+?)\.html'
22
23     _TESTS = [{
24         'url': 'http://www.telecinco.es/robinfood/temporada-01/t01xp14/Bacalao-cocochas-pil-pil_0_1876350223.html',
25         'md5': '5cbef3ad5ef17bf0d21570332d140729',
26         'info_dict': {
27             'id': 'MDSVID20141015_0058',
28             'ext': 'mp4',
29             'title': 'Con Martín Berasategui, hacer un bacalao al ...',
30             'duration': 662,
31         },
32     }, {
33         'url': 'http://www.cuatro.com/deportes/futbol/barcelona/Leo_Messi-Champions-Roma_2_2052780128.html',
34         'md5': '0a5b9f3cc8b074f50a0578f823a12694',
35         'info_dict': {
36             'id': 'MDSVID20150916_0128',
37             'ext': 'mp4',
38             'title': '¿Quién es este ex futbolista con el que hablan ...',
39             'duration': 79,
40         },
41     }, {
42         'url': 'http://www.telecinco.es/informativos/nacional/Pablo_Iglesias-Informativos_Telecinco-entrevista-Pedro_Piqueras_2_1945155182.html',
43         'only_matching': True,
44     }, {
45         'url': 'http://www.telecinco.es/espanasinirmaslejos/Espana-gran-destino-turistico_2_1240605043.html',
46         'only_matching': True,
47     }]
48
49     def _real_extract(self, url):
50         episode = self._match_id(url)
51         webpage = self._download_webpage(url, episode)
52         embed_data_json = self._search_regex(
53             r'(?s)MSV\.embedData\[.*?\]\s*=\s*({.*?});', webpage, 'embed data',
54         ).replace('\'', '"')
55         embed_data = json.loads(embed_data_json)
56
57         domain = embed_data['mediaUrl']
58         if not domain.startswith('http'):
59             # only happens in telecinco.es videos
60             domain = 'http://' + domain
61         info_url = compat_urlparse.urljoin(
62             domain,
63             compat_urllib_parse_unquote(embed_data['flashvars']['host'])
64         )
65         info_el = self._download_xml(info_url, episode).find('./video/info')
66
67         video_link = info_el.find('videoUrl/link').text
68         token_query = compat_urllib_parse.urlencode({'id': video_link})
69         token_info = self._download_json(
70             embed_data['flashvars']['ov_tk'] + '?' + token_query,
71             episode,
72             transform_source=strip_jsonp
73         )
74         formats = self._extract_m3u8_formats(
75             token_info['tokenizedUrl'], episode, ext='mp4', entry_protocol='m3u8_native')
76
77         return {
78             'id': embed_data['videoId'],
79             'display_id': episode,
80             'title': info_el.find('title').text,
81             'formats': formats,
82             'description': get_element_by_attribute('class', 'text', webpage),
83             'thumbnail': info_el.find('thumb').text,
84             'duration': parse_duration(info_el.find('duration').text),
85         }