-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus.py
43 lines (41 loc) · 1.32 KB
/
status.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
import os
import time
import requests
import json
if __name__ == "__main__":
if not os.path.exists("data"):
os.mkdir("data")
time_now = int(time.time() * 1000)
if os.path.exists("data/status.json"):
status = json.load(open("data/status.json"))
status = {k: v for k, v in status.items() if time_now - v["timestamp"] < 7 * 24 * 60 * 60 * 1000}
else:
status = {}
yymmddhhmm = time.strftime("%Y-%m-%dT%H:%M", time.localtime(time.time()))
res = None
e = None
ex = None
for i in range(5):
try:
res = requests.get("https://redenmc.com/api/status")
break
except Exception as e:
ex = e
if res is None:
status[yymmddhhmm] = {
"timestamp": time_now,
"status": 0,
"online_count": -1
}
json.dump(status, open("data/status.json", "w"))
print("Network failure.", ex)
exit(1)
else:
status[yymmddhhmm] = {
"timestamp": time_now,
"status": res.status_code,
"online_count": res.json()["online"]
}
json.dump(status, open("data/status.json", "w"))
print("Status updated. Time: " + yymmddhhmm + " Status: " + str(res.status_code) + " Online: "
+ str(res.json()["online"]) + " players.")