-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.py
102 lines (80 loc) · 2.8 KB
/
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import psycopg2
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
class Database:
def __init__ (self, user_name, user_password, db_name):
self.user_name = user_name
self.user_password = user_password
self.db_name = db_name
def conn_establish(self):
"""Tries to establish connection to the DB"""
try:
conn = psycopg2.connect(database=self.db_name, user=self.user_name,
password=self.user_password,
host="127.0.0.1",
port="5432")
except psycopg2.DatabaseError as error:
print (error)
def new_db(self):
"""Creates a new DB to add the table in"""
#Add: A check, the user shouldn't enter the name of an existing DB
con = None
try:
con = psycopg2.connect(database="postgres", user=self.user_name,
password=self.user_password,
host="127.0.0.1",
port="5432")
con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cur = con.cursor()
cur.execute("CREATE DATABASE " + self.db_name)
cur.close()
con.close()
except psycopg2.DatabaseError as error:
print (error)
def create_table(self):
"""Initialises a new table"""
con = None
try:
con = psycopg2.connect(database=self.db_name, user=self.user_name,
password=self.user_password,
host="127.0.0.1",
port="5432")
cur = con.cursor()
cur.execute("CREATE TABLE vuln_data ( cve_id text, pkg_name text, status text)")
con.commit()
con.close()
except psycopg2.DatabaseError as error:
print (error)
def first_insert(self):
"""Scrapes data, adds to the table"""
conn = None
try:
conn = psycopg2.connect(database=self.db_name, user=self.user_name,
password=self.user_password,
host="127.0.0.1",
port="5432")
cur = conn.cursor()
print ("Scraping Data!")
import scanner as sc
sc.ubuntu_data()
sc.debian_data()
for entries in range(len(sc.cve_id)):
cur.execute("INSERT into vuln_data(cve_id, pkg_name, status) VALUES (%s, %s, %s)",(sc.cve_id[entries],
sc.pkg_name[entries],
sc.vuln_status[entries]))
conn.commit()
conn.close()
except psycopg2.DatabaseError as error:
print (error)
def update(self):
"""Scraper scrapes new data, updates the whole darn table (temporary solution)"""
pass
"""
#FIX ME: Ineffective Solution
con = psycopg2.connect(database=db_name, user=user_name, password=user_password, host="127.0.0.1", port="5432")
cur = con.cursor()
count = 0
for entries in len(cve_id):
cur.execute ("UPDATE vuln_data SET (cve_id, pkg_name, status) WHERE (%s, %s, %s)", (cve_id [entries], pkg_name [entries], vuln_status [entries+1]))
con.commit()
con.close()
"""