This repository has been archived by the owner on Oct 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.py
69 lines (57 loc) · 1.64 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
import json
import asyncio
import logging
import time
import random
from quart import Quart, request, send_file, abort
from utils.chrome import Chrome
app = Quart(__name__)
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
logging.getLogger("asyncio").setLevel(logging.CRITICAL)
with open("./config.json", "r", encoding="utf8") as f:
config = json.load(f)
chrome = Chrome(
chromedriver=config.get("chromedriver_path", ""),
proxy=random.choice(config["proxies"]) if config.get("proxies", None) else None,
headless=config.get("chrome_headless", True),
timeout=config.get("chrome_timeout", 5),
window_size=config.get("chrome_window_size", [1920, 1080])
)
def debug_print(message: str):
if config.get("debug", False):
print(message)
@app.route("/", methods=["GET"])
async def index():
return {"hello": "world"}
@app.route("/", methods=["POST"])
async def html_engine():
print(1)
data = await request.json
print(2)
html = data.get("html", None)
css = data.get("css", None)
print(3)
if not html:
abort(400, "No HTML provided")
print(4)
before_ping = time.monotonic()
image_output = asyncio.ensure_future(
chrome.render(html, css), loop=loop
)
print(5)
await image_output
print(6)
after_ping = int((time.monotonic() - before_ping) * 1000)
debug_print(f"Took {after_ping}ms to produce the image")
print(7)
return await send_file(
image_output.result(),
mimetype="image/png",
attachment_filename="htmlcss.png"
)
app.run(
port=config.get("port", 8080),
debug=config.get("debug", False),
loop=loop
)