From bc5e4f0544c3f5f44798c7f2268b81be57b38e43 Mon Sep 17 00:00:00 2001 From: Simon R Date: Wed, 22 May 2019 10:15:08 +0200 Subject: [PATCH] make generate-domains-blacklist.py compatible to both python2 and python3 (#828) * update domains-blacklist-all.conf: Quidsup NoTrack moved to gitlab * make generate-domains-blacklist.py python3 compatible * fix whitespace --- .../generate-domains-blacklist.py | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/utils/generate-domains-blacklists/generate-domains-blacklist.py b/utils/generate-domains-blacklists/generate-domains-blacklist.py index 657c1d06..7343198f 100755 --- a/utils/generate-domains-blacklists/generate-domains-blacklist.py +++ b/utils/generate-domains-blacklists/generate-domains-blacklist.py @@ -5,8 +5,14 @@ import argparse import re import sys -import urllib2 +try: + import urllib2 as urllib + URLLIB_NEW = False +except (ImportError, ModuleNotFoundError): + import urllib.request as urllib + from urllib.request import Request + URLLIB_NEW = True def parse_time_restricted_list(content): rx_comment = re.compile(r'^(#|$)') @@ -81,19 +87,27 @@ def print_restricted_name(name, time_restrictions): def load_from_url(url): sys.stderr.write("Loading data from [{}]\n".format(url)) - req = urllib2.Request(url) + req = urllib.Request(url) trusted = False - if req.get_type() == "file": + + if URLLIB_NEW: + req_type = req.type + else: + req_type = req.get_type() + if req_type == "file": trusted = True + response = None try: - response = urllib2.urlopen(req, timeout=int(args.timeout)) + response = urllib.urlopen(req, timeout=int(args.timeout)) except urllib2.URLError as err: raise Exception("[{}] could not be loaded: {}\n".format(url, err)) if trusted is False and response.getcode() != 200: raise Exception("[{}] returned HTTP code {}\n".format( url, response.getcode())) content = response.read() + if URLLIB_NEW: + content = content.decode('utf-8', errors='replace') return (content, trusted)