22
22
class Database (object ):
23
23
"""Local sqlite database handler class"""
24
24
def __init__ (self ):
25
- """Initialises application database, if app db doesn't exist, it creates one"""
25
+ """Initialises or updates application database, if app db doesn't exist, it creates one"""
26
26
27
+ self .api_keys = [("virustotal" , "" , 0 ), ("shodan" , "" , 0 ), ("otx" , "" , 0 )]
28
+
27
29
# verify that db folder exists, if not create one
28
30
if sys .platform == "win32" :
29
31
self .db_path = os .getenv ("LOCALAPPDATA" )+ "\\ pockint\\ "
@@ -37,22 +39,33 @@ def __init__(self):
37
39
self .db = sqlite3 .connect (self .db_path + "\\ .pockint.db" )
38
40
self .cursor = self .db .cursor ()
39
41
42
+ # upgrades old database versions
43
+ self .upgrade_database ()
44
+
45
+ def upgrade_database (self ):
46
+ """Upgrades tables of existing pockint databases"""
40
47
# create json_data table if not exist (to upgrade old databases)
41
48
self .cursor .execute ("""SELECT name FROM sqlite_master WHERE type='table' AND name='json_data';""" )
42
49
if not self .cursor .fetchone ():
43
50
self .cursor .execute ('''CREATE TABLE json_data (id INTEGER PRIMARY KEY,
44
51
investigation_id TEXT, json TEXT)''' )
45
52
self .db .commit ()
46
53
54
+ # updates database with new api keys added to the system
55
+ for key in self .api_keys :
56
+ self .cursor .execute ('''SELECT api_key FROM api_keys WHERE api_name=?''' , (key [0 ],))
57
+ if not self .cursor .fetchone ():
58
+ self .cursor .execute (''' INSERT INTO api_keys(api_name, api_key, status) VALUES(?,?,?)''' , key )
59
+ self .db .commit ()
60
+
47
61
def create_database (self ):
48
62
"""Creates a new database in AppData/Local"""
49
63
db = sqlite3 .connect (self .db_path + "\\ .pockint.db" )
50
64
cursor = db .cursor ()
51
65
try :
52
66
cursor .execute ('''CREATE TABLE api_keys(id INTEGER PRIMARY KEY, api_name TEXT,
53
67
api_key TEXT, status INTEGER)''' )
54
- init_data = [("virustotal" , "" , 0 ), ("shodan" , "" , 0 ), ("otx" , "" , 0 )]
55
- cursor .executemany (''' INSERT INTO api_keys(api_name, api_key, status) VALUES(?,?,?)''' , init_data )
68
+ cursor .executemany (''' INSERT INTO api_keys(api_name, api_key, status) VALUES(?,?,?)''' , self .api_keys )
56
69
db .commit ()
57
70
cursor .execute ('''CREATE TABLE json_data (id INTEGER PRIMARY KEY,
58
71
investigation_id TEXT, json TEXT)''' )
0 commit comments