-
Notifications
You must be signed in to change notification settings - Fork 0
/
tutorcode_api.py
61 lines (54 loc) · 1.63 KB
/
tutorcode_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
import requests
import time
def fetch_data(id):
url = "http://api.tutorcode.org/item/" + str(id)
headers = {
"API-KEY": "tutorcode_api_key_temp_52312"
}
retry_cnt = 0
while True:
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()
else:
print(response.status_code)
retry_cnt += 1
time.sleep(1)
if retry_cnt >= 5:
break
return {'status': 'failed'}
def get_testcase(problem_id, case_id):
url = f"http://api.tutorcode.org/testcase/{problem_id}/{case_id}"
headers = {
"API-KEY": "tutorcode_api_key_temp_52312"
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()
else:
print(response.status_code)
return {'status': 'failed'}
def judge(id, code):
url = "http://api.tutorcode.org/judge"
headers = {
"API-KEY": "tutorcode_api_key_temp_52312",
'Content-Type': 'application/json',
}
payload = {
'code': code,
'id': id,
}
response = requests.post(url, headers=headers, json=payload)
if response.status_code == 200:
return response.json()
else:
print(response.status_code)
return {'status': 'failed'}
if __name__ == "__main__":
import sys
if sys.argv[1] == 'fetch':
print(fetch_data(int(sys.argv[2])))
elif sys.argv[1] == 'testcase':
print(get_testcase(int(sys.argv[2]), int(sys.argv[3])))
else:
print(judge(int(sys.argv[2]), open(sys.argv[3]).read()))