fix: Store website data as json
All checks were successful
Build Docker / Build Main Image (push) Successful in 19s
Build Docker / Build SLDs Image (push) Successful in 20s

This commit is contained in:
Nathan Woodburn 2023-11-08 22:09:26 +11:00
parent 303e0dddd1
commit 24544fc115
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1

31
db.py
View File

@ -1,6 +1,7 @@
import mysql.connector import mysql.connector
import os import os
import dotenv import dotenv
import json
dotenv.load_dotenv() dotenv.load_dotenv()
@ -32,7 +33,7 @@ def check_tables():
CREATE TABLE IF NOT EXISTS site ( CREATE TABLE IF NOT EXISTS site (
id INT(11) NOT NULL AUTO_INCREMENT, id INT(11) NOT NULL AUTO_INCREMENT,
domain VARCHAR(255) NOT NULL, domain VARCHAR(255) NOT NULL,
data VARCHAR(2048) NOT NULL, data JSON,
PRIMARY KEY (id) PRIMARY KEY (id)
) )
""") """)
@ -99,22 +100,34 @@ def get_website_data(domain):
# Create new entry # Create new entry
connection = mysql.connector.connect(**dbargs) connection = mysql.connector.connect(**dbargs)
cursor = connection.cursor() cursor = connection.cursor()
cursor.execute(""" data = {
INSERT INTO site (domain, data) "data": ""
VALUES (%s, %s) }
""", (domain, "")) insert_query = "INSERT INTO site (data,domain) VALUES (%s,%s)"
cursor.execute(insert_query, (json.dumps(data), domain))
connection.commit() connection.commit()
cursor.close() cursor.close()
connection.close() connection.close()
return "" return ""
return data[0][2]
parsed = data[0][2]
parsed = json.loads(parsed)
parsed = parsed['data']
# Decoding
parsed = parsed.encode('utf-8').decode('unicode-escape')
return parsed
def update_website_data(domain,data): def update_website_data(domain,data):
connection = mysql.connector.connect(**dbargs) connection = mysql.connector.connect(**dbargs)
cursor = connection.cursor() cursor = connection.cursor()
cursor.execute(""" # Create json object
UPDATE site SET data = %s WHERE domain = %s data = {
""", (data, domain)) "data": data
}
update_query = "UPDATE site SET data = %s WHERE domain = %s"
cursor.execute(update_query, (json.dumps(data), domain))
connection.commit() connection.commit()
cursor.close() cursor.close()
connection.close() connection.close()