-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathtopcoder.py
161 lines (139 loc) · 6.33 KB
/
topcoder.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
# Python Version: 3.x
import onlinejudge.service
import onlinejudge.problem
import onlinejudge.submission
import onlinejudge.dispatch
import onlinejudge.implementation.utils as utils
import onlinejudge.implementation.logging as log
import re
import bs4
import requests
import urllib.parse
import posixpath
import json
import selenium
import time
@utils.singleton
class TopCoderService(onlinejudge.service.Service):
def login(self, get_credentials, session=None):
session = session or utils.new_default_session()
# NOTE: you can see this login page with https://community.topcoder.com/longcontest/?module=Submit
url = 'https://community.topcoder.com/longcontest/'
username, password = get_credentials()
data = {
'nextpage': 'https://www.topcoder.com/',
'module': 'Login',
'ha': username,
'pass': password,
'rem': 'on',
}
resp = utils.request('POST', url, session=session, data=data)
if 'longcontest' not in resp.url:
log.success('Success')
return True
else:
log.failure('Failure')
return False
def get_url(self):
return 'https://www.topcoder.com/'
def get_name(self):
return 'topcoder'
@classmethod
def from_url(cls, s):
# example: https://www.topcoder.com/
result = urllib.parse.urlparse(s)
if result.scheme in ('', 'http', 'https') \
and result.netloc in [ 'www.topcoder.com', 'community.topcoder.com' ]:
return cls()
class TopCoderLongContestProblem(onlinejudge.problem.Problem):
def __init__(self, rd, cd=None, compid=None, pm=None):
self.rd = rd
self.cd = cd
self.compid = compid
self.pm = pm
def get_url(self):
return 'https://community.topcoder.com/tc?module=MatchDetails&rd=' + str(self.rd)
def get_service(self):
return TopCoderService()
@classmethod
def from_url(cls, s):
# example: https://community.topcoder.com/longcontest/?module=ViewProblemStatement&rd=16997&pm=14690
# example: https://community.topcoder.com/longcontest/?module=ViewProblemStatement&rd=16997&compid=57374
# example: https://community.topcoder.com/longcontest/?module=ViewStandings&rd=16997
# example: https://community.topcoder.com/tc?module=MatchDetails&rd=16997
result = urllib.parse.urlparse(s)
if result.scheme in ('', 'http', 'https') \
and result.netloc == 'community.topcoder.com' \
and utils.normpath(result.path) in [ '/longcontest', '/tc' ]:
querystring = dict(urllib.parse.parse_qsl(result.query))
if 'rd' in querystring:
kwargs = {}
for name in [ 'rd', 'cd', 'compid', 'pm' ]:
if name in querystring:
kwargs[name] = int(querystring[name])
return cls(**kwargs)
def get_language_dict(self, session=None):
session = session or utils.new_default_session()
# at 2017/09/21
return {
'Java': { 'value': '1', 'description': 'Java 8' },
'C++': { 'value': '3', 'description': 'C++11' },
'C#': { 'value': '4', 'description': '' },
'VB': { 'value': '5', 'description': '' },
'Python': { 'value': '6', 'description': 'Pyhton 2' },
}
def submit(self, code, language, kind='example', session=None):
assert kind in [ 'example', 'full' ]
session = session or utils.new_default_session()
# module=Submit
url = 'https://community.topcoder.com/tc?module=MatchDetails&rd=%d' % self.rd
resp = utils.request('GET', url, session=session)
soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding), utils.html_parser)
path = soup.find('a', text='Register/Submit').attrs['href']
assert path.startswith('/') and 'module=ViewReg' in path
# module=ViewActiveContests
url = 'https://community.topcoder.com' + path
resp = utils.request('GET', url, session=session)
soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding), utils.html_parser)
path = [ tag.attrs['href'] for tag in soup.find_all('a', text='Submit') if ('rd=%d' % self.rd) in tag.attrs['href'] ]
if len(path) == 0:
log.error('link to submit not found: Are you logged in? Are you registered? Is the contest running?')
return None
assert len(path) == 1
path = path[0]
assert path.startswith('/') and 'module=Submit' in path
query = dict(urllib.parse.parse_qsl(urllib.parse.urlparse(path).query))
self.cd = query['cd']
self.compid = query['compid']
# module=Submit
submit_url = 'https://community.topcoder.com' + path
resp = utils.request('GET', submit_url, session=session)
soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding), utils.html_parser)
# post
url = 'https://community.topcoder.com/longcontest/'
language_id = self.get_language_dict(session=session)[language]['value']
data = {
'module': 'Submit',
'rd': self.rd,
'cd': self.cd,
'compid': self.compid,
'Action': 'submit',
'exOn': { 'example': 'true', 'full': 'false' }[kind],
'lid': language_id,
'code': code,
}
resp = utils.request('POST', url, session=session, data=data)
# check if module=SubmitSuccess
if 'module=SubmitSuccess' in resp.content.decode(resp.encoding):
url = 'http://community.topcoder.com/longcontest/?module=SubmitSuccess&rd={}&cd={}&compid={}'.format(self.rd, self.cd, self.compid)
log.success('success: result: %s', url)
return onlinejudge.submission.CompatibilitySubmission(url)
else:
# module=Submit to get error messages
resp = utils.request('GET', submit_url, session=session)
soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding), utils.html_parser)
messages = soup.find('textarea', { 'name': 'messages' }).text
log.failure('%s', messages)
return None
onlinejudge.dispatch.services += [ TopCoderService ]
onlinejudge.dispatch.problems += [ TopCoderLongContestProblem ]