shakecities/sites/slds.py

71 lines
1.8 KiB
Python
Raw Normal View History

2023-11-08 20:43:33 +11:00
from flask import Flask, make_response, redirect, render_template_string, request, jsonify, render_template, send_from_directory
2023-11-08 17:55:49 +11:00
import os
import dotenv
import requests
import json
import schedule
import time
2023-11-08 20:43:33 +11:00
import db
import website
2023-11-08 19:06:08 +11:00
2023-11-08 17:55:49 +11:00
app = Flask(__name__)
dotenv.load_dotenv()
main_domain = "cities.hnshosting.au"
if os.getenv('MAIN_DOMAIN') != None:
main_domain = os.getenv('MAIN_DOMAIN')
2023-11-08 17:55:49 +11:00
#Assets routes
@app.route('/assets/<path:path>')
def assets(path):
return send_from_directory('templates/assets', path)
@app.route('/')
def index():
host = request.host
2023-11-08 20:49:03 +11:00
if len(host.split('.')) != 2:
return redirect('https://'+main_domain)
2023-11-08 20:49:03 +11:00
host = host.split('.')[0]
2023-11-08 20:43:33 +11:00
# Get website data
data = db.get_website_data(host)
2023-11-15 18:03:43 +11:00
db_object = db.get_website_data_raw(host)
2023-11-08 20:43:33 +11:00
# Render as HTML
2023-11-15 18:03:43 +11:00
return website.render(data,db_object)
2023-11-08 17:55:49 +11:00
2023-11-09 12:13:59 +11:00
@app.route('/.well-known/wallets/<token>')
def wallet(token):
address = db.get_website_wallet(request.host.split('.')[0],token)
if address == "":
return redirect('/')
# Plain text
response = make_response(address)
response.mimetype = "text/plain"
return response
2023-11-08 17:55:49 +11:00
@app.route('/<path:path>')
def catch_all(path):
return redirect('/') # 404 catch all
# 404 catch all
@app.errorhandler(404)
def not_found(e):
return redirect('/')
2023-11-15 18:03:43 +11:00
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)
2023-11-08 17:55:49 +11:00
if __name__ == '__main__':
app.run(debug=False, port=5000, host='0.0.0.0')