feat: Update city page
All checks were successful
Build Docker / Build Main Image (push) Successful in 20s
Build Docker / Build SLDs Image (push) Successful in 20s

This commit is contained in:
2023-11-15 18:03:43 +11:00
parent 771debdc50
commit 400505d472
12 changed files with 163 additions and 14 deletions

View File

@@ -34,6 +34,35 @@ def get_website_data(domain):
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 == []:
# 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
def get_website_wallet(domain,token):
connection = mysql.connector.connect(**dbargs)
cursor = connection.cursor()

View File

@@ -30,8 +30,9 @@ def index():
# Get website data
data = db.get_website_data(host)
db_object = db.get_website_data_raw(host)
# Render as HTML
return website.render(data)
return website.render(data,db_object)
@app.route('/.well-known/wallets/<token>')
@@ -53,6 +54,18 @@ def catch_all(path):
def not_found(e):
return redirect('/')
def clean_template():
# Clean template
with open('templates/city.html') as f:
data = f.read()
data = data.replace('#f1ffff', '{{bg_colour}}')
data = data.replace('#1fffff', '{{text_colour}}')
data = data.replace('#000000', '{{text_colour}}')
# Save
with open('templates/city.html', 'w') as f:
f.write(data)
print("Cleaned template", flush=True)
if __name__ == '__main__':
app.run(debug=False, port=5000, host='0.0.0.0')

View File

@@ -24,6 +24,8 @@ class GunicornApp(BaseApplication):
return self.application
if __name__ == '__main__':
print("Cleaning template...", flush=True)
slds.clean_template()
workers = os.getenv('WORKERS')
threads = os.getenv('THREADS')
if workers is None:

View File

@@ -7,18 +7,86 @@ main_domain = "cities.hnshosting.au"
if os.getenv('MAIN_DOMAIN') != None:
main_domain = os.getenv('MAIN_DOMAIN')
def render(data):
def render(data,db_object):
if data == "":
return redirect("https://" + main_domain + '/claim?domain=' + request.host.split('.')[0])
# Render as HTML
html = ""
try:
soup = BeautifulSoup(data, 'html.parser')
for script in soup.find_all('script'):
script.extract()
modified = str(soup)
return render_template_string(modified)
html = str(soup)
except Exception as e:
return "<h1>Invalid HTML</h1><br>" + str(e)
bg_colour = db_object['bg_colour']
hex_colour = bg_colour.lstrip('#')
text_colour = generate_foreground_color(tuple(int(hex_colour[i:i+2], 16) for i in (0, 2, 4)))
text_colour = rgb_to_hex(text_colour)
if (text_colour == "#000000"):
hns_icon = "assets/img/HNS.png"
else:
hns_icon = "assets/img/HNSW.png"
try:
avatar = db_object['avatar']
hnschat = db_object['hnschat']
location = db_object['location']
hns = db_object['HNS']
btc = db_object['BTC']
eth = db_object['ETH']
except Exception as e:
return "<h1>Invalid HTML</h1><br>" + str(e)
return "<h1>Invalid data</h1><br><h2>Please contact support</h2><br>" + str(e)
return render_template('city.html',html=html,bg_colour=bg_colour,text_colour=text_colour,
avatar=avatar,main_domain=main_domain,
hnschat=hnschat,location=location, hns_icon=hns_icon,
hns=hns,btc=btc,eth=eth, data=html)
def calculate_contrast_ratio(color1, color2):
def calculate_luminance(color):
def adjust_color_value(value):
value /= 255.0
if value <= 0.03928:
return value / 12.92
return ((value + 0.055) / 1.055) ** 2.4
r, g, b = color
r = adjust_color_value(r)
g = adjust_color_value(g)
b = adjust_color_value(b)
return 0.2126 * r + 0.7152 * g + 0.0722 * b
luminance1 = calculate_luminance(color1)
luminance2 = calculate_luminance(color2)
brighter = max(luminance1, luminance2)
darker = min(luminance1, luminance2)
contrast_ratio = (brighter + 0.05) / (darker + 0.05)
return contrast_ratio
def generate_foreground_color(background_color):
# A color with a high contrast ratio against the background color
contrast_color = (255, 255, 255) # White
# Calculate the contrast ratio
ratio = calculate_contrast_ratio(background_color, contrast_color)
# Adjust the contrast color based on the contrast ratio
if ratio < 4.5:
# If the contrast ratio is below the threshold, use black as the foreground color
return (0, 0, 0) # Black
else:
return contrast_color
def rgb_to_hex(rgb_color):
return "#{:02x}{:02x}{:02x}".format(*rgb_color)