import os import ujson import flask import random import secrets from dotenv import load_dotenv from json.decoder import JSONDecodeError load_dotenv() def register(app): @app.route('/api/tos-verification', methods=['POST']) def tos_verification_api(): if not flask.request.headers.get('Authorization') == os.getenv('TOS_VERIFICATION_KEY'): return 'Unauthorized', 401 code = secrets.token_urlsafe(6) emoji = random.choice(open('config/emojis.txt', encoding='utf8').read()) if not os.path.exists('data/tos.json'): open('data/tos.json', 'w', encoding='utf8').write('{}') try: data = ujson.load(open('data/tos.json', encoding='utf8')) except JSONDecodeError: data = {} data[code] = emoji ujson.dump(data, open('data/tos.json', 'w', encoding='utf8')) return { 'code': code, 'emoji': emoji } @app.route('/api/tos-verification/', methods=['DELETE']) def tos_verification_api_delete(code): if not flask.request.headers.get('Authorization') == os.getenv('TOS_VERIFICATION_KEY'): return 'Unauthorized', 401 data = ujson.load(open('data/tos.json', encoding='utf8')) del data[code] ujson.dump(data, open('data/tos.json', 'w', encoding='utf8')) return 'Removed.', 204 @app.route('/legal/') def legal_site(subpath): emoji = None if subpath == 'terms': emoji = ujson.load(open('data/tos.json', encoding='utf8')).get(flask.request.args.get('verify')) if emoji: emoji = emoji.encode('utf8') return flask.render_template(f'legal/{subpath}.html', verify=emoji)