Merge pull request #9288 from reyyed/issue#9063fix
[youtube-dl] / youtube_dl / extractor / mitele.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import (
8     compat_urllib_parse_urlencode,
9     compat_urlparse,
10 )
11 from ..utils import (
12     get_element_by_attribute,
13     int_or_none,
14     remove_start,
15     extract_attributes,
16     determine_ext,
17 )
18
19
20 class MiTeleBaseIE(InfoExtractor):
21     def _get_player_info(self, url, webpage):
22         player_data = extract_attributes(self._search_regex(
23             r'(?s)(<ms-video-player.+?</ms-video-player>)',
24             webpage, 'ms video player'))
25         video_id = player_data['data-media-id']
26         config_url = compat_urlparse.urljoin(url, player_data['data-config'])
27         config = self._download_json(
28             config_url, video_id, 'Downloading config JSON')
29         mmc_url = config['services']['mmc']
30
31         duration = None
32         formats = []
33         for m_url in (mmc_url, mmc_url.replace('/flash.json', '/html5.json')):
34             mmc = self._download_json(
35                 m_url, video_id, 'Downloading mmc JSON')
36             if not duration:
37                 duration = int_or_none(mmc.get('duration'))
38             for location in mmc['locations']:
39                 gat = self._proto_relative_url(location.get('gat'), 'http:')
40                 bas = location.get('bas')
41                 loc = location.get('loc')
42                 ogn = location.get('ogn')
43                 if None in (gat, bas, loc, ogn):
44                     continue
45                 token_data = {
46                     'bas': bas,
47                     'icd': loc,
48                     'ogn': ogn,
49                     'sta': '0',
50                 }
51                 media = self._download_json(
52                     '%s/?%s' % (gat, compat_urllib_parse_urlencode(token_data)),
53                     video_id, 'Downloading %s JSON' % location['loc'])
54                 file_ = media.get('file')
55                 if not file_:
56                     continue
57                 ext = determine_ext(file_)
58                 if ext == 'f4m':
59                     formats.extend(self._extract_f4m_formats(
60                         file_ + '&hdcore=3.2.0&plugin=aasp-3.2.0.77.18',
61                         video_id, f4m_id='hds', fatal=False))
62                 elif ext == 'm3u8':
63                     formats.extend(self._extract_m3u8_formats(
64                         file_, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False))
65         self._sort_formats(formats)
66
67         return {
68             'id': video_id,
69             'formats': formats,
70             'thumbnail': player_data.get('data-poster') or config.get('poster', {}).get('imageUrl'),
71             'duration': duration,
72         }
73
74
75 class MiTeleIE(MiTeleBaseIE):
76     IE_DESC = 'mitele.es'
77     _VALID_URL = r'https?://www\.mitele\.es/(?:[^/]+/){3}(?P<id>[^/]+)/'
78
79     _TESTS = [{
80         'url': 'http://www.mitele.es/programas-tv/diario-de/la-redaccion/programa-144/',
81         # MD5 is unstable
82         'info_dict': {
83             'id': '0NF1jJnxS1Wu3pHrmvFyw2',
84             'display_id': 'programa-144',
85             'ext': 'mp4',
86             'title': 'Tor, la web invisible',
87             'description': 'md5:3b6fce7eaa41b2d97358726378d9369f',
88             'series': 'Diario de',
89             'season': 'La redacción',
90             'episode': 'Programa 144',
91             'thumbnail': 're:(?i)^https?://.*\.jpg$',
92             'duration': 2913,
93         },
94     }, {
95         # no explicit title
96         'url': 'http://www.mitele.es/programas-tv/cuarto-milenio/temporada-6/programa-226/',
97         'info_dict': {
98             'id': 'eLZSwoEd1S3pVyUm8lc6F',
99             'display_id': 'programa-226',
100             'ext': 'mp4',
101             'title': 'Cuarto Milenio - Temporada 6 - Programa 226',
102             'description': 'md5:50daf9fadefa4e62d9fc866d0c015701',
103             'series': 'Cuarto Milenio',
104             'season': 'Temporada 6',
105             'episode': 'Programa 226',
106             'thumbnail': 're:(?i)^https?://.*\.jpg$',
107             'duration': 7312,
108         },
109         'params': {
110             'skip_download': True,
111         },
112     }]
113
114     def _real_extract(self, url):
115         display_id = self._match_id(url)
116
117         webpage = self._download_webpage(url, display_id)
118
119         info = self._get_player_info(url, webpage)
120
121         title = self._search_regex(
122             r'class="Destacado-text"[^>]*>\s*<strong>([^<]+)</strong>',
123             webpage, 'title', default=None)
124
125         mobj = re.search(r'''(?sx)
126                             class="Destacado-text"[^>]*>.*?<h1>\s*
127                             <span>(?P<series>[^<]+)</span>\s*
128                             <span>(?P<season>[^<]+)</span>\s*
129                             <span>(?P<episode>[^<]+)</span>''', webpage)
130         series, season, episode = mobj.groups() if mobj else [None] * 3
131
132         if not title:
133             if mobj:
134                 title = '%s - %s - %s' % (series, season, episode)
135             else:
136                 title = remove_start(self._search_regex(
137                     r'<title>([^<]+)</title>', webpage, 'title'), 'Ver online ')
138
139         info.update({
140             'display_id': display_id,
141             'title': title,
142             'description': get_element_by_attribute('class', 'text', webpage),
143             'series': series,
144             'season': season,
145             'episode': episode,
146         })
147         return info