2016-09-30 20:15:44 +00:00
|
|
|
#!/usr/bin/env python
|
2016-09-28 19:18:34 +00:00
|
|
|
# -*- 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.
|
|
|
|
|
2016-09-28 19:18:34 +00:00
|
|
|
import sqlite3
|
|
|
|
|
2016-10-02 04:16:31 +00:00
|
|
|
# Logfile containing unique list of all domains on downloaded lists.
|
2016-09-28 19:18:34 +00:00
|
|
|
logfile = '/etc/pihole/pihole.2.eventHorizon.txt'
|
|
|
|
|
|
|
|
# Create the SQLite connection
|
2016-10-02 16:39:37 +00:00
|
|
|
conn = sqlite3.connect('/etc/pihole/gravity.db')
|
2016-09-28 19:18:34 +00:00
|
|
|
|
2016-10-02 04:16:31 +00:00
|
|
|
# Python auto-handle commits, no need to call for commits manually
|
2016-10-02 03:03:50 +00:00
|
|
|
with conn:
|
|
|
|
c = conn.cursor()
|
2016-09-28 19:18:34 +00:00
|
|
|
|
2016-10-02 16:53:43 +00:00
|
|
|
# enable WAL mode
|
|
|
|
c.execute('PRAGMA journal_mode=WAL;')
|
|
|
|
|
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
|
2016-10-02 16:52:47 +00:00
|
|
|
gt = '''
|
2016-10-02 04:13:55 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS gravity (
|
|
|
|
idx INTEGER PRIMARY KEY ASC,
|
|
|
|
domain text
|
|
|
|
)
|
|
|
|
'''
|
2016-10-02 16:52:47 +00:00
|
|
|
c.execute(gt)
|
2016-09-28 19:18:34 +00:00
|
|
|
|
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:
|
2016-10-02 16:39:37 +00:00
|
|
|
line = line.rstrip()
|
2016-10-02 04:13:55 +00:00
|
|
|
sql = "INSERT INTO gravity (domain) VALUES (?)"
|
|
|
|
c.execute(sql, (line,))
|