From a32b3742e070ced933577f7dba9046078c556722 Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Tue, 31 Mar 2020 01:14:38 +0200 Subject: [PATCH] add a custom message for cloudflare protected mirrors --- pirate/torrent.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pirate/torrent.py b/pirate/torrent.py index 6e3acc6..4798c4c 100644 --- a/pirate/torrent.py +++ b/pirate/torrent.py @@ -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')