Files
pihole/generate_collections.py
T
2026-08-28 15:52:32 +02:00

48 lines
1.7 KiB
Python

import os
files = os.listdir("./lists")
files = [f for f in files if f.endswith(".txt") and not f.startswith("0_collection_") and not "template" in f]
allows = [f for f in files if "_allowed" in f]
disallows = [f for f in files if "_disallowed" in f]
allow_lines = []
disallow_lines = []
for f in allows:
with open(f"./lists/{f}", mode="r", encoding="utf-8") as file: content = file.readlines()
allow_lines.extend([l.strip() for l in content if l.startswith("0.0.0.0 ")])
for f in disallows:
with open(f"./lists/{f}", mode="r", encoding="utf-8") as file: content = file.readlines()
disallow_lines.extend([l.strip() for l in content if l.startswith("0.0.0.0 ")])
allow_lines = sorted(set(allow_lines))
disallow_lines = sorted(set(disallow_lines))
allow_count = len(allow_lines)
disallow_cont = len(disallow_lines)
print(f"Allows: {allow_count}\nDisallows: {disallow_cont}")
allow_header = f"""# Title: Collection of all allowed domains from https://gitlab.com/DasMoorhuhn/pihole
# Description: ...
# Syntax: Hosts (including possible subdomains)
# Number of entries: {allow_count}
# ALLOW
#
"""
disallow_header = f"""# Title: Collection of all disallowed domains from https://gitlab.com/DasMoorhuhn/pihole
# Description: ...
# Syntax: Hosts (including possible subdomains)
# Number of entries: {disallow_cont}
# DISALLOW
#
"""
allow_final = allow_header + "\n".join(allow_lines)
disallow_final = disallow_header + "\n".join(disallow_lines)
with open(file="./lists/0_collection_allowed_domains.txt", mode="w", encoding="utf-8") as file: file.write(allow_final)
with open(file="./lists/0_collection_disallowed_domains.txt", mode="w", encoding="utf-8") as file: file.write(disallow_final)
print("Done")