-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
50 lines (46 loc) · 1.2 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
import flask
import json
import os
import threading
start = """
<!DOCTYPE html>
<html>
<head>
<title>Soundboard</title>
</head>
<body>
<p>
"""
end = """
</p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$("*").each(function() {
if (this.id) {
this.addEventListener("click", function() {
$.get("/" + this.id);
});
}
});
</script>
</body>
</html>
"""
class Soundboard:
def __init__(self):
self.app = flask.Flask(__name__)
@self.app.route("/")
def index():
self.refresh_widgets()
return start + "\n".join(["<img id=\"{}\" src=\"{}\" width=128></img>".format(link, widget["icon"]) for link, widget in zip(self.links, self.widgets)]) + end
@self.app.route("/<link>")
def run(link):
widget = self.widgets[int(link)]
threading.Thread(target = lambda: os.system(widget["action"])).start()
return ""
def refresh_widgets(self):
self.widgets = json.load(open("widgets.json"))
self.links = list(range(len(self.widgets)))
if __name__ == "__main__":
soundboard = Soundboard()
soundboard.app.run(host="0.0.0.0", port=5000)