This repository has been archived by the owner on Feb 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
128 lines (95 loc) · 3.63 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
from pydantic import BaseModel
from os import getenv
from datetime import datetime, timedelta
from threading import Timer
from fastapi import FastAPI
from fastapi.responses import HTMLResponse, FileResponse
from requests import post
from dotenv import load_dotenv
load_dotenv()
discord_hook = getenv("COMMON_ROOM_WEBHOOK")
max_members = int(getenv("MAX_MEMBERS"))
max_time = int(getenv("MAX_TIME"))
class CommonRoomSchema(BaseModel):
name: str
class MembersList(dict):
def add(self, key, value):
self[key] = value
def delete(self, key):
del self[key]
class RepeatedTimer(object):
def __init__(self, interval, function, *args, **kwargs):
self._timer = None
self.interval = interval
self.function = function
self.args = args
self.kwargs = kwargs
self.is_running = False
self.start()
def _run(self):
self.is_running = False
self.start()
self.function(*self.args, **self.kwargs)
def start(self):
if not self.is_running:
self._timer = Timer(self.interval, self._run)
self._timer.start()
self.is_running = True
def stop(self):
self._timer.cancel()
self.is_running = False
app = FastAPI()
current_members = MembersList()
@app.post("/signin")
def common_room_signin(member: CommonRoomSchema):
if member.name in current_members or len(current_members) < max_members:
current_members.add(member.name, datetime.now())
webhook_data = {"content": f"{member.name} has just signed in to the common room, {len(current_members)}"
f"/{max_members} members currently signed in"}
post(discord_hook, data=webhook_data)
return {
"code": "200",
"message": f"{member.name} successfully signed in"
}
else:
return {
"code": "403",
"message": f"Could not sign {member.name} in to common room, too many people currently in the room"
}
@app.post("/signout")
def common_room_signout(member: CommonRoomSchema):
if member.name in current_members:
current_members.delete(member.name)
webhook_data = {"content": f"{member.name} has just signed out of the common room, {len(current_members)}"
f"/{max_members} members currently signed in"}
post(discord_hook, data=webhook_data)
return {
"code": "200",
"message": f"{member.name} successfully signed out"
}
return {
"code": "404",
"message": f"{member.name} not signed in to common room"
}
@app.get("/signin", response_class=HTMLResponse)
def signin_page():
return open("html/signin-form.html", "r").read()
@app.get("/signout", response_class=HTMLResponse)
def signout_page():
return open("html/signin-form.html", "r").read()
@app.get("/img/redbrick", response_class=FileResponse)
def redbrick_img():
return FileResponse("assets/redbrick.png")
@app.get("/favicon.ico", response_class=FileResponse)
def redbrick_img():
return FileResponse("assets/redbrick.png")
@app.get("/img/engineering", response_class=FileResponse)
def engineering_img():
return FileResponse("assets/engineering.png")
def check_members():
for member in list(current_members.keys()):
if current_members[member] < datetime.now() - timedelta(seconds=max_time):
webhook_data = {"content": f"{member}'s time has expired, please sign back in or leave the common room"}
post(discord_hook, data=webhook_data)
current_members.delete(member)
rt = RepeatedTimer(5, check_members)