shakecities/sites/db.py

37 lines
908 B
Python
Raw Normal View History

2023-11-08 19:11:55 +11:00
import mysql.connector
import os
import dotenv
dotenv.load_dotenv()
# Database connection
dbargs = {
'host':os.getenv('DB_HOST'),
'user':os.getenv('DB_USER'),
'password':os.getenv('DB_PASSWORD'),
'database':os.getenv('DB_NAME')
}
2023-11-08 20:43:33 +11:00
def get_website_data(domain):
2023-11-08 19:11:55 +11:00
connection = mysql.connector.connect(**dbargs)
cursor = connection.cursor()
cursor.execute("""
SELECT * FROM site WHERE domain = %s
""", (domain,))
2023-11-08 20:43:33 +11:00
data = cursor.fetchall()
2023-11-08 19:11:55 +11:00
cursor.close()
connection.close()
2023-11-08 20:43:33 +11:00
if data == []:
# Create new entry
connection = mysql.connector.connect(**dbargs)
cursor = connection.cursor()
cursor.execute("""
INSERT INTO site (domain, data)
VALUES (%s, %s)
""", (domain, ""))
connection.commit()
cursor.close()
connection.close()
return ""
return data[0][2]