-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstageSolver.py
184 lines (141 loc) · 6.73 KB
/
stageSolver.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from abc import ABC, abstractmethod
from urllib.parse import urlparse, parse_qs
import requests
import logging
import os
startUrlTemplate = "https://reset.inso.tuwien.ac.at/asetest/assignment/{studentId}/token"
testcaseUrlTemplate = "https://reset.inso.tuwien.ac.at/asetest/assignment/{studentId}/stage/{stage}/testcase/{testcase}?token={token}"
pdfUrlTemplate = "https://reset.inso.tuwien.ac.at/asetest/assignment/{studentId}/stage/{stage}/pdf?token={token}"
class StageSolver:
def __init__(self, sutdentId, stageImplementations=[], outdir="out", startStage = 1, token=None):
if not sutdentId:
raise ValueError("sutdentId")
self.studentId = sutdentId
self.stageImplementations = stageImplementations
self.outdir = outdir
self.testcase = 1
self.startStage = 1 if startStage is None else startStage
self.startToken = token
self.linkToNextTask = None
self.initialTokenUrl = startUrlTemplate.format(studentId=self.studentId)
self.session = requests.Session()
# Download assignemnt for first stage
def getInitialAssignment(self):
logging.info("downloading initial assignment / stage {}".format(1))
currentToken = self.__getToken()
self.__getAssignment(self.studentId, 1, currentToken)
# steps are performed in a loop
# get token
# get pdf
# solve stage (get testcase, solve, repeat)
# get next token
def run(self):
self.testcase = 1
stage = self.startStage
currentToken = self.__getToken() if self.startToken is None else self.startToken
self.linkToNextTask = self.__testcaseUrl(self.studentId, stage, self.testcase, currentToken)
for stageImpl in self.stageImplementations[stage-1:]:
logging.info("starting stage {}".format(stage))
try:
(nextStageUrl, nextToken) = self.__solveStage(stageImpl, stage, self.linkToNextTask)
if not nextStageUrl:
logging.info("stage failed")
return
except Exception as ex:
logging.error("something went wrong - stopping")
logging.error(ex)
return
self.linkToNextTask = nextStageUrl
currentToken = nextToken
stage = stage+1
if 'finished' in self.linkToNextTask:
logging.info("!!!!!!!!! ALL STAGES SOLVED !!!!!!!!!\nFINAL URL: {}\nFINAL TOKEN:{}".format(self.linkToNextTask, currentToken))
break
logging.info("Stage {} cleared !!\n{border}\nNext url: {}\nNext Token: {}\n{border}".format(stage-1, self.linkToNextTask, currentToken, border="#"*30))
self.__getAssignment(self.studentId, stage, currentToken)
logging.info("all implemented stages ({}) solved".format(len(self.stageImplementations)))
# get testcase
# solve
# parse response - return or repeat
def __solveStage(self, solver, stageNo, startUrl):
if not isinstance(solver, Stage):
raise TypeError("given stage solutions not of type Stage")
solver: Stage = solver
url = startUrl
testcaseNo = 1
while True:
testcase = self.__getTestcase(url)
logging.info("testcase {}: {}".format(testcaseNo, testcase))
answer = solver.solveTask(testcase)
logging.info("answer: {}".format(answer))
response = self.__postAnswer(url, answer)
if response.status_code == 202:
# next testcase or next stage
logging.info("testcase solved")
logging.debug("response: {}".format(response.text))
(s,tc,to, nextUrl) = self.__parseResponse(response)
url = nextUrl
testcaseNo = int(tc)
logging.debug("s: {}, tc: {}, to: {}".format(s,tc,to))
if int(s) > stageNo or (s == -1 and tc == -1):
# stage finished, return url and token for next stage
return (url, to)
else:
logging.error("testcase {} failed: {}".format(testcase, response.text))
return (False, False)
#parses stage, testcase and token from success URL
def __parseResponse(self, response):
json = response.json()
linkToNextTask = json['linkToNextTask']
o = urlparse(linkToNextTask)
path = o.path.split("/")
query = o.query
querydict = parse_qs(query)
if 'token' in querydict:
nextToken = querydict['token'].pop()
else:
logging.error("something went wrong, no token in valid response")
if len(path) > 3:
nextTestcase = path[-1]
nextStage = path[-3]
if 'finished' in path:
logging.debug("!!!!!!!!! ALL STAGES SOLVED !!!!!!!!!\nFINAL URL: {}\nFINAL TOKEN:{}".format(linkToNextTask,nextToken))
return (-1, -1, nextToken, linkToNextTask)
return (nextStage, nextTestcase, nextToken, linkToNextTask)
# get testcase
def __getTestcase(self, url):
logging.debug("GET testcase {}".format(url))
r = self.session.get(url)
res = r.json()
logging.debug(res)
return res
#submit answer
def __postAnswer(self, url, answer):
logging.debug("POST answer {}".format(answer))
r = self.session.post(url, json=answer)
logging.debug("HTTP {} - {}: {}".format(r.status_code, r.reason, r.text))
return r
# queries current assignment document and saves it
def __getAssignment(self, studentId, stage, token):
logging.info("Downloading assignment for stage {}".format(stage))
logging.debug("GET {}".format(self.__pdfUrl(studentId, stage, token)))
r = self.session.get(self.__pdfUrl(studentId, stage, token))
dest = os.path.join(self.outdir, "assignment{}.pdf".format(stage))
with open(dest, "wb") as f:
f.write(r.content)
logging.debug("file {} written".format(dest))
# GETs initial token
def __getToken(self):
logging.debug("GET initial token")
r = self.session.get(startUrlTemplate.format(studentId=self.studentId))
res = r.json()
logging.debug(res)
return res['token']
def __testcaseUrl(self, studentId, stage, testcase, token):
return testcaseUrlTemplate.format(stage=stage, studentId=studentId, testcase=testcase, token=token)
def __pdfUrl(self, studentId, stage, token):
return pdfUrlTemplate.format(stage=stage, studentId=studentId, token=token)
class Stage(ABC):
@abstractmethod
def solveTask(testcase: dict):
pass