-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathHistoryAnalyzer.py
76 lines (60 loc) · 2.12 KB
/
HistoryAnalyzer.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
66
67
68
69
70
71
72
73
74
75
76
import os
import sqlite3
import operator
from collections import OrderedDict
import matplotlib.pyplot as plt
import shutil
def parse(url):
try:
parsed_url_components = url.split('//')
sublevel_split = parsed_url_components[1].split('/', 1)
domain = sublevel_split[0].replace("www.", "")
return domain
except IndexError:
print "URL format error! url = {}".format(url)
def analyze(results):
prompt = raw_input("[.] Type <c> to print or <p> to plot\n[>] ")
if prompt.lower() == "c":
for site, count in sites_count_sorted.items():
print site, count
elif prompt.lower() == "p":
plt.bar(range(len(results)), results.values(), align='edge')
plt.xticks(rotation=45)
plt.xticks(range(len(results)), results.keys())
plt.show()
else:
print "[.] Uh?"
analyze (sites_count_sorted)
#path to user's history database (Chrome)
data_path = os.path.expanduser('~')+"\AppData\Local\Google\Chrome\User Data\Default"
files = os.listdir(data_path)
# path to user's temp files
temp_path = os.path.expanduser('~')+"\AppData\Local\Temp"
# create a copy of history database in temp
shutil.copyfile(os.path.join(data_path, 'History'),os.path.join(temp_path,'History.db'))
# connect to the copy of history database
history_db = os.path.join(temp_path,'History.db')
#connection
c = sqlite3.connect(history_db)
cursor = c.cursor()
#list all tables
#list_tables = "SELECT name FROM sqlite_master WHERE type='table';"
#cursor.execute(list_tables)
#print cursor.fetchall()
#url and visit counts
try:
select_statement = "SELECT urls.url, urls.visit_count FROM urls, visits WHERE urls.id = visits.url;"
cursor.execute(select_statement)
except sqlite3.OperationalError:
print "[!] The database is locked! Please exit Chrome and run the script again."
quit()
results = cursor.fetchall() #tuple
sites_count = {} #to iterate easier
for url, count in results:
url = parse(url)
if url in sites_count:
sites_count[url] += 1
else:
sites_count[url] = 1
sites_count_sorted = OrderedDict(sorted(sites_count.items(), key=operator.itemgetter(1), reverse=True))
analyze (sites_count_sorted)