Repository navigation
Expand file tree
/
Copy pathAPI.py
More file actions
101 lines (80 loc) · 2.81 KB
/
Copy pathAPI.py
File metadata and controls
101 lines (80 loc) · 2.81 KB
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
import requests
import json
import time
import os
api_token = "sdlfzmqrpsauhojnbjxqyhhjjlpcqyli"
api_url = 'https://codeweekend.dev:3721/api/'
files_url = 'https://codeweekend.dev:81/'
headers = {
'authorization': f'Bearer {api_token}'
}
def show(inner_json):
print(json.dumps(inner_json, indent=2))
def get_scoreboard():
return requests.get(api_url + 'scoreboard', headers=headers).json()
def get_team_dashboard():
return requests.get(api_url + 'team_dashboard', headers=headers).json()
def get_test(task_id):
task_id_padded = '{:03d}'.format(task_id)
url = f'{files_url}{task_id_padded}.json'
return requests.get(url, headers=headers).content
# Returns at most 50 submissions
def get_team_submissions(offset=0, task_id=None):
url = f'{api_url}team_submissions?offset={offset}'
if task_id is not None:
url += f'&task_id={task_id}'
return requests.get(url, headers=headers).json()
def get_submission_info(submission_id, wait=False):
url = f'{api_url}submission_info/{submission_id}'
res = requests.get(url, headers=headers).json()
if 'Pending' in res and wait:
print('Submission is in Pending state, waiting...')
time.sleep(1)
return get_submission_info(submission_id)
return res
# Returns submission_id
def submit(task_id, solution):
res = requests.post(url=f'{api_url}submit/{task_id}',
headers=headers, files={'file': solution})
if res.status_code == 200:
return res.text
print(f'Error: {res.text}')
return None
def download_submission(submission_id):
import urllib.request
url = f'{api_url}download_submission/{submission_id}'
opener = urllib.request.build_opener()
opener.addheaders = headers.items()
urllib.request.install_opener(opener)
try:
file, _ = urllib.request.urlretrieve(url, "downloaded.txt")
except Exception as e:
print('Failed to download submission: ', e)
return None
content = open(file, "r").read()
os.remove(file)
return content
def update_display_name(new_name):
url = api_url + 'update_user'
data = {
'display_name': "TONacar",
'email': "",
'team_members': "Davit Badishyan"
}
return requests.post(url, json=data, headers=headers).content
# show(get_scoreboard())
# show(get_submission_info(427))
# show(get_team_dashboard())
# show(get_team_submissions())
# download_submission(476)
# get_test(1)
# update_display_name('Test 123')
for i in range(26, 51):
print("API ", i)
outp = "output " + '{:03d}'.format(i) + ".json"
f = open(outp)
data = json.load(f)
data = json.dumps(data, indent=4, separators=(',', ': '))
# print(data)
submit(i, data)
f.close()