imagestack/tools/python_utils/app.py
2025-08-04 19:12:59 +02:00

69 lines
1.8 KiB
Python

from flask import Flask, send_from_directory, abort, request
import json
import requests
import os
import label
app = Flask(__name__)
def pretty_print_POST(req):
"""
At this point it is completely built and ready
to be fired; it is "prepared".
However pay attention at the formatting used in
this function because it is programmed to be pretty
printed and may differ from the actual request.
"""
print('{}\n{}\r\n{}\r\n\r\n{}'.format(
'-----------START-----------',
req.method + ' ' + req.url,
'\r\n'.join('{}: {}'.format(k, v) for k, v in req.headers.items()),
req.body,
))
FILES_DIR = os.path.join(os.path.dirname(__file__), "files")
@app.route('/')
def index():
return("↑ ↑ ↓ ↓ ← → ← → B A")
@app.route('/snipe_api')
def home():
api_key = os.environ['API_KEY']
return api_key
@app.route('/download_late_command')
def download_file():
try:
return send_from_directory(FILES_DIR, "late_command.sh", as_attachment=True)
except FileNotFoundError:
abort(404, description="File not found")
@app.route('/preseed.cfg')
def download_pre():
try:
return send_from_directory(FILES_DIR, "preseed.cfg", as_attachment=True)
except FileNotFoundError:
abort(404, description="File not found")
@app.route("/label", methods=["POST"])
def build():
typst_bytes = request.get_data()
typst_string = typst_bytes.decode("utf-8")
try:
j = json.loads(typst_string)
except json.JSONDecodeError as e:
print(e)
return "Failed"
pdf = label.compile_pdf(j)
subprocess.run(
["lpr", "-H", "10.200.4.12", "-P", "DYMO", "-o", "landscape", "-"], input=pdf
)
return "Success"
if __name__ == '__main__':
app.run(debug=True)