Skip to content

Instantly share code, notes, and snippets.

@Kapujino
Last active February 13, 2024 16:29
Show Gist options
  • Save Kapujino/03feaa159f72c84bbef37f3551cb069a to your computer and use it in GitHub Desktop.
Save Kapujino/03feaa159f72c84bbef37f3551cb069a to your computer and use it in GitHub Desktop.
This script adds a plain list of adlists to the adguard configuration AdGuardHome.yaml
#!/usr/bin/python3
import yaml
# load AdGuardHome.yaml
with open('AdGuardHome.yaml', 'r') as file:
data = yaml.load(file, Loader=yaml.FullLoader)
# load file with adlist sources
with open('adlists.txt', 'r') as file:
urls = file.readlines()
new_entries = []
starting_id = 3 # adjust the starting ID as needed
for url in urls:
url = url.strip()
new_filter = {
"enabled": True,
"url": url,
"name": url,
"id": starting_id
}
starting_id += 1
new_entries.append(new_filter)
# append the new entries to the existing filters
data['filters'] += new_entries
# save the new AdGuardHome.yaml_migrated
with open('AdGuardHome.yaml_migrated', 'w') as file:
yaml.dump(data, file, default_flow_style=False)
print("new file created. please compare it to the old file.")
@Kapujino
Copy link
Author

I did not find an easy solution to get the adlists from pihole, so i copied them from the webpage http://pi.hole/admin/groups-adlists.php and removed the unnecessary stuff with some regex.

Using the text editor vim you can use the following regex:
%s/\(https\:\/\/\|http\:\/\/\)\@!.// this removes everything but http://* and https://*
%s/^\s*\n// this removes the empty lines
%s/\t//g this removes tabstops

You can use any other method to get a clean list of your adlists.
Save this file as adlists.txt and put it together with this script and AdGuardHome.yaml in one folder.

ATTENTION: change starting_id = 3 if you have already added adlists to adguard!
Run the script migrateAdlistToAdguard.py to get a new adguard configuration called AdGuardHome.yaml_migrated.

Carefully compare the previous configuration AdGuardHome.yaml with the new one AdGuardHome.yaml_migrated.

Replace your old configuration if the changes are fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment