Blocklist vs regex preference?

That's correct. You can use any database editor that supports sqlite3 databases or use the sqlite3 CLI to achieve this or write a simply python script like

import sqlite3

# Connect to the database
db = sqlite3.connect('/etc/pihole/gravity.db')
# Instantiate a cursor object
cur = db.cursor()

# Define regex filters to be added in this file
domains = [('abc'),('def'),('fgh')]

# Alternatively, read them from a file
with open('regex.list', 'r') as f:
    domains = f.read().splitlines()

# Generator for domains
def domain():
    for c in domains:
        yield (c,)

# Can be used to remove all currently enabled black regex
# cur.execute('DELETE FROM domainlist WHERE type = 3')

# Add (or change to black regex if already existing) regex filters from list
cur.executemany('INSERT OR REPLACE INTO domainlist (domain, type) VALUES (?, 3)', domain())

# List all black regex
cur.execute('SELECT domain FROM domainlist WHERE type = 3')
results = cur.fetchall()
for row in results:
        print(row[0])

# Commit changes to the database
db.commit()
db.close()

Run it using

sudo -u pihole python3 import-regex.py

to ensure proper permissions for the database.

Note that, initially, we imported the legacy files whenever they were there instead of only when a new database is created during the upgrade v4 -> v5. What do you think?

Thanks for the python code. I'm relatively familiar with the sqlite3 CLI tool but have also been meaning to do more with python.

I loaded the dev version on another laptop that I will use while tethered to my cellphone. I'll report back.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.