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