From ec8b9941b2ea4b12ab4885663757b1247603e01c Mon Sep 17 00:00:00 2001 From: ltwy <1043218708@qq.com> Date: Fri, 4 Mar 2016 23:31:24 +0800 Subject: [PATCH] do some function and test --- robots/tests.py | 1 - robots/zoj.py | 64 ++++++++++++++++++++++++------------------------- tests.py | 14 +++++++++++ 3 files changed, 45 insertions(+), 34 deletions(-) delete mode 100644 robots/tests.py create mode 100644 tests.py diff --git a/robots/tests.py b/robots/tests.py deleted file mode 100644 index 9bad579..0000000 --- a/robots/tests.py +++ /dev/null @@ -1 +0,0 @@ -# coding=utf-8 diff --git a/robots/zoj.py b/robots/zoj.py index 0a3955b..9aad9d8 100644 --- a/robots/zoj.py +++ b/robots/zoj.py @@ -1,19 +1,19 @@ # coding=utf-8 import re import html -import requests +from .robot import Robot from .exceptions import AuthFailed,RequestFailed, RegexError, SubmitProblemFailed from .utils import Language -class Robot(object): +class ZOJRobot(Robot): def __init__(self, cookies=None): self.cookies = cookies if cookies is not None else {} - + # OK def check_url(self, url): regex = r"^http://acm.zju.edu.cn/onlinejudge/showProblem.do\?problemCode=\d{4}$" - return re.compile(regex).match(url) is not None; - + return re.compile(regex).match(url) is not None + #OK def login(self, username, password): url = r"http://acm.zju.edu.cn/onlinejudge/login.do" data = { @@ -25,18 +25,20 @@ class Robot(object): "Content-Type": "application/x-www-form-urlencoded", "Referer": "http://acm.zju.edu.cn/onlinejudge/login.do" } - r = self.post(url, data, headers); + r = self.post(url, data, headers) if r.status_code is not 302: - raise AuthFailed("Failed to login ZOJ!"); - self.cookies = dict(r.cookies); + raise AuthFailed("Failed to login ZOJ!") + self.cookies = dict(r.cookies) + #OK @property def is_logged_in(self): r = self.get("http://acm.zju.edu.cn/onlinejudge/editProfile.do", cookies = self.cookies) if re.compile(r"
|
|||\r|\n||").sub("", text)); + return self._decode_html(re.compile(r"|
|||\r|\n||").sub("", text)); def submit(self, url, language, code): if language == Language.C: - compiler_id = "3" + compiler_id = "1" elif language == Language.CPP: compiler_id = "2" else: - compiler_id = "10" - r = self.post(url, data={"utf8": "✓", "compiler_id": compiler_id, "code": code}, + compiler_id = "4" + regex = r"^http://acm.zju.edu.cn/onlinejudge/showProblem.do\?problemCode=\d{4}$" + problem_id = re.compile(regex).findall(url)[0][0] + r = self.post(url, data={"problemId": problem_id, "languageId": compiler_id, "source": code}, cookies=self.cookies, - headers={"Referer": "http://acm.zju.edu.cn/onlinejudge/submit.do?problemId="+, + headers={"Referer": "http://acm.zju.edu.cn/", "Content-Type": "application/x-www-form-urlencoded"}) - if r.status_code != 302: + if r.status_code != 200: raise SubmitProblemFailed("Failed to submit problem, url: %s, status code: %d" % (url, r.status_code)) - return str(re.compile(r"http://www.patest.cn/submissions/(\d+)").findall(r.headers["Location"])[0]) + return str(re.compile(r"Your source has been submitted. The submission id is \d+").findall(r.text)[0][0]) def _decode_html(self, text): """ @@ -96,15 +102,7 @@ class Robot(object): return html.unescape(text) def get_result(self, submission_id): - """ - 获取提交结果 - result和language按照utils中转换一下 - 返回值中info["error"]只有编译错误的时候才会为字符串,否则为None - :param submission_id: - :return: {"result": Result, "cpu_time": Int, "memory": Int, "info": {"error": None/String}} - """ - raise NotImplementedError() - + pass def check_status_code(self, response, status_code=200): """ 检查响应是否是指定的status code 否则引发异常 diff --git a/tests.py b/tests.py new file mode 100644 index 0000000..813f9e1 --- /dev/null +++ b/tests.py @@ -0,0 +1,14 @@ +# coding=utf-8 +import unittest +from robots.zoj import ZOJRobot + +class mytest(unittest): + def setUp(self): + pass + def tearDown(self): + pass + def testcheck_url(self): + self.assertEqual(ZOJRobot.check_url(r"http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1234"), True); + +if(__name__ == '__main__'): + unittest.main(); \ No newline at end of file