08f0c072e28b09dfbbde2f662b52f5de4cf46f3d
[youtube-dl] / youtube_dl / extractor / uol.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     clean_html,
7     int_or_none,
8     parse_duration,
9     update_url_query,
10     str_or_none,
11 )
12
13
14 class UOLIE(InfoExtractor):
15     IE_NAME = 'uol.com.br'
16     _VALID_URL = r'https?://(?:.+?\.)?uol\.com\.br/.*?(?:(?:mediaId|v)=|view/(?:[a-z0-9]+/)?|video(?:=|/(?:\d{4}/\d{2}/\d{2}/)?))(?P<id>\d+|[\w-]+-[A-Z0-9]+)'
17     _TESTS = [{
18         'url': 'http://player.mais.uol.com.br/player_video_v3.swf?mediaId=15951931',
19         'md5': '25291da27dc45e0afb5718a8603d3816',
20         'info_dict': {
21             'id': '15951931',
22             'ext': 'mp4',
23             'title': 'Miss simpatia é encontrada morta',
24             'description': 'md5:3f8c11a0c0556d66daf7e5b45ef823b2',
25         }
26     }, {
27         'url': 'http://tvuol.uol.com.br/video/incendio-destroi-uma-das-maiores-casas-noturnas-de-londres-04024E9A3268D4C95326',
28         'md5': 'e41a2fb7b7398a3a46b6af37b15c00c9',
29         'info_dict': {
30             'id': '15954259',
31             'ext': 'mp4',
32             'title': 'Incêndio destrói uma das maiores casas noturnas de Londres',
33             'description': 'Em Londres, um incêndio destruiu uma das maiores boates da cidade. Não há informações sobre vítimas.',
34         }
35     }, {
36         'url': 'http://mais.uol.com.br/static/uolplayer/index.html?mediaId=15951931',
37         'only_matching': True,
38     }, {
39         'url': 'http://mais.uol.com.br/view/15954259',
40         'only_matching': True,
41     }, {
42         'url': 'http://noticias.band.uol.com.br/brasilurgente/video/2016/08/05/15951931/miss-simpatia-e-encontrada-morta.html',
43         'only_matching': True,
44     }, {
45         'url': 'http://videos.band.uol.com.br/programa.asp?e=noticias&pr=brasil-urgente&v=15951931&t=Policia-desmonte-base-do-PCC-na-Cracolandia',
46         'only_matching': True,
47     }, {
48         'url': 'http://mais.uol.com.br/view/cphaa0gl2x8r/incendio-destroi-uma-das-maiores-casas-noturnas-de-londres-04024E9A3268D4C95326',
49         'only_matching': True,
50     }, {
51         'url': 'http://noticias.uol.com.br//videos/assistir.htm?video=rafaela-silva-inspira-criancas-no-judo-04024D983968D4C95326',
52         'only_matching': True,
53     }, {
54         'url': 'http://mais.uol.com.br/view/e0qbgxid79uv/15275470',
55         'only_matching': True,
56     }]
57
58     _FORMATS = {
59         '2': {
60             'width': 640,
61             'height': 360,
62         },
63         '5': {
64             'width': 1280,
65             'height': 720,
66         },
67         '6': {
68             'width': 426,
69             'height': 240,
70         },
71         '7': {
72             'width': 1920,
73             'height': 1080,
74         },
75         '8': {
76             'width': 192,
77             'height': 144,
78         },
79         '9': {
80             'width': 568,
81             'height': 320,
82         },
83         '11': {
84             'width': 640,
85             'height': 360,
86         }
87     }
88
89     def _real_extract(self, url):
90         video_id = self._match_id(url)
91         media_id = None
92
93         if video_id.isdigit():
94             media_id = video_id
95
96         if not media_id:
97             embed_page = self._download_webpage(
98                 'https://jsuol.com.br/c/tv/uol/embed/?params=[embed,%s]' % video_id,
99                 video_id, 'Downloading embed page', fatal=False)
100             if embed_page:
101                 media_id = self._search_regex(
102                     (r'uol\.com\.br/(\d+)', r'mediaId=(\d+)'),
103                     embed_page, 'media id', default=None)
104
105         if not media_id:
106             webpage = self._download_webpage(url, video_id)
107             media_id = self._search_regex(r'mediaId=(\d+)', webpage, 'media id')
108
109         video_data = self._download_json(
110             'http://mais.uol.com.br/apiuol/v3/player/getMedia/%s.json' % media_id,
111             media_id)['item']
112         title = video_data['title']
113
114         query = {
115             'ver': video_data.get('numRevision', 2),
116             'r': 'http://mais.uol.com.br',
117         }
118         for k in ('token', 'sign'):
119             v = video_data.get(k)
120             if v:
121                 query[k] = v
122
123         formats = []
124         for f in video_data.get('formats', []):
125             f_url = f.get('url') or f.get('secureUrl')
126             if not f_url:
127                 continue
128             f_url = update_url_query(f_url, query)
129             format_id = str_or_none(f.get('id'))
130             if format_id == '10':
131                 formats.extend(self._extract_m3u8_formats(
132                     f_url, video_id, 'mp4', 'm3u8_native',
133                     m3u8_id='hls', fatal=False))
134                 continue
135             fmt = {
136                 'format_id': format_id,
137                 'url': f_url,
138                 'source_preference': 1,
139             }
140             fmt.update(self._FORMATS.get(format_id, {}))
141             formats.append(fmt)
142         self._sort_formats(formats, ('height', 'width', 'source_preference', 'tbr', 'ext'))
143
144         tags = []
145         for tag in video_data.get('tags', []):
146             tag_description = tag.get('description')
147             if not tag_description:
148                 continue
149             tags.append(tag_description)
150
151         return {
152             'id': media_id,
153             'title': title,
154             'description': clean_html(video_data.get('desMedia')),
155             'thumbnail': video_data.get('thumbnail'),
156             'duration': int_or_none(video_data.get('durationSeconds')) or parse_duration(video_data.get('duration')),
157             'tags': tags,
158             'formats': formats,
159         }