-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·177 lines (154 loc) · 5.45 KB
/
main.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python3
import os
import subprocess
import io
import re
import flask
import base58
import qrcode
from PIL import Image, ImageDraw, ImageFont
import requests
import lxml.html
app = flask.Flask(__name__)
GNUJDB_KEY_REGEX = r"^[0-9A-HJ-NP-Za-km-z]{10}$"
GNUJDB_KEY_REGEX_COMPILED = re.compile(GNUJDB_KEY_REGEX)
def gen_key():
ret = ""
while not GNUJDB_KEY_REGEX_COMPILED.match(ret):
ret = base58.b58encode(os.getrandom(7)).decode()
return ret
def zbuduj_linie(opis):
ret = []
aktualna_opis = ""
slowa = opis.split(" ")
for slowo in slowa:
if aktualna_opis:
aktualna_opis += " "
if len(aktualna_opis) + len(slowo) > 16:
ret.append(aktualna_opis)
aktualna_opis = ""
if aktualna_opis:
aktualna_opis += " "
aktualna_opis += slowo
ret.append(aktualna_opis)
return "\n".join(ret)
def dopisz_do_gnujdb(k, opis, wlasnosc):
s = requests.Session()
url = "https://g.hs-ldz.pl/" + k
html = s.get(url).text
h = lxml.html.fromstring(html)
csrf_token = h.xpath('//input [@name="csrfmiddlewaretoken"]/@value')[0]
s.post(
url,
data={
"csrfmiddlewaretoken": csrf_token,
"tytul": opis,
"wlasnosc": wlasnosc,
},
)
def generuj_png(k, opis, wlasnosc):
qr = qrcode.make("https://g.hs-ldz.pl/" + k, box_size=3)
img = Image.new("RGB", (500, 150), color="white")
draw = ImageDraw.Draw(img)
myFont = ImageFont.truetype(
"/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf", 42
)
draw.text((0, 0), zbuduj_linie(opis), fill=(0, 0, 0), font=myFont)
img.paste(qr, (390, 0))
captionFont = ImageFont.truetype(
"/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf", 16
)
draw.text((390, 100), k, fill=(0, 0, 0), font=captionFont)
if wlasnosc:
draw.text(
(390, 120), "wł. " + wlasnosc, fill=(0, 0, 0), font=captionFont
)
bio = io.BytesIO()
img.save(bio, format="png")
return bio.getvalue()
def generuj_i_drukuj(opis, wlasnosc, kopii):
k = gen_key()
png_b = generuj_png(k, opis, wlasnosc)
path = "out/" + k + ".png"
with open(path, "wb") as f:
f.write(png_b)
for i in range(kopii):
subprocess.check_call(
"""brother_ql -m QL-800 -p /dev/usb/lp0 print -l 50 """ + path,
shell=True,
)
dopisz_do_gnujdb(k, opis, wlasnosc)
return path
@app.route("/drukuj", methods=["POST"])
def drukuj():
opis = flask.request.form["opis"]
wlasnosc = flask.request.form["wlasnosc"]
kopii = int(flask.request.form.get("kopii", 1))
try:
path = generuj_i_drukuj(opis, wlasnosc, kopii)
except subprocess.CalledProcessError:
return "subprocess.CalledProcessError. is the printer turned on?"
return flask.send_file(path, mimetype="image/png")
@app.route("/", methods=["POST", "GET"])
def podglad():
if flask.request.method == "POST":
opis = flask.request.form["opis"]
wlasnosc = flask.request.form["wlasnosc"]
png_b = generuj_png(gen_key(), opis, wlasnosc)
response = flask.make_response(png_b)
response.headers["Content-Type"] = "image_png"
return response
else:
return """
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>labelprinter</title>
<body onload="przerysujObrazek()">
<script>
function przerysujObrazek() {
var xhr = new XMLHttpRequest();
xhr.responseType = 'arraybuffer';
xhr.onreadystatechange = function() {
var arr = new Uint8Array(this.response);
var raw = String.fromCharCode.apply(null,arr);
var b64=btoa(raw);
var dataURL="data:image/jpeg;base64,"+b64;
document.getElementById("wygenerowany_obraz").src = dataURL;
};
xhr.open('POST', '/', true);
var data = new FormData();
opis = document.getElementById("opis").value;
if (opis) {
data.append('opis', document.getElementById("opis").value);
} else {
data.append('opis', "Przykładowy opis rzeczy");
}
data.append('wlasnosc', document.getElementById("wlasnosc").value);
xhr.send(data)
}
</script>
<form method="post" action="/drukuj">
<label for="kopii">Kopii: </label>
<input id="kopii" name="kopii" type="number"
min="1" max="10" minlegth="1" value="1" style="width: 4em;"
>
<label for="opis">Tytuł: </label>
<input id="opis" name="opis" type="text"
onchange="przerysujObrazek();"
onpaste="this.onchange();"
oninput="this.onchange();"
placeholder="Przykładowy opis rzeczy"
>
<label for="wlasnosc">Własność: </label>
<input id="wlasnosc" name="wlasnosc" type="text"
placeholder="nieznany"
onchange="przerysujObrazek();"
onpaste="this.onchange();"
oninput="this.onchange();"
>
<input type="submit" value="Drukuj">
</form>
<img id="wygenerowany_obraz"
style="border: 1px dotted; max-width: 100%">
"""
if __name__ == "__main__":
app.run(host="0.0.0.0")