-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
relay.py
111 lines (86 loc) · 2.51 KB
/
relay.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
from flask import Flask, request, jsonify, send_file, redirect
from flask_cors import CORS, cross_origin
from mcstatus import JavaServer
app = Flask(__name__)
CORS(app)
color_codes = {
"4": "dark_red",
"c": "red",
"6": "gold",
"e": "yellow",
"2": "dark_green",
"a": "green",
"b": "aqua",
"3": "dark_aqua",
"1": "dark_blue",
"9": "blue",
"d": "light_purple",
"5": "dark_purple",
"f": "white",
"7": "gray",
"8": "dark_gray",
"0": "black",
}
def process_color_codes(raw_desc):
if isinstance(raw_desc, dict):
if raw_desc.get("extra"):
return raw_desc["extra"]
raw_desc = raw_desc["text"]
description = []
token = ""
params = {"color": "default"}
desc_iter = iter(raw_desc)
try:
while True:
char = next(desc_iter)
if char != "§":
token += char
else:
if token:
description.append({"text": token, **params})
token = ""
code = next(desc_iter)
if code in color_codes:
params["color"] = color_codes[code]
elif code == "l":
params["bold"] = True
elif code == "o":
params["italic"] = True
elif code == "n":
params["underlined"] = True
elif code == "m":
params["strikethrough"] = True
elif code == "k":
params["obfuscated"] = True
elif code == "r":
params = {"color": "default"}
except StopIteration:
pass
if token:
description.append({"text": token, **params})
return description
@app.route("/status")
@cross_origin()
def status():
if request.args.get("server"):
try:
server = JavaServer.lookup(request.args["server"])
raw_status = server.status().raw
status = {
"description": process_color_codes(raw_status["description"]),
"players": raw_status["players"],
}
return jsonify(status)
except OSError:
return jsonify({}), 404
@app.route("/")
def index():
return redirect("https://breq.dev/apps/mcstatus.html")
@app.route("/mcstatus.css")
def css():
return send_file("mcstatus.css")
@app.route("/mcstatus.js")
def js():
return send_file("mcstatus.js")
if __name__ == "__main__":
app.run("0.0.0.0")