[compat] Add compat_urllib_parse_urlencode and eliminate encode_dict
[youtube-dl] / youtube_dl / extractor / mitele.py
1 from __future__ import unicode_literals
2
3 from .common import InfoExtractor
4 from ..compat import (
5     compat_urllib_parse_urlencode,
6     compat_urlparse,
7 )
8 from ..utils import (
9     get_element_by_attribute,
10     int_or_none,
11 )
12
13
14 class MiTeleIE(InfoExtractor):
15     IE_DESC = 'mitele.es'
16     _VALID_URL = r'https?://www\.mitele\.es/[^/]+/[^/]+/[^/]+/(?P<id>[^/]+)/'
17
18     _TESTS = [{
19         'url': 'http://www.mitele.es/programas-tv/diario-de/la-redaccion/programa-144/',
20         'md5': '0ff1a13aebb35d9bc14081ff633dd324',
21         'info_dict': {
22             'id': '0NF1jJnxS1Wu3pHrmvFyw2',
23             'display_id': 'programa-144',
24             'ext': 'flv',
25             'title': 'Tor, la web invisible',
26             'description': 'md5:3b6fce7eaa41b2d97358726378d9369f',
27             'thumbnail': 're:(?i)^https?://.*\.jpg$',
28             'duration': 2913,
29         },
30     }]
31
32     def _real_extract(self, url):
33         display_id = self._match_id(url)
34
35         webpage = self._download_webpage(url, display_id)
36
37         config_url = self._search_regex(
38             r'data-config\s*=\s*"([^"]+)"', webpage, 'data config url')
39         config_url = compat_urlparse.urljoin(url, config_url)
40
41         config = self._download_json(
42             config_url, display_id, 'Downloading config JSON')
43
44         mmc = self._download_json(
45             config['services']['mmc'], display_id, 'Downloading mmc JSON')
46
47         formats = []
48         for location in mmc['locations']:
49             gat = self._proto_relative_url(location.get('gat'), 'http:')
50             bas = location.get('bas')
51             loc = location.get('loc')
52             ogn = location.get('ogn')
53             if None in (gat, bas, loc, ogn):
54                 continue
55             token_data = {
56                 'bas': bas,
57                 'icd': loc,
58                 'ogn': ogn,
59                 'sta': '0',
60             }
61             media = self._download_json(
62                 '%s/?%s' % (gat, compat_urllib_parse_urlencode(token_data)),
63                 display_id, 'Downloading %s JSON' % location['loc'])
64             file_ = media.get('file')
65             if not file_:
66                 continue
67             formats.extend(self._extract_f4m_formats(
68                 file_ + '&hdcore=3.2.0&plugin=aasp-3.2.0.77.18',
69                 display_id, f4m_id=loc))
70
71         title = self._search_regex(
72             r'class="Destacado-text"[^>]*>\s*<strong>([^<]+)</strong>', webpage, 'title')
73
74         video_id = self._search_regex(
75             r'data-media-id\s*=\s*"([^"]+)"', webpage,
76             'data media id', default=None) or display_id
77         thumbnail = config.get('poster', {}).get('imageUrl')
78         duration = int_or_none(mmc.get('duration'))
79
80         return {
81             'id': video_id,
82             'display_id': display_id,
83             'title': title,
84             'description': get_element_by_attribute('class', 'text', webpage),
85             'thumbnail': thumbnail,
86             'duration': duration,
87             'formats': formats,
88         }