Created
August 28, 2020 10:28
-
-
Save Ray-Eldath/0c78d773b64be3c6c6c75b2c09833373 to your computer and use it in GitHub Desktop.
AutoPOJ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
import click | |
import requests | |
from lxml import etree | |
def noi(uri): | |
return 'http://noi.openjudge.cn/' + uri | |
def login(email, password): | |
auth = requests.post(noi('/api/auth/login'), data={'email': email, 'password': password}) | |
print('login success with email ' + email) | |
return auth.cookies | |
def get_answer(chapter_dic, problem_id): | |
answer_url = 'https://raw.githubusercontent.com/hsefz2018/NOIP-openjudge/master/component_solution/{}/{}.cpp'.format( | |
chapter_dic, problem_id) | |
return requests.get(answer_url) | |
def submit_problem(contest_id, program_number, answer, credential): | |
submitted_response = requests.post(noi('/api/solution/submit'), | |
data={ | |
'contestId': contest_id, | |
'problemNumber': program_number, | |
'sourceEncode': 'base64', | |
'language': 'G++', | |
'source': base64.encodebytes(answer.text.encode('UTF-8')) | |
}, cookies=credential) | |
submitted = submitted_response.json() | |
if not submitted_response.ok: | |
print('\t -> ERROR [submitting {}]'.format(submitted["message"])) | |
else: | |
print('\t -> {} [{}]'.format(submitted["result"], submitted["message"])) | |
@click.command() | |
@click.option('--chapter', required=True, help='Chapter ID of problems, e.g. ch0101') | |
@click.option('--email', required=True, help='Email of your NOJ account.') | |
@click.option('--password', required=True, help='Password of your NOJ account.') | |
def auto_judge(chapter, email, password): | |
chapter_dic = '{}.{}'.format(int(chapter[2:4]), int(chapter[4:6])) | |
problems = requests.get(noi(chapter)).text | |
problem_ids = etree.HTML(problems).xpath('''//tbody/tr/td[@class='problem-id']//text()''') | |
credential = login(email, password) | |
for problem_id in problem_ids: | |
print('now processing: ' + problem_id) | |
submit = etree.HTML(requests.get(noi('{}/{}/submit'.format(chapter, problem_id)), cookies=credential).text) | |
contest_id = submit.xpath("//input[@name='contestId']/@value")[0] | |
program_number = submit.xpath("//input[@name='problemNumber']/@value")[0] | |
print('\t contest_id=' + contest_id) | |
print('\t program_number=' + program_number) | |
answer = get_answer(chapter_dic, problem_id) | |
if not answer.ok: | |
print('\t -> ERROR [answer_2 {}]'.format(answer.text)) | |
continue | |
submit_problem(contest_id, program_number, answer, credential) | |
if __name__ == '__main__': | |
auto_judge() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment