Initial disaster
This commit is contained in:
		
							parent
							
								
									69ecf8b295
								
							
						
					
					
						commit
						6fe753f98d
					
				@ -19,11 +19,11 @@
 | 
			
		||||
        overlays = [devshell.overlays.default];
 | 
			
		||||
      };
 | 
			
		||||
      mach-lib = mach-nix.lib."${system}";
 | 
			
		||||
    in {
 | 
			
		||||
    in rec {
 | 
			
		||||
      packages.default = mach-lib.mkPython {
 | 
			
		||||
        requirements = builtins.readFile ./requirements.txt;
 | 
			
		||||
      };
 | 
			
		||||
      devShells.default = pkgs.devshell.mkShell {packages = with pkgs; [imagemagick];};
 | 
			
		||||
      devShells.default = pkgs.devshell.mkShell {packages = with pkgs; [packages.default imagemagick resvg python3Packages.brother-ql];};
 | 
			
		||||
      formatter = pkgs.alejandra;
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										85
									
								
								main.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										85
									
								
								main.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,85 @@
 | 
			
		||||
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',
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
with open("dump.json") as f:
 | 
			
		||||
    ws = sorted(
 | 
			
		||||
        filter(lambda e: re.search("W\d+", e["name"]), json.load(f)),
 | 
			
		||||
        key=lambda e: e["name"],
 | 
			
		||||
    )
 | 
			
		||||
with open("custom.json") as cf:
 | 
			
		||||
    cws = sorted(
 | 
			
		||||
        json.load(cf),
 | 
			
		||||
        key=lambda e: e["name"],
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
indexed = { w["name"]: w for w in ws} | { w["name"]: (w | dict(local=True)) for w in cws}
 | 
			
		||||
 | 
			
		||||
db = sqlite3.connect("counts.db")
 | 
			
		||||
cur = db.cursor()
 | 
			
		||||
cur.execute("CREATE TABLE IF NOT EXISTS counts(what, count)")
 | 
			
		||||
 | 
			
		||||
@app.route("/", methods=["GET"])
 | 
			
		||||
async def home():
 | 
			
		||||
    return await render_template("index.html", ws=ws, cws=cws)
 | 
			
		||||
 | 
			
		||||
@app.route('/static/<path:path>')
 | 
			
		||||
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 thewarn.get("local"):
 | 
			
		||||
        printit(thewarn["url"].removeprefix("/"))
 | 
			
		||||
        lol = thewarn["name"]
 | 
			
		||||
    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)
 | 
			
		||||
        printit(png_filename)
 | 
			
		||||
        # with open("count.txt", "w") as cf:
 | 
			
		||||
        #     try:
 | 
			
		||||
        #         c = int(cf.read())
 | 
			
		||||
        #     except UnsupportedOperation: # no file
 | 
			
		||||
        #         c = 0
 | 
			
		||||
        #     cf.write(str(c + 1))
 | 
			
		||||
    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()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    return redirect("/")
 | 
			
		||||
if __name__ == "__main__":
 | 
			
		||||
    app.run()
 | 
			
		||||
							
								
								
									
										2
									
								
								requirements.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								requirements.txt
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,2 @@
 | 
			
		||||
requests
 | 
			
		||||
quart
 | 
			
		||||
							
								
								
									
										58
									
								
								templates/index.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								templates/index.html
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,58 @@
 | 
			
		||||
<!DOCTYPE html>
 | 
			
		||||
<html lang="en">
 | 
			
		||||
<head>
 | 
			
		||||
    <meta charset="UTF-8">
 | 
			
		||||
    <title></title>
 | 
			
		||||
    <style>
 | 
			
		||||
        body {
 | 
			
		||||
            margin: 0 auto;
 | 
			
		||||
        }
 | 
			
		||||
        #things {
 | 
			
		||||
            display: flex;
 | 
			
		||||
            flex-flow: row wrap;
 | 
			
		||||
            gap: 5px;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        .thing {
 | 
			
		||||
            width: 250px;
 | 
			
		||||
            border: solid 2px;
 | 
			
		||||
        }
 | 
			
		||||
    </style>
 | 
			
		||||
</head>
 | 
			
		||||
<body>
 | 
			
		||||
    <h1>ISO7010 Warning Stickers On Demand</h1>
 | 
			
		||||
    <p>Please do not abuse kthxbai!</p>
 | 
			
		||||
    <form action="/upload" method="POST">
 | 
			
		||||
        <label for=""> Upload a PNG: (black and white, no transparency!) </label>
 | 
			
		||||
        <input type="file" accept="image/png">
 | 
			
		||||
    </form>
 | 
			
		||||
    <h2>Official ISO7010</h2>
 | 
			
		||||
    <div id="things">
 | 
			
		||||
        {% for w in ws %}
 | 
			
		||||
            <div class="thing">
 | 
			
		||||
                <img src="{{ w.url }}" width="100%">
 | 
			
		||||
                <div>
 | 
			
		||||
                    {{ w.name }}
 | 
			
		||||
                    <form method="POST">
 | 
			
		||||
                        <button type="submit" name="w" value="{{ w.name }}">Print</button>
 | 
			
		||||
                    </form>
 | 
			
		||||
                </div>
 | 
			
		||||
            </div>
 | 
			
		||||
        {% endfor %}
 | 
			
		||||
    </div>
 | 
			
		||||
    <h2>"Unofficial" ISO7010</h2>
 | 
			
		||||
    <div id="things">
 | 
			
		||||
        {% for w in cws %}
 | 
			
		||||
            <div class="thing">
 | 
			
		||||
                <img src="{{ w.url }}" width="100%">
 | 
			
		||||
                <div>
 | 
			
		||||
                    {{ w.name }}
 | 
			
		||||
                    <form method="POST">
 | 
			
		||||
                        <button type="submit" name="w" value="{{ w.name }}">Print</button>
 | 
			
		||||
                    </form>
 | 
			
		||||
                </div>
 | 
			
		||||
            </div>
 | 
			
		||||
        {% endfor %}
 | 
			
		||||
    </div>
 | 
			
		||||
</body>
 | 
			
		||||
</html>
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user