1
0
mirror of https://github.com/vikstrous/pirate-get synced 2025-01-09 09:59:51 +01:00

add a custom message for cloudflare protected mirrors

This commit is contained in:
Michele Guerini Rocco 2020-03-31 01:14:38 +02:00
parent 850c88b398
commit a32b3742e0
Signed by: rnhmjoj
GPG Key ID: BFBAF4C975F76450

View File

@ -163,7 +163,15 @@ def remote(printer, pages, category, sort, mode, terms, mirror):
req = request.Request(mirror + path,
headers=pirate.data.default_headers)
req.add_header('Accept-encoding', 'gzip')
f = opener.open(req, timeout=pirate.data.default_timeout)
try:
f = opener.open(req, timeout=pirate.data.default_timeout)
except urllib.error.URLError as e:
res = e.fp.read().decode()
if e.code == 503 and 'cf-browser-verification' in res:
raise IOError('Cloudflare protected')
raise e
if f.info().get('Content-Encoding') == 'gzip':
f = gzip.GzipFile(fileobj=BytesIO(f.read()))
res = f.read().decode('utf-8')