pi-hole/advanced/Scripts/gravity_parse.py

48 lines
1.4 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# -*- coding: utf-8 -*-
2016-10-02 01:56:48 +00:00
# Pi-hole: A black hole for Internet advertisements
# (c) 2015, 2016 by Jacob Salmela
# Network-wide ad blocking via your Raspberry Pi
# http://pi-hole.net
# Controller for all pihole scripts and functions.
#
# Pi-hole is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
import sqlite3
2016-10-02 04:16:31 +00:00
# Logfile containing unique list of all domains on downloaded lists.
logfile = '/etc/pihole/pihole.2.eventHorizon.txt'
# Create the SQLite connection
conn = sqlite3.connect('/etc/pihole/gravity.db')
2016-10-02 04:16:31 +00:00
# Python auto-handle commits, no need to call for commits manually
with conn:
c = conn.cursor()
2016-10-02 04:16:31 +00:00
# Lists have just been downloaded, clear out the existing data
2016-10-02 04:13:55 +00:00
c.execute('DROP TABLE IF EXISTS gravity')
2016-10-02 04:16:31 +00:00
# Ready new table for list of domains
gt = '''
2016-10-02 04:13:55 +00:00
CREATE TABLE IF NOT EXISTS gravity (
idx INTEGER PRIMARY KEY ASC,
domain text
)
'''
c.execute(gt)
2016-10-02 03:12:56 +00:00
# enable WAL mode
c.execute('PRAGMA journal_mode=WAL;')
2016-10-02 04:16:31 +00:00
# Parse the log file into the database
2016-10-02 04:13:55 +00:00
with open(logfile) as f:
for line in f:
line = line.rstrip()
2016-10-02 04:13:55 +00:00
sql = "INSERT INTO gravity (domain) VALUES (?)"
c.execute(sql, (line,))