-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMooshak.py
236 lines (183 loc) · 7.02 KB
/
Mooshak.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env python2
#
# (C) 2011 David Miguel de Araujo Serrano
#
# This file is part of pyMooshak.
#
# pyMooshak is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyMooshak is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PyMooshak. If not, see <http://www.gnu.org/licenses/>.
import urllib
import os
import sys
import pycurl
from StringIO import StringIO
from BeautifulSoup import BeautifulSoup
class Mooshak:
session = None
base_url = None
curl = None
quote = ':./?=&'
def __init__(self, base_url):
self.base_url = base_url
self.curl = pycurl.Curl()
self.curl.setopt(pycurl.COOKIEFILE, "")
def _sink(self, buf):
pass
def _get_req_handler(self, req = "", data = None, headers = [],
read_fun = None):
self.curl.setopt(pycurl.URL, urllib.quote(
self.base_url + 'cgi-bin/execute/' + req, self.quote))
if read_fun != None:
self.curl.setopt(pycurl.WRITEFUNCTION, read_fun)
else:
self.curl.setopt(pycurl.WRITEFUNCTION, self._sink)
if data != None:
self.curl.setopt(pycurl.POST, 1)
self.curl.setopt(pycurl.POSTFIELDS, data)
else:
self.curl.setopt(pycurl.POST, 0)
self.curl.setopt(pycurl.HTTPHEADER, headers)
self.curl.perform()
def _new_session(self):
if self.session != None:
return
self.curl.setopt(pycurl.URL, urllib.quote(
self.base_url + 'cgi-bin/execute', self.quote))
self.curl.setopt(pycurl.WRITEFUNCTION, self._sink)
self.curl.setopt(pycurl.POST, 0)
self.curl.setopt(pycurl.FOLLOWLOCATION, 1)
self.curl.perform()
self.session = self.curl.getinfo(
pycurl.EFFECTIVE_URL).split("/")[-1]
def _login(self, contest, user, password):
if (self.session == None):
self._new_session();
# Do the actual login
data_dict = {}
data_dict['command'] = 'login'
data_dict['arguments'] = ''
data_dict['contest'] = contest
data_dict['user'] = user
data_dict['password'] = password
self._get_req_handler(self.session,
urllib.urlencode(data_dict));
def _guest_session(self, contest):
self._new_session()
data_dict = { 'contest': contest };
self._get_req_handler(self.session + '?login',
urllib.urlencode(data_dict))
self._get_req_handler(self.session + '?guest')
pass
def _get_submission_response(self, html):
soup = BeautifulSoup(html)
h3 = soup.findAll("h3")[0].string;
return h3
def _get_submission_list(self, html):
soup = BeautifulSoup(html)
table = soup.findAll('table')[0]
trows = table.findAll('tr')[1:]
ret = []
for row in trows:
divs = row.findAll('td')
dict = {}
dict['number'] = divs[0].findAll('b')[0].string
dict['time'] = divs[1].string
dict['team'] = divs[3].findAll('font')[0].string
dict['problem'] = divs[4].findAll('font')[0].string
dict['language']= divs[5].string
dict['result']= divs[6].findAll('font')[0].string
dict['state']= divs[7].findAll('font')[0].string
ret.append(dict)
return ret
"""
Submits a solution. Returns a string containing the submissions result.
Note that to know if the submission was accepted (or not), you must
call get_last_result()
"""
def submit(self, contest, user, password, problem, file_path):
self._login(contest, user, password)
params = { 'command': 'analyze',
'problem': problem,
'program' : file(file_path, 'rb'),
'analyze':'Submit'
}
self.curl.setopt(pycurl.URL, urllib.quote(
self.base_url + 'cgi-bin/execute/' + self.session, self.quote))
self.curl.setopt(pycurl.HTTPPOST, [
('command', 'analyze'),
('problem', problem),
('program', (self.curl.FORM_FILE, file_path)),
('analyze', 'Submit')])
b = StringIO()
self.curl.setopt(pycurl.WRITEFUNCTION, b.write)
self.curl.perform()
return self._get_submission_response(b.getvalue())
"""
Returns a dictionary of contests in the current server.
The key is the contest real name, the value is the contest description
"""
def list_contests(self):
self._new_session()
b = StringIO()
self._get_req_handler(self.session + '?login', read_fun = b.write)
soup = BeautifulSoup(b.getvalue())
contest_sel = soup.findAll('select')[-1]
contests_opt = contest_sel.findAll('option')
ret = {}
for opt in contests_opt[1:]:
ret[opt['value']] = str(opt.string).rstrip()
return ret
"""
Returns a dictionary of problems from a specified contest, in the current
server.
The key is the problem real name, the value is the problem description
"""
def list_problems(self, contest):
self._guest_session(contest)
b = StringIO()
self._get_req_handler(self.session + '?vtools', read_fun=b.write)
soup = BeautifulSoup(b.getvalue())
selects = soup.findAll('select')
probs_opt = selects[1].findAll('option')
ret = {}
for opt in probs_opt:
ret[opt['value']] = str(opt.string)
return ret
"""
Lists submissions made into a specific contest.
"""
def list_submissions(self, contest, page=0, lines=5):
self._guest_session(contest)
b = StringIO()
data = ('all_problems=on' +
'&page=' + str(int(page)) +
'&all_teams=on' +
'&type=submissions' +
'&lines=' + str(int(lines)) +
'&time=0' +
'&command=listing')
self._get_req_handler(self.session + '?split', data)
self._get_req_handler(self.session + '?' + data,
read_fun = b.write)
return self._get_submission_list(b.getvalue())
"""
Returns the last found result for a specified user on a specified contest
"""
def get_last_result(self, contest, user, maxlines=100):
subs = self.list_submissions(contest, 0, maxlines)
for s in subs:
suser = '_'.join(s['team'].split(' ')[1].split(' '))
if suser == user:
return s
return None
pycurl.global_init(pycurl.GLOBAL_ALL)