diff --git a/.gitignore b/.gitignore index e897b3c..13a4f63 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ __pycache__/ *.json + +sites/templates/ diff --git a/sites/db.py b/sites/db.py index 3854b83..d5cee0c 100644 --- a/sites/db.py +++ b/sites/db.py @@ -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() diff --git a/sites/slds.py b/sites/slds.py index ebd2600..a56035b 100644 --- a/sites/slds.py +++ b/sites/slds.py @@ -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/') @@ -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') \ No newline at end of file diff --git a/sites/sldserver.py b/sites/sldserver.py index 68e1f18..536ff7e 100644 --- a/sites/sldserver.py +++ b/sites/sldserver.py @@ -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: diff --git a/sites/website.py b/sites/website.py index cdfbc80..cf480e4 100644 --- a/sites/website.py +++ b/sites/website.py @@ -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 "

Invalid HTML


" + 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 "

Invalid HTML


" + str(e) \ No newline at end of file + return "

Invalid data


Please contact support


" + 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) diff --git a/templates/account.html b/templates/account.html index 2903756..6100f45 100644 --- a/templates/account.html +++ b/templates/account.html @@ -4,7 +4,7 @@ - shakecities + ShakeCities diff --git a/templates/assets/css/styles.min.css b/templates/assets/css/styles.min.css index b77d653..948b536 100644 --- a/templates/assets/css/styles.min.css +++ b/templates/assets/css/styles.min.css @@ -1 +1 @@ -.bs-icon{--bs-icon-size:.75rem;display:flex;flex-shrink:0;justify-content:center;align-items:center;font-size:var(--bs-icon-size);width:calc(var(--bs-icon-size) * 2);height:calc(var(--bs-icon-size) * 2);color:var(--bs-primary)}.bs-icon-xs{--bs-icon-size:1rem;width:calc(var(--bs-icon-size) * 1.5);height:calc(var(--bs-icon-size) * 1.5)}.bs-icon-sm{--bs-icon-size:1rem}.bs-icon-md{--bs-icon-size:1.5rem}.bs-icon-lg{--bs-icon-size:2rem}.bs-icon-xl{--bs-icon-size:2.5rem}.bs-icon.bs-icon-primary{color:var(--bs-white);background:var(--bs-primary)}.bs-icon.bs-icon-primary-light{color:var(--bs-primary);background:rgba(var(--bs-primary-rgb),.2)}.bs-icon.bs-icon-semi-white{color:var(--bs-primary);background:rgba(255,255,255,.5)}.bs-icon.bs-icon-rounded{border-radius:.5rem}.bs-icon.bs-icon-circle{border-radius:50%}.right-align{right:5%} \ No newline at end of file +.bs-icon{--bs-icon-size:.75rem;display:flex;flex-shrink:0;justify-content:center;align-items:center;font-size:var(--bs-icon-size);width:calc(var(--bs-icon-size) * 2);height:calc(var(--bs-icon-size) * 2);color:var(--bs-primary)}.bs-icon-xs{--bs-icon-size:1rem;width:calc(var(--bs-icon-size) * 1.5);height:calc(var(--bs-icon-size) * 1.5)}.bs-icon-sm{--bs-icon-size:1rem}.bs-icon-md{--bs-icon-size:1.5rem}.bs-icon-lg{--bs-icon-size:2rem}.bs-icon-xl{--bs-icon-size:2.5rem}.bs-icon.bs-icon-primary{color:var(--bs-white);background:var(--bs-primary)}.bs-icon.bs-icon-primary-light{color:var(--bs-primary);background:rgba(var(--bs-primary-rgb),.2)}.bs-icon.bs-icon-semi-white{color:var(--bs-primary);background:rgba(255,255,255,.5)}.bs-icon.bs-icon-rounded{border-radius:.5rem}.bs-icon.bs-icon-circle{border-radius:50%}.right-align{right:1%}.top-align{top:0;margin-top:1%}.sticky-bottom{width:100%;bottom:1%;left:0} \ No newline at end of file diff --git a/templates/city.html b/templates/city.html index 7570c58..8ffa78f 100644 --- a/templates/city.html +++ b/templates/city.html @@ -1,10 +1,10 @@ - + - shakecities + ShakeCities @@ -16,7 +16,42 @@ - + +
+
+
+
+
+

{{hnschat}}

+
+
+

{{location}}

+
+
+
+
+
+
+

HNS: {{hns}}

+
+
+

BTC: {{btc}}

+
+
+

ETH: {{eth}}

+
+
+
+
+
+

{{data|safe}}

+
+
+
+
+

Powered by ShakeCities

+
+
diff --git a/templates/edit.html b/templates/edit.html index 424326e..be19111 100644 --- a/templates/edit.html +++ b/templates/edit.html @@ -4,7 +4,7 @@ - shakecities + ShakeCities diff --git a/templates/index.html b/templates/index.html index b1897c4..458206c 100644 --- a/templates/index.html +++ b/templates/index.html @@ -4,7 +4,7 @@ - shakecities + ShakeCities diff --git a/templates/login.html b/templates/login.html index 770bf6f..a954f93 100644 --- a/templates/login.html +++ b/templates/login.html @@ -4,7 +4,7 @@ - shakecities + ShakeCities diff --git a/templates/signup.html b/templates/signup.html index 3d316b2..cc5f0c3 100644 --- a/templates/signup.html +++ b/templates/signup.html @@ -4,7 +4,7 @@ - shakecities + ShakeCities