Files
blocklists/scripts/merge_blocklists.py
T
CodeX 9fed65b469 Initial creation
Python script for threeway merge, called from runner action script
2026-03-22 16:53:20 +01:00

49 lines
1.8 KiB
Python

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)