nictemplate/server.py

68 lines
2.1 KiB
Python
Raw Permalink Normal View History

2023-10-30 15:37:51 +11:00
from flask import Flask, make_response, redirect, request, jsonify, render_template, send_from_directory
import os
import dotenv
2023-11-03 14:49:45 +11:00
import requests
2024-02-11 20:20:50 +11:00
import datetime
2023-10-30 15:37:51 +11:00
app = Flask(__name__)
dotenv.load_dotenv()
2024-02-20 15:02:40 +11:00
not_for_sale = ['australia', 'newzealand']
2024-02-19 10:41:07 +11:00
2023-10-30 15:37:51 +11:00
@app.route('/assets/<path:path>')
def send_report(path):
return send_from_directory('templates/assets', path)
@app.route('/')
def index():
2024-02-11 20:20:50 +11:00
year = datetime.datetime.now().year
2023-11-02 19:11:12 +11:00
https_redirect="<script src=\"https://nathan.woodburn/https.js\"></script>"
2023-10-30 15:37:51 +11:00
# Get host from request
host = request.headers.get('host')
2023-10-30 16:13:19 +11:00
tld = host.split('.')[-1]
2023-10-31 14:40:58 +11:00
tld = tld.split(':')[0]
if tld == 'localhost' or tld == '1':
2023-11-03 14:49:45 +11:00
tld = 'freeconcept'
https_redirect = "<script>console.log('https.js not loaded on localhost')</script>"
2023-11-02 19:11:12 +11:00
2024-02-19 10:41:07 +11:00
if tld in not_for_sale:
return render_template('not_for_sale.html', tld=tld, https_redirect=https_redirect, year=year)
2023-11-03 14:49:45 +11:00
# Count sales
sales = requests.get('https://reg.woodburn.au/api?action=getMyStaked', headers={'Authorization': 'Bearer ' + os.getenv('reg_auth')})
sales = sales.json()
if 'data' not in sales:
if tld.startswith('xn--'):
tld = tld.encode('ascii').decode('idna')
2024-02-11 20:20:50 +11:00
return render_template('index.html', tld=tld, https_redirect=https_redirect, sales=0, year=year)
2023-11-03 14:49:45 +11:00
tld_sales = 0
sales = sales['data']
for sale in sales:
if sale['tld'] == tld:
tld_sales = sale['slds']
if tld.startswith('xn--'):
tld = tld.encode('ascii').decode('idna')
2024-02-19 10:41:07 +11:00
if tld_sales == 0:
not_for_sale.append(tld)
return render_template('not_for_sale.html', tld=tld, https_redirect=https_redirect, year=year)
2024-02-11 20:20:50 +11:00
return render_template('index.html', tld=tld, https_redirect=https_redirect, sales=tld_sales, year=year)
2023-11-02 19:11:12 +11:00
# 404 catch all
@app.errorhandler(404)
def not_found(e):
host = request.headers.get('host')
tld = host.split('.')[-1]
tld = tld.split(':')[0]
if tld == 'localhost' or tld == '1':
tld = 'example'
return render_template('404.html', tld=tld), 404
2023-10-30 15:37:51 +11:00
if __name__ == '__main__':
app.run(debug=False, port=5000, host='0.0.0.0')