81d6d36d325eb0af5c03a6e93536c34ae2da5bce
[youtube-dl] / youtube_dl / extractor / globo.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import base64
5 import hashlib
6 import json
7 import random
8 import re
9
10 from .common import InfoExtractor
11 from ..compat import (
12     compat_HTTPError,
13     compat_str,
14 )
15 from ..utils import (
16     ExtractorError,
17     float_or_none,
18     int_or_none,
19     orderedSet,
20     str_or_none,
21 )
22
23
24 class GloboIE(InfoExtractor):
25     _VALID_URL = r'(?:globo:|https?://.+?\.globo\.com/(?:[^/]+/)*(?:v/(?:[^/]+/)?|videos/))(?P<id>\d{7,})'
26     _LOGGED_IN = False
27     _NETRC_MACHINE = 'globo'
28     _TESTS = [{
29         'url': 'http://g1.globo.com/carros/autoesporte/videos/t/exclusivos-do-g1/v/mercedes-benz-gla-passa-por-teste-de-colisao-na-europa/3607726/',
30         'md5': 'b3ccc801f75cd04a914d51dadb83a78d',
31         'info_dict': {
32             'id': '3607726',
33             'ext': 'mp4',
34             'title': 'Mercedes-Benz GLA passa por teste de colisão na Europa',
35             'duration': 103.204,
36             'uploader': 'Globo.com',
37             'uploader_id': '265',
38         },
39     }, {
40         'url': 'http://globoplay.globo.com/v/4581987/',
41         'md5': 'f36a1ecd6a50da1577eee6dd17f67eff',
42         'info_dict': {
43             'id': '4581987',
44             'ext': 'mp4',
45             'title': 'Acidentes de trânsito estão entre as maiores causas de queda de energia em SP',
46             'duration': 137.973,
47             'uploader': 'Rede Globo',
48             'uploader_id': '196',
49         },
50     }, {
51         'url': 'http://canalbrasil.globo.com/programas/sangue-latino/videos/3928201.html',
52         'only_matching': True,
53     }, {
54         'url': 'http://globosatplay.globo.com/globonews/v/4472924/',
55         'only_matching': True,
56     }, {
57         'url': 'http://globotv.globo.com/t/programa/v/clipe-sexo-e-as-negas-adeus/3836166/',
58         'only_matching': True,
59     }, {
60         'url': 'http://globotv.globo.com/canal-brasil/sangue-latino/t/todos-os-videos/v/ator-e-diretor-argentino-ricado-darin-fala-sobre-utopias-e-suas-perdas/3928201/',
61         'only_matching': True,
62     }, {
63         'url': 'http://canaloff.globo.com/programas/desejar-profundo/videos/4518560.html',
64         'only_matching': True,
65     }, {
66         'url': 'globo:3607726',
67         'only_matching': True,
68     }]
69
70     def _real_initialize(self):
71         if self._LOGGED_IN:
72             return
73
74         email, password = self._get_login_info()
75         if email is None:
76             return
77
78         try:
79             self._download_json(
80                 'https://login.globo.com/api/authentication', None, data=json.dumps({
81                     'payload': {
82                         'email': email,
83                         'password': password,
84                         'serviceId': 4654,
85                     },
86                 }).encode(), headers={
87                     'Content-Type': 'application/json; charset=utf-8',
88                 })
89         except ExtractorError as e:
90             if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401:
91                 resp = self._parse_json(e.cause.read(), None)
92                 raise ExtractorError(resp.get('userMessage') or resp['id'], expected=True)
93             raise
94         self._LOGGED_IN = True
95
96     def _real_extract(self, url):
97         video_id = self._match_id(url)
98
99         video = self._download_json(
100             'http://api.globovideos.com/videos/%s/playlist' % video_id,
101             video_id)['videos'][0]
102
103         title = video['title']
104
105         formats = []
106         for resource in video['resources']:
107             resource_id = resource.get('_id')
108             resource_url = resource.get('url')
109             if not resource_id or not resource_url:
110                 continue
111
112             security = self._download_json(
113                 'http://security.video.globo.com/videos/%s/hash' % video_id,
114                 video_id, 'Downloading security hash for %s' % resource_id, query={
115                     'player': 'flash',
116                     'version': '17.0.0.132',
117                     'resource_id': resource_id,
118                 })
119
120             security_hash = security.get('hash')
121             if not security_hash:
122                 message = security.get('message')
123                 if message:
124                     raise ExtractorError(
125                         '%s returned error: %s' % (self.IE_NAME, message), expected=True)
126                 continue
127
128             hash_code = security_hash[:2]
129             received_time = security_hash[2:12]
130             received_random = security_hash[12:22]
131             received_md5 = security_hash[22:]
132
133             sign_time = compat_str(int(received_time) + 86400)
134             padding = '%010d' % random.randint(1, 10000000000)
135
136             md5_data = (received_md5 + sign_time + padding + '0xFF01DD').encode()
137             signed_md5 = base64.urlsafe_b64encode(hashlib.md5(md5_data).digest()).decode().strip('=')
138             signed_hash = hash_code + received_time + received_random + sign_time + padding + signed_md5
139
140             signed_url = '%s?h=%s&k=%s' % (resource_url, signed_hash, 'flash')
141             if resource_id.endswith('m3u8') or resource_url.endswith('.m3u8'):
142                 formats.extend(self._extract_m3u8_formats(
143                     signed_url, resource_id, 'mp4', entry_protocol='m3u8_native',
144                     m3u8_id='hls', fatal=False))
145             elif resource_id.endswith('mpd') or resource_url.endswith('.mpd'):
146                 formats.extend(self._extract_mpd_formats(
147                     signed_url, resource_id, mpd_id='dash', fatal=False))
148             elif resource_id.endswith('manifest') or resource_url.endswith('/manifest'):
149                 formats.extend(self._extract_ism_formats(
150                     signed_url, resource_id, ism_id='mss', fatal=False))
151             else:
152                 formats.append({
153                     'url': signed_url,
154                     'format_id': 'http-%s' % resource_id,
155                     'height': int_or_none(resource.get('height')),
156                 })
157
158         self._sort_formats(formats)
159
160         duration = float_or_none(video.get('duration'), 1000)
161         uploader = video.get('channel')
162         uploader_id = str_or_none(video.get('channel_id'))
163
164         return {
165             'id': video_id,
166             'title': title,
167             'duration': duration,
168             'uploader': uploader,
169             'uploader_id': uploader_id,
170             'formats': formats
171         }
172
173
174 class GloboArticleIE(InfoExtractor):
175     _VALID_URL = r'https?://.+?\.globo\.com/(?:[^/]+/)*(?P<id>[^/.]+)(?:\.html)?'
176
177     _VIDEOID_REGEXES = [
178         r'\bdata-video-id=["\'](\d{7,})',
179         r'\bdata-player-videosids=["\'](\d{7,})',
180         r'\bvideosIDs\s*:\s*["\']?(\d{7,})',
181         r'\bdata-id=["\'](\d{7,})',
182         r'<div[^>]+\bid=["\'](\d{7,})',
183     ]
184
185     _TESTS = [{
186         'url': 'http://g1.globo.com/jornal-nacional/noticia/2014/09/novidade-na-fiscalizacao-de-bagagem-pela-receita-provoca-discussoes.html',
187         'info_dict': {
188             'id': 'novidade-na-fiscalizacao-de-bagagem-pela-receita-provoca-discussoes',
189             'title': 'Novidade na fiscalização de bagagem pela Receita provoca discussões',
190             'description': 'md5:c3c4b4d4c30c32fce460040b1ac46b12',
191         },
192         'playlist_count': 1,
193     }, {
194         'url': 'http://g1.globo.com/pr/parana/noticia/2016/09/mpf-denuncia-lula-marisa-e-mais-seis-na-operacao-lava-jato.html',
195         'info_dict': {
196             'id': 'mpf-denuncia-lula-marisa-e-mais-seis-na-operacao-lava-jato',
197             'title': "Lula era o 'comandante máximo' do esquema da Lava Jato, diz MPF",
198             'description': 'md5:8aa7cc8beda4dc71cc8553e00b77c54c',
199         },
200         'playlist_count': 6,
201     }, {
202         'url': 'http://gq.globo.com/Prazeres/Poder/noticia/2015/10/all-o-desafio-assista-ao-segundo-capitulo-da-serie.html',
203         'only_matching': True,
204     }, {
205         'url': 'http://gshow.globo.com/programas/tv-xuxa/O-Programa/noticia/2014/01/xuxa-e-junno-namoram-muuuito-em-luau-de-zeze-di-camargo-e-luciano.html',
206         'only_matching': True,
207     }, {
208         'url': 'http://oglobo.globo.com/rio/a-amizade-entre-um-entregador-de-farmacia-um-piano-19946271',
209         'only_matching': True,
210     }]
211
212     @classmethod
213     def suitable(cls, url):
214         return False if GloboIE.suitable(url) else super(GloboArticleIE, cls).suitable(url)
215
216     def _real_extract(self, url):
217         display_id = self._match_id(url)
218         webpage = self._download_webpage(url, display_id)
219         video_ids = []
220         for video_regex in self._VIDEOID_REGEXES:
221             video_ids.extend(re.findall(video_regex, webpage))
222         entries = [
223             self.url_result('globo:%s' % video_id, GloboIE.ie_key())
224             for video_id in orderedSet(video_ids)]
225         title = self._og_search_title(webpage, fatal=False)
226         description = self._html_search_meta('description', webpage)
227         return self.playlist_result(entries, display_id, title, description)