-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploader.py
155 lines (116 loc) · 4.65 KB
/
uploader.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
import time
import requests
import random
from pathlib import Path
SESSION_ID = ':)'
CSRFTOKEN = ':)'
TASK_ID = 1448
LOAD_SIZE = 300
def get_request(url):
request = requests.get(url,
cookies={
'sessionid': SESSION_ID
})
time.sleep(1)
return request
def upload_solution(filename, content):
form_data = {
'is_sandbox': (None, 1),
'language': (None, 1),
'solution': (filename, content, 'text/plain')
}
url = f'https://cups.online/api_v2/task/{TASK_ID}/upload_solution/'
request = requests.post(url,
cookies={
'sessionid': SESSION_ID,
'csrftoken': CSRFTOKEN
},
headers={
'Referer': f'https://cups.online/ru/workareas/technocup-22/767/{TASK_ID}?is_sandbox=1',
'X-CSRFToken': CSRFTOKEN
},
files=form_data)
time.sleep(1)
return request
def get_submission_result(submission_id):
url = f'https://cups.online/api_v2/solution/{submission_id}/test_results/'
request = get_request(url)
return request.json()
def get_submissions(page_size=20):
url = f'https://cups.online/api_v2/task/{TASK_ID}/uploaded_solutions/?is_sandbox=1&page_size={page_size}'
request = get_request(url)
return request.json()
def parse_test(submission):
result = get_submission_result(submission)[0]['output']
if len(result) > LOAD_SIZE:
result = result[:LOAD_SIZE]
return result
def get_random_file_name(extension='.py'):
return f'{random.randrange(1, 10 ** 20):020}{extension}'
def get_submission_id(filename):
submissions = get_submissions()['results']
for submission in submissions:
if submission['real_filename'] == filename and submission['state'] == 'Проверено':
return submission['id']
return -1
def get_file_size(filename):
tmp_name = get_random_file_name()
upload_solution(tmp_name, f'print(len(open("{filename}", "r").read()))')
submission_id = -1
while submission_id == -1:
submission_id = get_submission_id(tmp_name)
filesize = parse_test(submission_id)
try:
filesize = int(filesize)
return filesize
except Exception:
return -1
def download_file(filename):
print(f'Downloading {filename}')
write_dir = Path(f'sources{filename}')
if write_dir.exists():
print('Already downloaded)')
return 0
filesize = get_file_size(filename)
if filesize == -1:
print(f'File not found: {filename}')
return -1
if not write_dir.parent.exists():
Path.mkdir(write_dir.parent, parents=True)
print(f'File size: {filesize} symbols')
total_submissions = (filesize + LOAD_SIZE - 1) // LOAD_SIZE
print(f'Expected submissions: {total_submissions}')
submissions = [[get_random_file_name(), '\n' * LOAD_SIZE, 0] for _ in range(total_submissions)]
uploaded_cnt = 0
while uploaded_cnt < total_submissions:
results = get_submissions(page_size=50)['results']
upload_this_iter = True
for i in range(total_submissions):
tmp_name, content, submission_id = submissions[i]
if submission_id > 0: # uploaded + parsed
continue
elif submission_id == 0: # not uploaded
if upload_this_iter:
r = upload_solution(tmp_name,
f'print(open("{filename}", "r").read()[{i * LOAD_SIZE}:].replace(" ", "§"))')
if r.status_code == 200 and r.json()['details'] == 'ok':
submissions[i][2] = -1
else:
upload_this_iter = False
print(r.status_code, r.content, r.text)
else: # uploaded
for res in results:
if res['real_filename'] == tmp_name:
if res['state'] == 'Проверено':
submissions[i][2] = res['id']
submissions[i][1] = parse_test(res['id']).replace('§', ' ')
uploaded_cnt += 1
print(f'Parsed {uploaded_cnt} of {total_submissions}')
result = ''
for tmp_name, content, submission_id in submissions:
result += content
with open(write_dir, 'wt') as out:
print(result, file=out, end='')
return 0
for file in open('download_files.txt', 'rt'):
download_file(file.strip())