Upload conversationDB.py
Browse filesUpdated the methods of the DataBase class
- conversationDB.py +42 -8
conversationDB.py
CHANGED
@@ -35,7 +35,7 @@ class DataBase():
|
|
35 |
db.mysql_password=mysql_password
|
36 |
db.mysql_db=mysql_db
|
37 |
def getmessages(db,username = ""):
|
38 |
-
#
|
39 |
with SSHTunnelForwarder(
|
40 |
(db.ssh_host, db.ssh_port),
|
41 |
ssh_username=db.ssh_user,
|
@@ -56,7 +56,7 @@ class DataBase():
|
|
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()
|
@@ -64,9 +64,8 @@ class DataBase():
|
|
64 |
# close the SSH client
|
65 |
db.ssh_client.close()
|
66 |
return messages
|
67 |
-
|
68 |
-
|
69 |
-
# start the SSH tunnel using sshtunnel
|
70 |
with SSHTunnelForwarder(
|
71 |
(db.ssh_host, db.ssh_port),
|
72 |
ssh_username=db.ssh_user,
|
@@ -83,7 +82,8 @@ class DataBase():
|
|
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)
|
|
|
87 |
cursor.execute(query)
|
88 |
mysql_conn.commit()
|
89 |
|
@@ -92,11 +92,45 @@ class DataBase():
|
|
92 |
|
93 |
# close the SSH client
|
94 |
db.ssh_client.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
if __name__ == '__main__':
|
97 |
-
|
|
|
98 |
|
99 |
db = DataBase()
|
100 |
-
db.setmessages(username='user2',message_text='some message')
|
|
|
101 |
messages = db.getmessages(username)
|
102 |
print(messages)
|
|
|
35 |
db.mysql_password=mysql_password
|
36 |
db.mysql_db=mysql_db
|
37 |
def getmessages(db,username = ""):
|
38 |
+
# get the entire conversation with the specific user
|
39 |
with SSHTunnelForwarder(
|
40 |
(db.ssh_host, db.ssh_port),
|
41 |
ssh_username=db.ssh_user,
|
|
|
56 |
cursor.execute(query)
|
57 |
rows = cursor.fetchall()
|
58 |
|
59 |
+
messages = [(message[2],message[3]) for message in rows]
|
60 |
|
61 |
# close the MySQL connection
|
62 |
mysql_conn.close()
|
|
|
64 |
# close the SSH client
|
65 |
db.ssh_client.close()
|
66 |
return messages
|
67 |
+
def setmessages(db,username, message_text="", bot_reply=""):
|
68 |
+
# Adding the record into the database
|
|
|
69 |
with SSHTunnelForwarder(
|
70 |
(db.ssh_host, db.ssh_port),
|
71 |
ssh_username=db.ssh_user,
|
|
|
82 |
|
83 |
# send a query to the MySQL database and print the response table
|
84 |
with mysql_conn.cursor() as cursor:
|
85 |
+
query = f"INSERT INTO {dbTableName} (username,message_text,bot_reply) " \
|
86 |
+
f"VALUES ('{username}','{message_text}','{bot_reply}');"
|
87 |
cursor.execute(query)
|
88 |
mysql_conn.commit()
|
89 |
|
|
|
92 |
|
93 |
# close the SSH client
|
94 |
db.ssh_client.close()
|
95 |
+
def cleanup(db, username = "", remaining_messages = 3):
|
96 |
+
# Cleanup the records, except the last N rows
|
97 |
+
with SSHTunnelForwarder(
|
98 |
+
(db.ssh_host, db.ssh_port),
|
99 |
+
ssh_username=db.ssh_user,
|
100 |
+
ssh_pkey=db.private_key,
|
101 |
+
remote_bind_address=(db.mysql_host, db.mysql_port)) as tunnel:
|
102 |
+
# connect to the MySQL server through the SSH tunnel
|
103 |
+
mysql_conn = pymysql.connect(
|
104 |
+
host='localhost', # will be due to the SSH tunneling issue
|
105 |
+
port=tunnel.local_bind_port,
|
106 |
+
user=db.mysql_user,
|
107 |
+
password=db.mysql_password,
|
108 |
+
db=db.mysql_db
|
109 |
+
)
|
110 |
+
|
111 |
+
# send a query to the MySQL database to delete the records except the last ones
|
112 |
+
with mysql_conn.cursor() as cursor:
|
113 |
+
|
114 |
+
query = f"DELETE FROM {dbTableName} WHERE username = '{username}' AND id NOT IN " \
|
115 |
+
f"(SELECT id FROM (SELECT id FROM (SELECT id FROM {dbTableName} " \
|
116 |
+
f"WHERE username = '{username}' ORDER BY id DESC LIMIT {remaining_messages}) " \
|
117 |
+
f"subquery) subsubquery)"
|
118 |
+
cursor.execute(query)
|
119 |
+
mysql_conn.commit()
|
120 |
+
|
121 |
+
# close the MySQL connection
|
122 |
+
mysql_conn.close()
|
123 |
+
|
124 |
+
# close the SSH client
|
125 |
+
db.ssh_client.close()
|
126 |
+
|
127 |
|
128 |
if __name__ == '__main__':
|
129 |
+
# This is for testing purpose only:
|
130 |
+
username = 'user1'
|
131 |
|
132 |
db = DataBase()
|
133 |
+
db.setmessages(username='user2',message_text='some message',bot_reply='some reply')
|
134 |
+
#db.cleanup(username='user2',remaining_messages=1)
|
135 |
messages = db.getmessages(username)
|
136 |
print(messages)
|