From 2a50a686df3ab8d8e6bc5e211709e476ea21a395 Mon Sep 17 00:00:00 2001 From: db0 Date: Thu, 14 Sep 2023 17:28:45 +0200 Subject: [PATCH] feat: show only diff. Protect against many changes --- examples/update_blacklist.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/examples/update_blacklist.py b/examples/update_blacklist.py index 04c517e..e9f2292 100644 --- a/examples/update_blacklist.py +++ b/examples/update_blacklist.py @@ -1,5 +1,6 @@ -import requests import os +import json +import sys from dotenv import load_dotenv from pythorhead import Lemmy from pythonseer import Fediseer @@ -37,6 +38,8 @@ trusted_instances = [ reason_filters = [] # If you want to only censure instances which have been marked by more than 1 trusted instance, then increase the number below min_censures = 1 +# Change this to the number of changes over which this script will ask for manual confirmation +changes_warning_threshold = 10 password = os.getenv("LEMMY_PASSWORD", PASSWORD) lemmy = Lemmy(f"https://{LEMMY_DOMAIN}") @@ -60,6 +63,26 @@ censures = fediseer.censure.get_given( format = FormatType.LIST, ) defed = (set(blacklist) | set(censures["domains"]) | set(sus["domains"])) - set(whitelist) +try: + with open("previous_defed.json", 'r') as file: + prev_defed = set(json.loads(file.read())) +except FileNotFoundError: + prev_defed = set() +if len(defed - prev_defed) > 0: + print(f"New blocks to add {defed - prev_defed}") +if len(prev_defed - defed) > 0: + print(f"blocks that will be removed {prev_defed - defed}") +if len(defed - prev_defed) + len(prev_defed - defed) == 0: + print("No changes to do in blocklist") + sys.exit() +if len(defed - prev_defed) + len(prev_defed - defed) >= changes_warning_threshold: + if input( + f"\n### WARNING ###\n" + f"You are about to do {changes_warning_threshold} of more changes to your blocklist. " + "Proceed? (Y/n)", + ) not in ["y", "Y", "", "yes"]: + print("Aborting run") + sys.exit() # I need to retrieve the site info because it seems if "RequireApplication" is set # We need to always re-set the application_question. # So we retrieve it from the existing site, to set the same value @@ -78,3 +101,6 @@ else: blocked_instances=list(defed), ) print("Edit Successful") +with open("previous_defed.json", 'w') as file: + file.write(json.dumps(list(defed),indent=4)) +print("Stored previous defed list to disk file 'previous_defed.json'")