-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.py
94 lines (67 loc) · 2.37 KB
/
index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import asyncio
import json
import sass
import aiohttp
import random
import hashlib
from io import BytesIO
from quart import Quart, render_template, request, send_file
app = Quart(__name__)
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
def load_json(filename: str) -> dict:
with open(filename, "r", encoding="utf8") as f:
data = json.load(f)
return data
def string_to_sha512(text: str):
salt = config.get("sha512_salt", "")
result = hashlib.sha512(f"{salt}{text}".encode()).hexdigest()
return result
config = load_json("./config.json")
testimonials = load_json("./data/testimonials.json")
@app.route("/")
async def index():
ip_for_seed = request.headers.get("CF-Connecting-IP") or \
request.headers.get("X-Forwarded-For") or \
request.remote_addr
encrypted_ip = string_to_sha512(ip_for_seed)
async with aiohttp.ClientSession() as session:
async with session.get(f"https://api.alexflipnote.dev/nft?seed={encrypted_ip}") as response:
data = await response.json()
random.shuffle(testimonials)
return await render_template(
"index.html", nft=data,
testimonials=testimonials,
enumerate=enumerate
)
async def fetch_and_read(filename: str, text_hex: str, text_colour: str):
async with aiohttp.ClientSession() as session:
async with session.get(f"https://api.alexflipnote.dev/nft/{text_hex}/{text_colour}") as response:
data = await response.read()
return await send_file(
BytesIO(data), mimetype="image/png",
attachment_filename=filename,
as_attachment=True
)
@app.route("/download/<text_hex>/<text_colour>")
async def index_download(text_hex, text_colour):
return await fetch_and_read("xela_nft.png", text_hex, text_colour)
@app.route("/steal/<text_hex>/<text_colour>")
async def index_stolen_download(text_hex, text_colour):
return await fetch_and_read("stolen_xela_nft.png", text_hex, text_colour)
@app.errorhandler(Exception)
async def handle_exception(e):
return await render_template(
"error_handler.html",
error_code=e.code,
error_message=e.description
)
sass.compile(
dirname=["./static/sass", "./static/css"],
output_style="compressed"
)
app.config["TEMPLATES_AUTO_RELOAD"] = True
app.run(
port=config.get("port", 8080),
debug=config.get("debug", False)
)