kertser commited on
Commit
1ef5f8b
1 Parent(s): 0abcb4b

Upload conversationDB.py

Browse files

Updated DB manager methods

Files changed (1) hide show
  1. conversationDB.py +86 -37
conversationDB.py CHANGED
@@ -13,41 +13,90 @@ mysql_port = 3306 # the default MySQL port
13
  mysql_user = 'root'
14
  mysql_password = 'naP2tion'
15
  mysql_db = 'warbot'
 
 
16
 
17
- # create an SSH client and load the private key
18
- ssh_client = paramiko.SSHClient()
19
- ssh_client.load_system_host_keys()
20
- ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
21
- private_key = paramiko.RSAKey.from_private_key_file(ssh_key_path)
22
-
23
- # start the SSH tunnel using sshtunnel
24
- with SSHTunnelForwarder(
25
- (ssh_host, 22),
26
- ssh_username=ssh_user,
27
- ssh_pkey=private_key,
28
- remote_bind_address=(mysql_host, mysql_port)) as tunnel:
29
-
30
- # connect to the MySQL server through the SSH tunnel
31
- mysql_conn = pymysql.connect(
32
- host='localhost',
33
- port=tunnel.local_bind_port,
34
- user=mysql_user,
35
- password=mysql_password,
36
- db=mysql_db
37
- )
38
-
39
- # send a query to the MySQL database and print the response table
40
- with mysql_conn.cursor() as cursor:
41
- query = 'SELECT * FROM conversations WHERE username = "user1";'
42
- cursor.execute(query)
43
- rows = cursor.fetchall()
44
- for row in rows:
45
- print(row)
46
-
47
- # close the MySQL connection
48
- mysql_conn.close()
49
-
50
- # close the SSH client
51
- ssh_client.close()
52
-
53
- # query = 'SELECT * FROM conversations WHERE username = "user1";'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  mysql_user = 'root'
14
  mysql_password = 'naP2tion'
15
  mysql_db = 'warbot'
16
+ ssh_port = 22
17
+ dbTableName = 'conversations' # messages data table
18
 
19
+ class DataBase():
20
+ # manages mySQL connection and send-receive
21
+ def __init__(db, ssh_key_path = ssh_key_path, ssh_host=ssh_host, ssh_port=ssh_port, ssh_user=ssh_user,
22
+ mysql_host=mysql_host,mysql_port=mysql_port,mysql_user=mysql_user,mysql_password=mysql_password,mysql_db=mysql_db):
23
+ # create an SSH client and load the private key
24
+ db.ssh_client = paramiko.SSHClient()
25
+ db.ssh_client.load_system_host_keys()
26
+ db.ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
27
+ db.private_key = paramiko.RSAKey.from_private_key_file(ssh_key_path)
28
+ db.ssh_key_path = ssh_key_path
29
+ db.ssh_host=ssh_host
30
+ db.ssh_port=ssh_port
31
+ db.ssh_user=ssh_user
32
+ db.mysql_host=mysql_host
33
+ db.mysql_port=mysql_port
34
+ db.mysql_user=mysql_user
35
+ db.mysql_password=mysql_password
36
+ db.mysql_db=mysql_db
37
+ def getmessages(db,username = ""):
38
+ # start the SSH tunnel using sshtunnel
39
+ with SSHTunnelForwarder(
40
+ (db.ssh_host, db.ssh_port),
41
+ ssh_username=db.ssh_user,
42
+ ssh_pkey=db.private_key,
43
+ remote_bind_address=(db.mysql_host, db.mysql_port)) as tunnel:
44
+ # connect to the MySQL server through the SSH tunnel
45
+ mysql_conn = pymysql.connect(
46
+ host='localhost', # will be due to the SSH tunneling issue
47
+ port=tunnel.local_bind_port,
48
+ user=db.mysql_user,
49
+ password=db.mysql_password,
50
+ db=db.mysql_db
51
+ )
52
+
53
+ # send a query to the MySQL database and print the response table
54
+ with mysql_conn.cursor() as cursor:
55
+ query = f'SELECT * FROM {dbTableName} WHERE username = "{username}";'
56
+ cursor.execute(query)
57
+ rows = cursor.fetchall()
58
+
59
+ messages = [message[2] for message in rows]
60
+
61
+ # close the MySQL connection
62
+ mysql_conn.close()
63
+
64
+ # close the SSH client
65
+ db.ssh_client.close()
66
+ return messages
67
+
68
+ def setmessages(db,username, message_text):
69
+ # start the SSH tunnel using sshtunnel
70
+ with SSHTunnelForwarder(
71
+ (db.ssh_host, db.ssh_port),
72
+ ssh_username=db.ssh_user,
73
+ ssh_pkey=db.private_key,
74
+ remote_bind_address=(db.mysql_host, db.mysql_port)) as tunnel:
75
+ # connect to the MySQL server through the SSH tunnel
76
+ mysql_conn = pymysql.connect(
77
+ host='localhost', # will be due to the SSH tunneling issue
78
+ port=tunnel.local_bind_port,
79
+ user=db.mysql_user,
80
+ password=db.mysql_password,
81
+ db=db.mysql_db
82
+ )
83
+
84
+ # send a query to the MySQL database and print the response table
85
+ with mysql_conn.cursor() as cursor:
86
+ query = f"INSERT INTO {dbTableName} (username,message_text) VALUES ('{username}','{message_text}');"
87
+ cursor.execute(query)
88
+ mysql_conn.commit()
89
+
90
+ # close the MySQL connection
91
+ mysql_conn.close()
92
+
93
+ # close the SSH client
94
+ db.ssh_client.close()
95
+
96
+ if __name__ == '__main__':
97
+ username = 'user2'
98
+
99
+ db = DataBase()
100
+ db.setmessages(username='user2',message_text='some message')
101
+ messages = db.getmessages(username)
102
+ print(messages)