forked from daxton256/ScratchHTTP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext-server.py
More file actions
69 lines (54 loc) · 2.4 KB
/
Copy pathtext-server.py
File metadata and controls
69 lines (54 loc) · 2.4 KB
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
from flask import Flask, jsonify, request
import requests
from io import BytesIO
from PIL import Image, ImageOps
app = Flask(__name__)
methods = {"am": requests.get, "ar": requests.post, "az": requests.put, "eu": requests.delete, "bg": requests.patch, "zu": lambda url: requests.Response()}
body_methods = (requests.post, requests.put, requests.patch)
types = {"text/html": "h", "text/css": "c", "text/plain": "t", "image/avif": "i", "image/bmp": "i", "image/gif": "i", "image/jpeg": "i", "image/png": "i", "image/tiff": "i", "image/webp": "i"}
color_to_b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
def process_image(_image: bytes):
image = Image.open(BytesIO(_image))
image = image.convert("RGB")
image = ImageOps.contain(image, (256, 256))
palette = Image.open("palette.png")
image = image.quantize(palette=palette)
string = ""
for j in range(image.height):
for i in range(image.width):
pixel = image.getpixel((i, j))
char = color_to_b64[pixel]
string += char
string += " "
return string
@app.route('/translate', methods=['GET'])
def server():
full_txt = request.args["text"].split("|")
txt = full_txt[0]
body = full_txt[1]
method = methods[request.args["language"]]
if method in body_methods:
result = method(txt, body)
if request.args["language"] == "zu":
result.headers["content-type"] = "application/octet-stream"
filetype = types.get(result.headers["content-type"].split(";")[0], "d")
data = {"result": "|".join((str(result.status_code), filetype, result.text))}
else:
result = method(txt)
if request.args["language"] == "zu":
result.headers["content-type"] = "application/octet-stream"
filetype = types.get(result.headers["content-type"].split(";")[0], "d")
match filetype:
case "h" | "c" | "t":
text = result.text
case "i":
text = process_image(result.content)
case "d":
text = result.content.hex()
data = {"result": "|".join((str(result.status_code), filetype, text))}
print(data)
res = jsonify(data)
res.headers.add('Access-Control-Allow-Origin', '*')
return res
if __name__ == '__main__':
app.run(host='127.0.0.1', port=443, ssl_context=("text-server.crt", "text-server.key"))