Created
December 17, 2024 20:08
-
-
Save sirkirby/6b094d0e30322687e15cebca6e5e0878 to your computer and use it in GitHub Desktop.
Migrate Pi-Hole adlist to Adguard DNS Blocklist Filters yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import yaml | |
import sys | |
from datetime import datetime, timezone | |
def convert_adlists(adlists_json): | |
"""Convert Pi-hole adlists to AdGuard Home filters format.""" | |
try: | |
adlists_data = json.loads(adlists_json) | |
except json.JSONDecodeError: | |
print("Error: Invalid JSON format in adlists data", file=sys.stderr) | |
sys.exit(1) | |
filters = [] | |
for i, adlist in enumerate(adlists_data, start=1): | |
# Extract relevant data from Pi-hole adlist | |
address = adlist.get('address') | |
enabled = adlist.get('enabled', 0) == 1 | |
comment = adlist.get('comment', '') | |
# Create AdGuard Home filter entry | |
filter_entry = { | |
'enabled': enabled, | |
'url': address, | |
'name': comment if comment else f"Pi-hole List {i}", | |
'id': i | |
} | |
filters.append(filter_entry) | |
return {'filters': filters} | |
def main(): | |
if len(sys.argv) != 2: | |
print("Usage: python script.py <adlists.json>") | |
sys.exit(1) | |
adlists_file = sys.argv[1] | |
# Read Pi-hole adlists | |
try: | |
with open(adlists_file, 'r') as f: | |
adlists_json = f.read() | |
except FileNotFoundError: | |
print(f"Error: Pi-hole adlists file {adlists_file} not found", file=sys.stderr) | |
sys.exit(1) | |
# Convert and output | |
config = convert_adlists(adlists_json) | |
print(yaml.dump(config, default_flow_style=False)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment