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
This commit is contained in:
parent
dcce060ef2
commit
bc5e4f0544
|
@ -5,8 +5,14 @@
|
||||||
import argparse
|
import argparse
|
||||||
import re
|
import re
|
||||||
import sys
|
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):
|
def parse_time_restricted_list(content):
|
||||||
rx_comment = re.compile(r'^(#|$)')
|
rx_comment = re.compile(r'^(#|$)')
|
||||||
|
@ -81,19 +87,27 @@ def print_restricted_name(name, time_restrictions):
|
||||||
|
|
||||||
def load_from_url(url):
|
def load_from_url(url):
|
||||||
sys.stderr.write("Loading data from [{}]\n".format(url))
|
sys.stderr.write("Loading data from [{}]\n".format(url))
|
||||||
req = urllib2.Request(url)
|
req = urllib.Request(url)
|
||||||
trusted = False
|
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
|
trusted = True
|
||||||
|
|
||||||
response = None
|
response = None
|
||||||
try:
|
try:
|
||||||
response = urllib2.urlopen(req, timeout=int(args.timeout))
|
response = urllib.urlopen(req, timeout=int(args.timeout))
|
||||||
except urllib2.URLError as err:
|
except urllib2.URLError as err:
|
||||||
raise Exception("[{}] could not be loaded: {}\n".format(url, err))
|
raise Exception("[{}] could not be loaded: {}\n".format(url, err))
|
||||||
if trusted is False and response.getcode() != 200:
|
if trusted is False and response.getcode() != 200:
|
||||||
raise Exception("[{}] returned HTTP code {}\n".format(
|
raise Exception("[{}] returned HTTP code {}\n".format(
|
||||||
url, response.getcode()))
|
url, response.getcode()))
|
||||||
content = response.read()
|
content = response.read()
|
||||||
|
if URLLIB_NEW:
|
||||||
|
content = content.decode('utf-8', errors='replace')
|
||||||
|
|
||||||
return (content, trusted)
|
return (content, trusted)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue