Spaces:
Running
Running
Olivier-Truong
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import *
|
2 |
+
from base64 import b64decode, b64encode
|
3 |
+
from time import ctime, sleep, time
|
4 |
+
from threading import Thread
|
5 |
+
import gzip, requests
|
6 |
+
from getFreeRoomsFromAde2 import AdeRequest
|
7 |
+
|
8 |
+
Ade = AdeRequest()
|
9 |
+
infos = Ade.getRoomsInfos()
|
10 |
+
freeRooms = Ade.getCurrentsFreeRooms()
|
11 |
+
|
12 |
+
|
13 |
+
|
14 |
+
def import_allowed():
|
15 |
+
tab = []
|
16 |
+
|
17 |
+
for room in freeRooms:
|
18 |
+
tab.append([room, freeRooms[room]["freeUntil"]])
|
19 |
+
return tab
|
20 |
+
|
21 |
+
def import_response_data():
|
22 |
+
tab = []
|
23 |
+
|
24 |
+
for room in freeRooms:
|
25 |
+
tab.append([room, freeRooms[room]["capacity"], freeRooms[room]["freeUntil"], freeRooms[room]["busy"]])
|
26 |
+
return tab
|
27 |
+
|
28 |
+
app = Flask(__name__)
|
29 |
+
app.config['response_data'] = import_response_data() # [numRoom: str, capacity: int, freeUntil: str, busy: tuple]
|
30 |
+
app.config['allowed'] = import_allowed() # [numRoom: str, freeUtil: str]
|
31 |
+
|
32 |
+
def reloadData():
|
33 |
+
while True:
|
34 |
+
try:
|
35 |
+
sleep(600)
|
36 |
+
infos = Ade.getRoomsInfos()
|
37 |
+
freeRooms = Ade.getCurrentsFreeRooms()
|
38 |
+
app.config['response_data'] = import_response_data() # [numRoom: str, capacity: int, freeUntil: str, busy: tuple]
|
39 |
+
app.config['allowed'] = import_allowed() # [numRoom: str, freeUtil: str]
|
40 |
+
print("[+] Refresh Data From Ade")
|
41 |
+
|
42 |
+
except Exception as e:
|
43 |
+
print("Err. When reload:", e)
|
44 |
+
|
45 |
+
|
46 |
+
Thread(target=reloadData).start()
|
47 |
+
|
48 |
+
|
49 |
+
@app.route('/')
|
50 |
+
def index():
|
51 |
+
prepPage = ""
|
52 |
+
deux = ""
|
53 |
+
for ip in app.config["allowed"]:
|
54 |
+
prepPage += "<div class=\"ip-item" + deux + "\" onclick=\"PrintResponse(this, '" + ip[0] +"');\">\n"
|
55 |
+
ipPadded = ip[0] + ((14 - len(ip[0])) * " ")
|
56 |
+
prepPage += "<p>Salle n°" + ipPadded + "</p> \n"
|
57 |
+
prepPage += "<p>Disponible jusqu'à -> " + ip[1] + "</p> \n"
|
58 |
+
|
59 |
+
prepPage += "</div>\n"
|
60 |
+
if deux == "":
|
61 |
+
deux = "2"
|
62 |
+
else:
|
63 |
+
deux = ""
|
64 |
+
res = open("templates/index.html", "r").read().replace("=====cnt=====", prepPage).replace("\\=====HOME-IP_Addr=====\\", "Get Free Rooms From Ade").encode()
|
65 |
+
resp = Response(gzip.compress(res))
|
66 |
+
resp.headers['Content-Encoding'] = 'gzip'
|
67 |
+
return resp
|
68 |
+
|
69 |
+
|
70 |
+
@app.route("/responsesFrom/<ip>")
|
71 |
+
def responsesFrom(ip):
|
72 |
+
def busyUntil(tab):
|
73 |
+
ts, te = tab
|
74 |
+
return f'- {str(ts[0]).zfill(2)}h{str(ts[1]).zfill(2)} à {str(te[0]).zfill(2)}h{str(te[1]).zfill(2)}\n'
|
75 |
+
responses = "\r\n\r\n"
|
76 |
+
for resp in app.config['response_data']:
|
77 |
+
if resp[0] == ip:
|
78 |
+
responses += str(ip) + " capacité: " + str(resp[1]) + " disponible jusqu'à: " + str(resp[2])
|
79 |
+
if resp[3] != []:
|
80 |
+
responses += "\noccupée durrant:\n" + str("".join(busyUntil(x) for x in resp[3])) + "\r\n\r\n"
|
81 |
+
resp = Response(gzip.compress(responses.encode()))
|
82 |
+
resp.headers['Content-Encoding'] = 'gzip'
|
83 |
+
return resp
|
84 |
+
|
85 |
+
|
86 |
+
|
87 |
+
if __name__ == '__main__':
|
88 |
+
app.run(host='0.0.0.0', port=7860)
|