]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/globo.py
[ie/youtube] Suppress "Unavailable videos are hidden" warning (#10159)
[yt-dlp.git] / yt_dlp / extractor / globo.py
CommitLineData
db2058f6
RA
1import base64
2import hashlib
3import json
f47754f0 4import random
26394d02 5import re
f47754f0
S
6
7from .common import InfoExtractor
3d2623a8 8from ..networking import HEADRequest
8c25f81b
PH
9from ..utils import (
10 ExtractorError,
11 float_or_none,
26394d02 12 orderedSet,
e7d34c03 13 str_or_none,
b89378a6 14 try_get,
8c25f81b 15)
f47754f0
S
16
17
18class GloboIE(InfoExtractor):
25042f73 19 _VALID_URL = r'(?:globo:|https?://.+?\.globo\.com/(?:[^/]+/)*(?:v/(?:[^/]+/)?|videos/))(?P<id>\d{7,})'
d81ffc3a 20 _NETRC_MACHINE = 'globo'
ad607563 21 _TESTS = [{
ad607563 22 'url': 'http://g1.globo.com/carros/autoesporte/videos/t/exclusivos-do-g1/v/mercedes-benz-gla-passa-por-teste-de-colisao-na-europa/3607726/',
ad607563
S
23 'info_dict': {
24 'id': '3607726',
25 'ext': 'mp4',
26 'title': 'Mercedes-Benz GLA passa por teste de colisão na Europa',
27 'duration': 103.204,
b89378a6
AG
28 'uploader': 'G1',
29 'uploader_id': '2015',
30 },
31 'params': {
32 'skip_download': True,
264cd00f 33 },
ad607563 34 }, {
264cd00f 35 'url': 'http://globoplay.globo.com/v/4581987/',
ad607563 36 'info_dict': {
264cd00f 37 'id': '4581987',
ad607563 38 'ext': 'mp4',
264cd00f
S
39 'title': 'Acidentes de trânsito estão entre as maiores causas de queda de energia em SP',
40 'duration': 137.973,
41 'uploader': 'Rede Globo',
e7d34c03 42 'uploader_id': '196',
264cd00f 43 },
b89378a6
AG
44 'params': {
45 'skip_download': True,
46 },
264cd00f
S
47 }, {
48 'url': 'http://canalbrasil.globo.com/programas/sangue-latino/videos/3928201.html',
49 'only_matching': True,
50 }, {
51 'url': 'http://globosatplay.globo.com/globonews/v/4472924/',
52 'only_matching': True,
53 }, {
54 'url': 'http://globotv.globo.com/t/programa/v/clipe-sexo-e-as-negas-adeus/3836166/',
55 'only_matching': True,
56 }, {
57 '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/',
58 'only_matching': True,
5d501a09
S
59 }, {
60 'url': 'http://canaloff.globo.com/programas/desejar-profundo/videos/4518560.html',
61 'only_matching': True,
26394d02
S
62 }, {
63 'url': 'globo:3607726',
64 'only_matching': True,
63c3ee4f
B
65 }, {
66 'url': 'https://globoplay.globo.com/v/10248083/',
67 'info_dict': {
68 'id': '10248083',
69 'ext': 'mp4',
70 'title': 'Melhores momentos: Equador 1 x 1 Brasil pelas Eliminatórias da Copa do Mundo 2022',
71 'duration': 530.964,
72 'uploader': 'SporTV',
73 'uploader_id': '698',
74 },
75 'params': {
76 'skip_download': True,
77 },
ad607563 78 }]
f47754f0 79
f47754f0
S
80 def _real_extract(self, url):
81 video_id = self._match_id(url)
82
63c3ee4f
B
83 self._request_webpage(
84 HEADRequest('https://globo-ab.globo.com/v2/selected-alternatives?experiments=player-isolated-experiment-02&skipImpressions=true'),
85 video_id, 'Getting cookies')
86
f47754f0 87 video = self._download_json(
add96eb9 88 f'http://api.globovideos.com/videos/{video_id}/playlist',
db2058f6 89 video_id)['videos'][0]
a06916d9 90 if not self.get_param('allow_unplayable_formats') and video.get('encrypted') is True:
88acdbc2 91 self.report_drm(video_id)
f47754f0
S
92
93 title = video['title']
f47754f0
S
94
95 formats = []
b89378a6 96 security = self._download_json(
add96eb9 97 'https://playback.video.globo.com/v2/video-session', video_id, f'Downloading security hash for {video_id}',
b89378a6 98 headers={'content-type': 'application/json'}, data=json.dumps({
add96eb9 99 'player_type': 'desktop',
100 'video_id': video_id,
101 'quality': 'max',
102 'content_protection': 'widevine',
103 'vsid': '581b986b-4c40-71f0-5a58-803e579d5fa2',
104 'tz': '-3.0:00',
b89378a6
AG
105 }).encode())
106
63c3ee4f
B
107 self._request_webpage(HEADRequest(security['sources'][0]['url_template']), video_id, 'Getting locksession cookie')
108
109 security_hash = security['sources'][0]['token']
b89378a6
AG
110 if not security_hash:
111 message = security.get('message')
112 if message:
113 raise ExtractorError(
add96eb9 114 f'{self.IE_NAME} returned error: {message}', expected=True)
b89378a6
AG
115
116 hash_code = security_hash[:2]
117 padding = '%010d' % random.randint(1, 10000000000)
118 if hash_code in ('04', '14'):
119 received_time = security_hash[3:13]
120 received_md5 = security_hash[24:]
121 hash_prefix = security_hash[:23]
122 elif hash_code in ('02', '12', '03', '13'):
123 received_time = security_hash[2:12]
124 received_md5 = security_hash[22:]
125 padding += '1'
126 hash_prefix = '05' + security_hash[:22]
127
add96eb9 128 padded_sign_time = str(int(received_time) + 86400) + padding
b89378a6
AG
129 md5_data = (received_md5 + padded_sign_time + '0xAC10FD').encode()
130 signed_md5 = base64.urlsafe_b64encode(hashlib.md5(md5_data).digest()).decode().strip('=')
131 signed_hash = hash_prefix + padded_sign_time + signed_md5
63c3ee4f 132 source = security['sources'][0]['url_parts']
b89378a6 133 resource_url = source['scheme'] + '://' + source['domain'] + source['path']
add96eb9 134 signed_url = '{}?h={}&k=html5&a={}'.format(resource_url, signed_hash, 'F' if video.get('subscriber_only') else 'A')
b89378a6 135
3f047fc4
F
136 fmts, subtitles = self._extract_m3u8_formats_and_subtitles(
137 signed_url, video_id, 'mp4', entry_protocol='m3u8_native', m3u8_id='hls', fatal=False)
138 formats.extend(fmts)
b89378a6 139
f47754f0 140 for resource in video['resources']:
b89378a6 141 if resource.get('type') == 'subtitle':
30eb05cb 142 subtitles.setdefault(resource.get('language') or 'por', []).append({
b89378a6 143 'url': resource.get('url'),
30eb05cb 144 })
b89378a6
AG
145 subs = try_get(security, lambda x: x['source']['subtitles'], expected_type=dict) or {}
146 for sub_lang, sub_url in subs.items():
147 if sub_url:
148 subtitles.setdefault(sub_lang or 'por', []).append({
149 'url': sub_url,
db2058f6 150 })
b89378a6
AG
151 subs = try_get(security, lambda x: x['source']['subtitles_webvtt'], expected_type=dict) or {}
152 for sub_lang, sub_url in subs.items():
153 if sub_url:
154 subtitles.setdefault(sub_lang or 'por', []).append({
155 'url': sub_url,
8c72beb2 156 })
f47754f0 157
fffccaaf 158 duration = float_or_none(video.get('duration'), 1000)
fffccaaf 159 uploader = video.get('channel')
e7d34c03 160 uploader_id = str_or_none(video.get('channel_id'))
fffccaaf 161
f47754f0
S
162 return {
163 'id': video_id,
164 'title': title,
165 'duration': duration,
166 'uploader': uploader,
167 'uploader_id': uploader_id,
30eb05cb
RA
168 'formats': formats,
169 'subtitles': subtitles,
5f6a1245 170 }
ad607563
S
171
172
173class GloboArticleIE(InfoExtractor):
26394d02 174 _VALID_URL = r'https?://.+?\.globo\.com/(?:[^/]+/)*(?P<id>[^/.]+)(?:\.html)?'
ad607563
S
175
176 _VIDEOID_REGEXES = [
c40f327a
B
177 r'\bdata-video-id=["\'](\d{7,})["\']',
178 r'\bdata-player-videosids=["\'](\d{7,})["\']',
9e5751b9 179 r'\bvideosIDs\s*:\s*["\']?(\d{7,})',
c40f327a
B
180 r'\bdata-id=["\'](\d{7,})["\']',
181 r'<div[^>]+\bid=["\'](\d{7,})["\']',
182 r'<bs-player[^>]+\bvideoid=["\'](\d{8,})["\']',
ad607563
S
183 ]
184
5d501a09 185 _TESTS = [{
ad607563 186 'url': 'http://g1.globo.com/jornal-nacional/noticia/2014/09/novidade-na-fiscalizacao-de-bagagem-pela-receita-provoca-discussoes.html',
ad607563 187 'info_dict': {
26394d02
S
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,
5d501a09
S
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,
9e5751b9
S
207 }, {
208 'url': 'http://oglobo.globo.com/rio/a-amizade-entre-um-entregador-de-farmacia-um-piano-19946271',
209 'only_matching': True,
17b18388
B
210 }, {
211 'url': 'https://ge.globo.com/video/ta-na-area-como-foi-assistir-ao-jogo-do-palmeiras-que-a-globo-nao-passou-10287094.ghtml',
212 'info_dict': {
213 'id': 'ta-na-area-como-foi-assistir-ao-jogo-do-palmeiras-que-a-globo-nao-passou-10287094',
214 'title': 'Tá na Área: como foi assistir ao jogo do Palmeiras que a Globo não passou',
215 'description': 'md5:2d089d036c4c9675117d3a56f8c61739',
216 },
217 'playlist_count': 1,
c40f327a
B
218 }, {
219 'url': 'https://redeglobo.globo.com/rpc/meuparana/noticia/a-producao-de-chocolates-no-parana.ghtml',
220 'info_dict': {
221 'id': 'a-producao-de-chocolates-no-parana',
222 'title': 'A produção de chocolates no Paraná',
223 'description': 'md5:f2e3daf00ffd1dc0e9a8a6c7cfb0a89e',
224 },
225 'playlist_count': 2,
5d501a09 226 }]
ad607563
S
227
228 @classmethod
229 def suitable(cls, url):
add96eb9 230 return False if GloboIE.suitable(url) else super().suitable(url)
ad607563
S
231
232 def _real_extract(self, url):
233 display_id = self._match_id(url)
234 webpage = self._download_webpage(url, display_id)
26394d02
S
235 video_ids = []
236 for video_regex in self._VIDEOID_REGEXES:
237 video_ids.extend(re.findall(video_regex, webpage))
238 entries = [
add96eb9 239 self.url_result(f'globo:{video_id}', GloboIE.ie_key())
26394d02 240 for video_id in orderedSet(video_ids)]
c40f327a 241 title = self._og_search_title(webpage).strip()
26394d02
S
242 description = self._html_search_meta('description', webpage)
243 return self.playlist_result(entries, display_id, title, description)