shakecities/sites/db.py

85 lines
2.0 KiB
Python
Raw Normal View History

2023-11-08 19:11:55 +11:00
import mysql.connector
import os
import dotenv
2023-11-08 22:14:06 +11:00
import json
2023-11-08 19:11:55 +11:00
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 == []:
return ""
2023-11-08 22:14:06 +11:00
parsed = data[0][2]
parsed = json.loads(parsed)
parsed = parsed['data']
# Decoding
parsed = parsed.encode('utf-8').decode('unicode-escape')
2023-11-09 12:13:59 +11:00
return parsed
2023-11-15 18:03:43 +11:00
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 == []:
# Create new entry
connection = mysql.connector.connect(**dbargs)
cursor = connection.cursor()
data = {
"data": ""
}
insert_query = "INSERT INTO site (data,domain) VALUES (%s,%s)"
cursor.execute(insert_query, (json.dumps(data), domain))
connection.commit()
cursor.close()
connection.close()
return ""
parsed = data[0][2]
parsed = json.loads(parsed)
return parsed
2023-11-09 12:13:59 +11:00
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]
2023-11-09 12:24:35 +11:00
return parsed
2023-11-09 12:13:59 +11:00
return ""