-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.py
225 lines (200 loc) · 7.92 KB
/
bot.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import discord
import requests
import os
from base64 import b64encode,b64decode
from datetime import datetime
from dotenv import load_dotenv
import dateutil.parser
load_dotenv()
EMAIL = os.environ['EMAIL']
PASSWORD = os.environ['PASSWORD']
STUDIO = {"hash":os.environ['STUDIO'], "int":(b64decode(os.environ['STUDIO'].encode("utf-8"))).decode("utf-8").split(":")[1]}
SESSION = os.environ['SESSION']
S = requests.Session()
if len(SESSION) > 8:
S.cookies.set('SESSION', SESSION)
TENANT = os.environ['BRAND']
if TENANT == 'mcfit':
API_HOST = "my.mcfit.com"
UTILIZATION_VERSION = 1
elif TENANT == 'fitx':
API_HOST = "mein.fitx.de"
UTILIZATION_VERSION = 2
else:
print(f'Invalid brand: "{TENANT}", use either "mcfit" or "fitx"')
exit()
try:
res = requests.get("https://" + API_HOST + "/whitelabelconfigs/web")
res.raise_for_status()
PUBLIC_FACILITY_GROUP = res.json()["publicFacilityGroup"]
TENANT = res.json()["tenantName"]
except Exception as e:
print(e)
print("API call failed, please raise an issue on github")
exit()
try:
res = requests.get("https://" + API_HOST + "/sponsorship/v1/public/studios/"+STUDIO["hash"],
headers={
"x-public-facility-group": PUBLIC_FACILITY_GROUP
}
)
res.raise_for_status()
NAME = res.json()["name"]
except Exception as e:
print(e)
print("Your Studio ID is wrong")
exit()
print(f"Studio: {NAME}")
bot = discord.Bot()
async def login():
if EMAIL == "ENTER_EMAIL":
print("Set an email and password in the .env file")
exit()
headers = {
'authority': API_HOST,
'accept': '*/*',
'accept-language': 'de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7',
'authorization': f'Basic {(b64encode((EMAIL+":"+PASSWORD).encode("utf-8"))).decode("utf-8")}',
'origin': 'https://' + API_HOST,
'referer': 'https://' + API_HOST + '/login-register',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
'x-nox-client-type': 'WEB',
'x-public-facility-group': PUBLIC_FACILITY_GROUP,
'x-tenant': TENANT,
}
json_data = {
'username': EMAIL,
'password': PASSWORD,
}
r = S.post('https://' + API_HOST + '/login', headers=headers, json=json_data)
r.raise_for_status()
print("Session after login: " + S.cookies.get('SESSION'))
print("You can save this in the .env file to speed up the login")
async def get_util_v1():
headers = {
'authority': API_HOST,
'accept': '*/*',
'accept-language': 'de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7',
'content-type': 'application/json',
'referer': 'https://' + API_HOST + '/studio/'+STUDIO["hash"],
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
'x-ms-web-context': '/studio/'+STUDIO["hash"],
'x-nox-client-type': 'WEB',
'x-nox-web-context': 'v=1',
'x-public-facility-group': PUBLIC_FACILITY_GROUP,
'x-tenant': TENANT,
}
r = S.get(f'https://{API_HOST}/nox/v1/studios/{STUDIO["int"]}/utilization', headers=headers)
if r.status_code != 200:
await login()
r = S.get(f'https://{API_HOST}/nox/v1/studios/{STUDIO["int"]}/utilization', headers=headers)
r.raise_for_status()
return r.json()
async def get_util_v2():
headers = {
'authority': API_HOST,
'accept': '*/*',
'accept-language': 'de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7',
'content-type': 'application/json',
'referer': 'https://' + API_HOST + '/studio/'+STUDIO["hash"],
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
'x-ms-web-context': '/studio/'+STUDIO["hash"],
'x-nox-client-type': 'WEB',
'x-nox-web-context': '',
'x-public-facility-group': PUBLIC_FACILITY_GROUP,
'x-tenant': TENANT,
}
r = S.get(f'https://{API_HOST}/nox/v1/studios/{STUDIO["int"]}/utilization/v2/today', headers=headers)
if r.status_code != 200:
await login()
r = S.get(f'https://{API_HOST}/nox/v1/studios/{STUDIO["int"]}/utilization/v2/today', headers=headers)
r.raise_for_status()
return r.json()
@bot.event
async def on_ready():
print(f"We have logged in as {bot.user}")
@bot.slash_command(description="Show today's utilization")
async def util(ctx):
try:
if UTILIZATION_VERSION == 1:
info = await get_util_v1()
elif UTILIZATION_VERSION == 2:
info = await get_util_v2()
embed = discord.Embed(title=f"{NAME} Utilization", color=16774219)
time = ""
status = ""
isCurrentStr = "current"
if UTILIZATION_VERSION == 1:
info = info["items"]
isCurrentStr = "isCurrent"
for item in info:
start = dateutil.parser.parse(item['startTime'], fuzzy=True).strftime("%H:%M")
end = dateutil.parser.parse(item['endTime'], fuzzy=True).strftime("%H:%M")
time += f"{start} -> {end}\n"
if item['percentage'] > 80:
status+=f"🔴 {item['percentage']}%\n"
elif item['percentage'] > 40:
status+=f"🟡 {item['percentage']}%\n"
else:
status+=f"🟢 {item['percentage']}%\n"
if item[isCurrentStr]:
break
embed.add_field(name="Location", value=f"```{NAME}```", inline=False)
embed.add_field(name="Time", value=f"```{time}```")
embed.add_field(name="Status", value=f"```{status}```")
embed.set_footer(text=f"{datetime.now().strftime('%d.%m.%Y %H:%M:%S')}")
await ctx.respond(embed=embed)
except Exception as e:
print(e)
await ctx.respond(f"Error")
@bot.slash_command(description="Show the current Utilization")
async def now_util(ctx):
try:
if UTILIZATION_VERSION == 1:
info = await get_util_v1()
elif UTILIZATION_VERSION == 2:
info = await get_util_v2()
embed = discord.Embed(title=f"{NAME} Utilization", color=16774219)
time = ""
status = ""
isCurrentStr = "current"
if UTILIZATION_VERSION == 1:
info = info["items"]
isCurrentStr = "isCurrent"
for item in info:
if not item[isCurrentStr]:
continue
if item['percentage'] > 80:
status = f"🔴 {item['percentage']}%"
elif item['percentage'] > 40:
status = f"🟡 {item['percentage']}%"
else:
status = f"🟢 {item['percentage']}%"
break
embed.add_field(name="Location", value=f"```{NAME}```", inline=False)
embed.add_field(name="Status", value=f"```{status}```")
embed.set_footer(text=f"{datetime.now().strftime('%d.%m.%Y %H:%M:%S')}")
await ctx.respond(embed=embed)
except Exception as e:
print(e)
await ctx.respond(f"Error")
if __name__ == "__main__":
bot.run(os.environ['DISCORD_BOT_TOKEN'])