[youtube] fix extraction for embed restricted live streams(fixes #16433)
[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': 1080,
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     }
84
85     def _real_extract(self, url):
86         video_id = self._match_id(url)
87         media_id = None
88
89         if video_id.isdigit():
90             media_id = video_id
91
92         if not media_id:
93             embed_page = self._download_webpage(
94                 'https://jsuol.com.br/c/tv/uol/embed/?params=[embed,%s]' % video_id,
95                 video_id, 'Downloading embed page', fatal=False)
96             if embed_page:
97                 media_id = self._search_regex(
98                     (r'uol\.com\.br/(\d+)', r'mediaId=(\d+)'),
99                     embed_page, 'media id', default=None)
100
101         if not media_id:
102             webpage = self._download_webpage(url, video_id)
103             media_id = self._search_regex(r'mediaId=(\d+)', webpage, 'media id')
104
105         video_data = self._download_json(
106             'http://mais.uol.com.br/apiuol/v3/player/getMedia/%s.json' % media_id,
107             media_id)['item']
108         title = video_data['title']
109
110         query = {
111             'ver': video_data.get('numRevision', 2),
112             'r': 'http://mais.uol.com.br',
113         }
114         formats = []
115         for f in video_data.get('formats', []):
116             f_url = f.get('url') or f.get('secureUrl')
117             if not f_url:
118                 continue
119             format_id = str_or_none(f.get('id'))
120             fmt = {
121                 'format_id': format_id,
122                 'url': update_url_query(f_url, query),
123             }
124             fmt.update(self._FORMATS.get(format_id, {}))
125             formats.append(fmt)
126         self._sort_formats(formats)
127
128         tags = []
129         for tag in video_data.get('tags', []):
130             tag_description = tag.get('description')
131             if not tag_description:
132                 continue
133             tags.append(tag_description)
134
135         return {
136             'id': media_id,
137             'title': title,
138             'description': clean_html(video_data.get('desMedia')),
139             'thumbnail': video_data.get('thumbnail'),
140             'duration': int_or_none(video_data.get('durationSeconds')) or parse_duration(video_data.get('duration')),
141             'tags': tags,
142             'formats': formats,
143         }