-
Notifications
You must be signed in to change notification settings - Fork 21
/
create_history_db.py
71 lines (52 loc) · 2.12 KB
/
create_history_db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
import os
import sqlite3
import sys
from os.path import exists
from urlparse import urlparse
def create_history_db(input_db):
flags = os.O_CREAT | os.O_EXCL | os.O_WRONLY
home_directory = os.path.expanduser('~')
os.walk(home_directory)
pearsignore = str(home_directory) + '/.pearsignore'
f = open(pearsignore, 'r')
firefox_history_db = input_db
if firefox_history_db is None:
print 'Cannot find Firefox history database...\n\nExiting...'
sys.exit(2)
# Connect to the firefox history database
firefox_con = sqlite3.connect(firefox_history_db)
# Create the history.db file if it does not exist
if not exists('history.db'):
new_db = os.open('history.db', flags)
os.close(new_db)
history_con = sqlite3.connect('history.db')
with firefox_con:
firefox_con.row_factory = sqlite3.Row
cur = firefox_con.cursor()
cur.execute("SELECT * FROM moz_places")
rows = cur.fetchall()
history_cur = history_con.cursor()
history_cur.execute("DROP TABLE IF EXISTS History")
history_cur.execute("CREATE TABLE History(Id INTEGER PRIMARY KEY, URL TEXT, TITLE TEXT, BODY TEXT)")
# Get a list of the values from the .pearsignore file in the home directory
exclude_list = f.readline().split(',')
print exclude_list
for row in rows:
exclude_flag = False
url = row['url']
# Check to make sure input is a valid url
u = urlparse(url)
if u.scheme == 'https' or 'http':
# Check the exclude list to see if contained in the url
for excludes in exclude_list:
if excludes in u.netloc:
exclude_flag = True
print url + ' OMITTED FROM DATABASE'
continue
# Commit valid url
if exclude_flag is False:
history_cur.execute("INSERT INTO History(URL) VALUES (?)", (url, ))
history_con.commit()
history_con.close()
firefox_con.close()