Skip to content

Commit cce8aae

Browse files
author
netevert
committed
closes #28
1 parent 2ec3356 commit cce8aae

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

utils.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
class Database(object):
2323
"""Local sqlite database handler class"""
2424
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"""
2626

27+
self.api_keys = [("virustotal", "", 0), ("shodan", "", 0), ("otx", "", 0)]
28+
2729
# verify that db folder exists, if not create one
2830
if sys.platform == "win32":
2931
self.db_path = os.getenv("LOCALAPPDATA")+ "\\pockint\\"
@@ -37,22 +39,33 @@ def __init__(self):
3739
self.db = sqlite3.connect(self.db_path + "\\.pockint.db")
3840
self.cursor = self.db.cursor()
3941

42+
# upgrades old database versions
43+
self.upgrade_database()
44+
45+
def upgrade_database(self):
46+
"""Upgrades tables of existing pockint databases"""
4047
# create json_data table if not exist (to upgrade old databases)
4148
self.cursor.execute("""SELECT name FROM sqlite_master WHERE type='table' AND name='json_data';""")
4249
if not self.cursor.fetchone():
4350
self.cursor.execute('''CREATE TABLE json_data (id INTEGER PRIMARY KEY,
4451
investigation_id TEXT, json TEXT)''')
4552
self.db.commit()
4653

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+
4761
def create_database(self):
4862
"""Creates a new database in AppData/Local"""
4963
db = sqlite3.connect(self.db_path + "\\.pockint.db")
5064
cursor = db.cursor()
5165
try:
5266
cursor.execute('''CREATE TABLE api_keys(id INTEGER PRIMARY KEY, api_name TEXT,
5367
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)
5669
db.commit()
5770
cursor.execute('''CREATE TABLE json_data (id INTEGER PRIMARY KEY,
5871
investigation_id TEXT, json TEXT)''')

0 commit comments

Comments
 (0)