From 9fed65b4695f0f018dd897c2597877e67ec8a992 Mon Sep 17 00:00:00 2001 From: Prilly Date: Sun, 22 Mar 2026 16:53:20 +0100 Subject: [PATCH] Initial creation Python script for threeway merge, called from runner action script --- scripts/merge_blocklists.py | 49 +++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 scripts/merge_blocklists.py diff --git a/scripts/merge_blocklists.py b/scripts/merge_blocklists.py new file mode 100644 index 0000000..3eb9efb --- /dev/null +++ b/scripts/merge_blocklists.py @@ -0,0 +1,49 @@ +import urllib.request +import os + +files = { + "blacklist": "https://raw.githubusercontent.com/Cleanuparr/Cleanuparr/main/blacklist", + "blacklist_permissive": "https://raw.githubusercontent.com/Cleanuparr/Cleanuparr/main/blacklist_permissive", + "whitelist": "https://raw.githubusercontent.com/Cleanuparr/Cleanuparr/main/whitelist", + "whitelist_with_subtitles": "https://raw.githubusercontent.com/Cleanuparr/Cleanuparr/main/whitelist_with_subtitles", +} + +def merge_blocklist(filename, url): + prev_file = f"{filename}.prev" + + # Fetch new upstream + with urllib.request.urlopen(url) as r: + upstream_new = set(line.strip() for line in r.read().decode().splitlines() if line.strip()) + + # Read previous upstream (empty set if first run) + try: + with open(prev_file) as f: + upstream_prev = set(line.strip() for line in f if line.strip()) + except FileNotFoundError: + upstream_prev = upstream_new.copy() + + # Read current local file + try: + with open(filename) as f: + local = set(line.strip() for line in f if line.strip()) + except FileNotFoundError: + local = set() + + # Three-way merge + custom = local - upstream_prev + result = upstream_new | custom + + print(f"[{filename}] Custom preserved: {sorted(custom)}") + print(f"[{filename}] Upstream added: {sorted(upstream_new - upstream_prev)}") + print(f"[{filename}] Upstream removed: {sorted(upstream_prev - upstream_new)}") + + # Write merged result sorted + with open(filename, "w") as f: + f.write("\n".join(sorted(result)) + "\n") + + # Store new upstream as prev for next run + with open(prev_file, "w") as f: + f.write("\n".join(sorted(upstream_new)) + "\n") + +for filename, url in files.items(): + merge_blocklist(filename, url) \ No newline at end of file