import os import sqlite3 import json from quart import Quart, render_template, websocket, request, redirect, send_from_directory import re import requests import subprocess from io import UnsupportedOperation app = Quart(__name__) app.config['TEMPLATES_AUTO_RELOAD'] = True headers = { 'User-Agent': 'iso7010print/0.0.1', } defs = { "Official ISO7010": "warns.json", "Unofficial": "custom.json", } def load_things(): out = dict() for section, fname in defs.items(): with open(fname) as f: out[section] = sorted(json.load(f), key=lambda e: e["name"]) return out things = load_things() indexed = {w["name"]: w for kj in things.values() for w in kj} db = sqlite3.connect("counts.db") cur = db.cursor() cur.execute("CREATE TABLE IF NOT EXISTS counts(what TEXT UNIQUE, count INTEGER)") def get_db(): db = sqlite3.connect("counts.db") cur = db.cursor() cur.execute("CREATE TABLE IF NOT EXISTS counts(what TEXT UNIQUE, count INTEGER)") return db @app.route("/", methods=["GET"]) async def home(): cur = get_db().cursor() counts = {c[0]: c[1] for c in cur.execute("select what, count from counts").fetchall()} return await render_template("index.html", ws=things, counts=counts) @app.route('/static/') def staticfile(path): return send_from_directory('static', path) def printit(filename): subprocess.run(f"brother_ql -b pyusb -p usb://0x04f9:0x2042 -m QL-700 print -l62 {filename}", shell=True, check=True) @app.route("/", methods=["POST"]) async def print(): w = (await request.form)["w"] thewarn = indexed[w] if not thewarn["url"].startswith("http"): # it's local filename = thewarn["url"].removeprefix("/") lol = thewarn["name"] png_filename = f"{lol.replace(' ', '_')}.png" else: filename = thewarn["url"].split("/")[-1] lol = re.search('W\d+', filename)[0] png_filename = f"{lol}.png" quad_png_filename = f"{lol}_quad.png" if not os.path.exists(filename): r = requests.get(thewarn["url"], headers=headers) r.raise_for_status() with open(filename, "wb") as file: file.write(r.content) if not os.path.exists(png_filename): subprocess.run(f"resvg {filename} {png_filename}", shell=True, check=True) subprocess.run(f"convert {png_filename} -resize 696x -background white -alpha remove -alpha off -monochrome -threshold 20% {png_filename}", shell=True, check=True) #if not os.path.exists(quad_png_filename): # subprocess.run("magick -size 696x xc:white ({png_filename} -resize 50%x) for _ in range(int((await request.form)["count"])): printit(png_filename) cur.execute("""INSERT OR REPLACE INTO counts(what, count) VALUES (:what, COALESCE( (SELECT count FROM counts WHERE what=:what), 0) + 1); """, dict(what=lol)) db.commit() with open("counts.txt", "a") as f: f.write(f"{lol}\n") return redirect("/") if __name__ == "__main__": app.run()