-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOTOG_API.py
139 lines (109 loc) · 3.37 KB
/
OTOG_API.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
import requests
import json
_otog_host = ""
_otog_api_host = ""
def init(host: str, api_host: str):
global _otog_host, _otog_api_host
_otog_host = host
_otog_api_host = api_host
def getNumProblems():
try:
response = requests.get(f"{_otog_api_host}/problem")
except:
return -1
if response.status_code != 200:
return -1
else:
return len(response.json())
def getRanking():
try:
response = requests.get(f"{_otog_api_host}/user")
except:
return -1
if response.status_code != 200:
return -1
else:
data = response.json()
ranking = list()
for user in data:
if user["role"] == "user" and user["rating"] != None:
ranking.append((user["rating"], user["showName"]))
ranking.sort(key=lambda x: x[0])
ranking.reverse()
return ranking
def getRankingContest(nCon: int):
try:
response = requests.get(f"{_otog_api_host}/contest/{nCon}/scoreboard")
except:
return -1
if response.status_code != 200:
return -1
else:
data = response.json()
result = dict()
result["name"] = data["name"]
result["problem"] = list()
for problem in data["problems"]:
result["problem"].append(
(f"({problem['id']}){problem['name']} : {problem['score']} คะแนน")
)
result["ranking"] = list()
for user in data["users"]:
if user["role"] == "user":
scores = []
timeUse = 0
for sc in user["submissions"]:
scores.append(sc["score"])
timeUse += sc["timeUsed"]
result["ranking"].append((user["showName"], scores, timeUse))
result["ranking"].sort(key=lambda x: sum(x[1]))
result["ranking"].reverse()
return result
def getUserLife():
try:
response = requests.get(f"{_otog_api_host}/user/online")
except:
return -1
if response.status_code != 200:
return -1
else:
data = response.json()
outStr = ""
if len(data) == 0:
return 0
elif len(data) == 1:
outStr += f"{data[0]['showName']} (คนเหงา)"
elif len(data) <= 7:
for i in range(len(data) - 1):
outStr += data[i]["showName"] + ", "
outStr = outStr[:-1] + "และ" + data[-1]["showName"]
else:
outStr += f"คน {len(data)} คน"
return outStr
def contestNow():
# with open("TestContent.txt","r",encoding="utf8") as f:
# x = json.loads(f.read())
# return x
response = requests.get(f"{_otog_api_host}/contest/now")
if response.status_code != 200:
return -1
else:
try:
data = response.json()
except:
return -1
if data["currentContest"] is None:
return -1
thisContest = dict()
for k in data["currentContest"]:
if k != "problems":
thisContest[k] = data["currentContest"][k]
return thisContest
def isWorking() -> bool:
try:
response = requests.get(_otog_host, timeout=5)
except:
return False
return 200 <= response.status_code < 300
if __name__ == "__main__":
print(isWorking())