shakecities/main.py

298 lines
9.1 KiB
Python
Raw Normal View History

2023-11-08 17:55:49 +11:00
from flask import Flask, make_response, redirect, request, jsonify, render_template, send_from_directory
import os
import dotenv
import requests
import json
import schedule
import time
from email_validator import validate_email, EmailNotValidError
import accounts
2023-11-08 19:02:24 +11:00
import db
2023-11-16 14:12:24 +11:00
import varo
2023-11-08 17:55:49 +11:00
app = Flask(__name__)
dotenv.load_dotenv()
2023-11-08 19:02:24 +11:00
# 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 17:55:49 +11:00
2023-11-09 12:59:15 +11:00
CITY_DOMAIN = os.getenv('CITY_DOMAIN')
if CITY_DOMAIN == None:
CITY_DOMAIN = "exampledomainnathan1"
2023-11-15 22:51:30 +11:00
random_sites = ""
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)
2023-11-08 19:02:24 +11:00
2023-11-08 17:55:49 +11:00
def error(message):
return jsonify({'success': False, 'message': message}), 400
@app.route('/')
def index():
2023-11-15 22:51:30 +11:00
global random_sites
if random_sites == "":
random_sites_names = db.get_random_sites()
for site in random_sites_names:
random_sites += "<a href='https://" + site + "." + CITY_DOMAIN + "' target='_blank'>" + site + "." +CITY_DOMAIN+ "</a><br>"
2023-11-08 17:55:49 +11:00
if 'token' in request.cookies:
token = request.cookies['token']
# Verify token
user = accounts.validate_token(token)
if not user:
# Remove cookie
resp = make_response(redirect('/'))
resp.set_cookie('token', '', expires=0)
return resp
2023-11-15 22:51:30 +11:00
return render_template('index.html',account=user['email'],account_link="account",account_link_name="Account",CITY_DOMAIN=CITY_DOMAIN,random_sites=random_sites)
return render_template('index.html',account="Login",account_link="login",account_link_name="Login",CITY_DOMAIN=CITY_DOMAIN,random_sites=random_sites)
2023-11-08 17:55:49 +11:00
@app.route('/signup', methods=['POST'])
def signup():
email = request.form['email']
2023-11-17 10:43:46 +11:00
domain = request.form['domain'].lower()
2023-11-08 17:55:49 +11:00
password = request.form['password']
# Verify domain
if domain.startswith('_'):
return error('Invalid domain')
2023-11-08 17:55:49 +11:00
try:
valid = validate_email(email)
email = valid.email
user = accounts.create_user(email, domain, password)
if not user['success']:
return error(user['message'])
# Redirect to dashboard with cookie
resp = make_response(redirect('/edit'))
resp.set_cookie('token', user['token'])
return resp
except EmailNotValidError as e:
return jsonify({'success': False, 'message': 'Invalid email'}), 400
2023-11-08 20:06:47 +11:00
@app.route('/login', methods=['POST'])
def login():
email=request.form['email']
password=request.form['password']
user = accounts.login(email,password)
if not user['success']:
return error(user['message'])
# Redirect to dashboard with cookie
2023-11-15 16:00:30 +11:00
resp = make_response(redirect('/account'))
2023-11-08 20:06:47 +11:00
resp.set_cookie('token', user['token'])
return resp
2023-11-08 20:43:33 +11:00
@app.route('/edit')
def edit():
2023-11-09 12:59:15 +11:00
if 'token' not in request.cookies:
2023-11-15 15:52:44 +11:00
return redirect('/login')
2023-11-09 12:59:15 +11:00
2023-11-08 20:43:33 +11:00
token = request.cookies['token']
if not accounts.validate_token(token):
return error('Invalid token')
# Verify token
user = accounts.validate_token(token)
if not user:
# Remove cookie
2023-11-15 15:52:44 +11:00
resp = make_response(redirect('/login'))
2023-11-08 20:43:33 +11:00
resp.set_cookie('token', '', expires=0)
return resp
2023-11-09 12:13:59 +11:00
data = db.get_website_data_raw(user['domain'])
2023-11-09 12:17:55 +11:00
html = ""
hns = ""
btc = ""
eth = ""
2023-11-15 16:24:56 +11:00
hnschat = ""
location = ""
avatar = ""
bg_colour = ""
2023-11-15 18:24:11 +11:00
fg_colour = ""
text_colour = ""
2023-11-16 12:56:34 +11:00
email = ""
2023-11-09 12:13:59 +11:00
2023-11-09 12:17:55 +11:00
if 'data' in data:
html = data['data'].encode('utf-8').decode('unicode-escape')
if 'HNS' in data:
hns = data['HNS']
if 'BTC' in data:
btc = data['BTC']
if 'ETH' in data:
eth = data['ETH']
2023-11-15 16:24:56 +11:00
if 'hnschat' in data:
hnschat = data['hnschat']
if 'location' in data:
location = data['location']
if 'avatar' in data:
avatar = data['avatar']
if 'bg_colour' in data:
bg_colour = data['bg_colour']
2023-11-15 18:24:11 +11:00
if 'fg_colour' in data:
fg_colour = data['fg_colour']
2023-11-16 15:31:38 +11:00
else:
fg_colour = "#ffffff"
2023-11-15 18:24:11 +11:00
if 'text_colour' in data:
text_colour = data['text_colour']
if 'email' in data:
email = data['email']
2023-11-09 12:13:59 +11:00
2023-11-15 16:24:56 +11:00
return render_template('edit.html',account=user['email'],account_link="account",account_link_name="Account",data=html,
hns=hns,btc=btc,eth=eth,hnschat=hnschat,email=email,location=location,avatar=avatar,
2023-11-15 18:24:11 +11:00
bg_colour=bg_colour,fg_colour=fg_colour,text_colour=text_colour,
CITY_DOMAIN=CITY_DOMAIN,domain=user['domain'])
2023-11-08 20:43:33 +11:00
@app.route('/edit', methods=['POST'])
def send_edit():
token = request.cookies['token']
if not accounts.validate_token(token):
return error('Invalid token')
# Verify token
user = accounts.validate_token(token)
if not user:
# Remove cookie
2023-11-15 15:52:44 +11:00
resp = make_response(redirect('/login'))
2023-11-08 20:43:33 +11:00
resp.set_cookie('token', '', expires=0)
return resp
2023-11-09 12:21:06 +11:00
# Json data
2023-11-09 12:13:59 +11:00
data = {}
data['data'] = request.form['data']
data['HNS'] = request.form['hns']
data['BTC'] = request.form['btc']
data['ETH'] = request.form['eth']
2023-11-15 16:24:56 +11:00
data['hnschat'] = request.form['hnschat']
data['location'] = request.form['location']
data['avatar'] = request.form['avatar']
data['bg_colour'] = request.form['bg_colour']
2023-11-15 18:24:11 +11:00
data['fg_colour'] = request.form['fg_colour']
data['text_colour'] = request.form['text_colour']
data['email'] = request.form['email']
2023-11-09 12:13:59 +11:00
2023-11-16 14:12:24 +11:00
varo.update_avatar(data['avatar'],user['domain'])
2023-11-09 12:21:06 +11:00
# Convert to json
data = json.dumps(data)
2023-11-09 12:13:59 +11:00
db.update_website_data_raw(user['domain'],data)
2023-11-08 20:43:33 +11:00
return redirect('/edit')
2023-11-08 20:06:47 +11:00
2023-11-08 17:55:49 +11:00
2023-11-08 19:02:24 +11:00
@app.route('/logout')
def logout():
token = request.cookies['token']
if not accounts.logout(token)['success']:
return error('Invalid token')
# Remove cookie
resp = make_response(redirect('/'))
resp.set_cookie('token', '', expires=0)
return resp
2023-11-09 13:06:58 +11:00
@app.route('/claim')
def claim():
# Find domain
domain = request.args.get('domain')
return redirect('/signup?domain=' + domain)
2023-11-16 14:12:24 +11:00
@app.route('/hnschat')
def hnschat():
token = request.cookies['token']
if not accounts.validate_token(token):
return error('Invalid token')
# Verify token
user = accounts.validate_token(token)
if not user:
# Remove cookie
resp = make_response(redirect('/login'))
resp.set_cookie('token', '', expires=0)
return resp
record = varo.get_auth(user['domain'])
return render_template('hnschat.html',account=user['email'],account_link="account",account_link_name="Account",CITY_DOMAIN=CITY_DOMAIN,domain=user['domain'],hnschat=record)
@app.route('/hnschat', methods=['POST'])
def save_hnschat():
token = request.cookies['token']
if not accounts.validate_token(token):
return error('Invalid token')
# Verify token
user = accounts.validate_token(token)
if not user:
# Remove cookie
resp = make_response(redirect('/login'))
resp.set_cookie('token', '', expires=0)
return resp
record = request.form['hnschat']
varo.update_auth(record,user['domain'])
return redirect('/hnschat')
2023-11-09 13:06:58 +11:00
2023-11-08 17:55:49 +11:00
@app.route('/<path:path>')
def catch_all(path):
2023-11-08 20:06:47 +11:00
account = "Login"
account_link = "login"
2023-11-15 15:52:44 +11:00
account_link_name = "Login"
2023-11-08 20:06:47 +11:00
site = "Null"
2023-11-09 13:06:58 +11:00
domain = ""
if 'domain' in request.args:
domain = request.args.get('domain')
2023-11-08 20:06:47 +11:00
if 'token' in request.cookies:
token = request.cookies['token']
# Verify token
user = accounts.validate_token(token)
if not user:
# Remove cookie
resp = make_response(redirect('/'))
resp.set_cookie('token', '', expires=0)
return resp
account = user['email']
account_link = "account"
2023-11-15 15:52:44 +11:00
account_link_name = "Account"
2023-11-09 12:59:15 +11:00
site = user['domain'] + "." + CITY_DOMAIN
2023-11-08 20:43:33 +11:00
elif path != "signup" and path != "login":
return redirect('/')
2023-11-15 16:00:30 +11:00
if path == "account":
account_link = "logout"
account_link_name = "Logout"
2023-11-08 20:06:47 +11:00
2023-11-08 17:55:49 +11:00
# If file exists, load it
if os.path.isfile('templates/' + path):
2023-11-15 15:52:44 +11:00
return render_template(path,account=account,account_link=account_link,account_link_name=account_link_name,site=site,CITY_DOMAIN=CITY_DOMAIN,domain=domain)
2023-11-08 17:55:49 +11:00
# Try with .html
if os.path.isfile('templates/' + path + '.html'):
2023-11-15 15:52:44 +11:00
return render_template(path + '.html',account=account,account_link=account_link,account_link_name=account_link_name,site=site,CITY_DOMAIN=CITY_DOMAIN,domain=domain)
2023-11-08 17:55:49 +11:00
return redirect('/') # 404 catch all
# 404 catch all
@app.errorhandler(404)
def not_found(e):
return redirect('/')
2023-11-15 22:51:30 +11:00
def update_random_sites():
global random_sites
random_sites_names = db.get_random_sites()
random_sites = ""
for site in random_sites_names:
random_sites += "<a href='https://" + site + "." + CITY_DOMAIN + "' target='_blank'>" + site + "." +CITY_DOMAIN+ "</a><br>"
2023-11-08 19:02:24 +11:00
2023-11-08 17:55:49 +11:00
if __name__ == '__main__':
2023-11-08 19:02:24 +11:00
db.check_tables()
2023-11-08 17:55:49 +11:00
app.run(debug=False, port=5000, host='0.0.0.0')