Upload conversationDB.py
Browse filesUpdated DB manager methods
- 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 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|