shakecities/sites/db.py
Nathan Woodburn 8c895eddea
All checks were successful
Build Docker / Build Main Image (push) Successful in 19s
Build Docker / Build SLDs Image (push) Successful in 20s
fix: Update no domain data to use False instead of empty string
2023-11-17 13:06:43 +11:00

74 lines
1.6 KiB
Python

import mysql.connector
import os
import dotenv
import json
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')
}
def get_website_data(domain):
connection = mysql.connector.connect(**dbargs)
cursor = connection.cursor()
cursor.execute("""
SELECT * FROM site WHERE domain = %s
""", (domain,))
data = cursor.fetchall()
cursor.close()
connection.close()
if data == []:
return False
parsed = data[0][2]
parsed = json.loads(parsed)
parsed = parsed['data']
# Decoding
parsed = parsed.encode('utf-8').decode('unicode-escape')
return parsed
def get_website_data_raw(domain):
connection = mysql.connector.connect(**dbargs)
cursor = connection.cursor()
cursor.execute("""
SELECT * FROM site WHERE domain = %s
""", (domain,))
data = cursor.fetchall()
cursor.close()
connection.close()
if data == []:
return ""
parsed = data[0][2]
parsed = json.loads(parsed)
return parsed
def get_website_wallet(domain,token):
connection = mysql.connector.connect(**dbargs)
cursor = connection.cursor()
cursor.execute("""
SELECT * FROM site WHERE domain = %s
""", (domain,))
data = cursor.fetchall()
cursor.close()
connection.close()
if data == []:
return ""
parsed = data[0][2]
parsed = json.loads(parsed)
if token in parsed:
parsed = parsed[token]
return parsed
return ""