Development
This commit is contained in:
parent
f794314f28
commit
e4a9d5c038
5 changed files with 35 additions and 15 deletions
|
|
@ -13,6 +13,7 @@ Usage:
|
|||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import urllib.request
|
||||
|
|
@ -21,6 +22,8 @@ from pathlib import Path
|
|||
SCRIPT_DIR = Path(__file__).parent
|
||||
CONFIG_FILE = SCRIPT_DIR / "config.json"
|
||||
BLOCKLIST_DIR = SCRIPT_DIR / "blocklists"
|
||||
LOG_FILE = BLOCKLIST_DIR / ".log"
|
||||
LAST_DL_FILE = BLOCKLIST_DIR / ".last-dl"
|
||||
|
||||
|
||||
def die(msg):
|
||||
|
|
@ -40,9 +43,27 @@ def load_config():
|
|||
return json.load(f)
|
||||
|
||||
|
||||
def setup_logging():
|
||||
BLOCKLIST_DIR.mkdir(exist_ok=True)
|
||||
try:
|
||||
file_handler = logging.FileHandler(LOG_FILE, mode='a')
|
||||
except PermissionError:
|
||||
print(f"WARNING: Cannot write to {LOG_FILE} -- run with sudo.")
|
||||
file_handler = None
|
||||
handlers = [logging.StreamHandler()]
|
||||
if file_handler:
|
||||
handlers.insert(0, file_handler)
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s %(levelname)-8s %(message)s",
|
||||
datefmt="%Y-%m-%d %H:%M:%S",
|
||||
handlers=handlers,
|
||||
force=True,
|
||||
)
|
||||
|
||||
|
||||
def download_blocklists(data):
|
||||
any_fail = False
|
||||
BLOCKLIST_DIR.mkdir(exist_ok=True)
|
||||
for bl in data.get("dns_blocking", {}).get("blocklists", []):
|
||||
if bl.get("bl_type") == "local":
|
||||
continue
|
||||
|
|
@ -50,32 +71,29 @@ def download_blocklists(data):
|
|||
save_as = bl.get("save_as", "")
|
||||
name = bl.get("name", "?")
|
||||
if not url or not save_as:
|
||||
print(f" Skipped '{name}': missing url or save_as")
|
||||
logging.warning("Skipped '%s': missing url or save_as", name)
|
||||
continue
|
||||
try:
|
||||
req = urllib.request.Request(url, headers={"User-Agent": "routlin/1.0"})
|
||||
with urllib.request.urlopen(req, timeout=30) as r:
|
||||
content = r.read()
|
||||
(BLOCKLIST_DIR / save_as).write_bytes(content)
|
||||
print(f" Downloaded: {name} ({len(content):,} bytes)")
|
||||
logging.info("Downloaded: %s (%s bytes)", name, f"{len(content):,}")
|
||||
except Exception as e:
|
||||
print(f" ERROR: Failed to download '{name}': {e}")
|
||||
logging.error("Failed to download '%s': %s", name, e)
|
||||
any_fail = True
|
||||
return not any_fail
|
||||
|
||||
|
||||
LAST_DL_FILE = BLOCKLIST_DIR / ".last-dl"
|
||||
|
||||
|
||||
def main():
|
||||
check_root()
|
||||
setup_logging()
|
||||
data = load_config()
|
||||
print("Downloading blocklists ==============================================")
|
||||
logging.info("Downloading blocklists ==========================================")
|
||||
success = download_blocklists(data)
|
||||
BLOCKLIST_DIR.mkdir(exist_ok=True)
|
||||
LAST_DL_FILE.write_text(str(int(__import__('time').time())))
|
||||
if not success:
|
||||
print("WARNING: One or more downloads failed.")
|
||||
logging.warning("One or more downloads failed.")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue