diff --git a/robots/hduoj.py b/robots/hduoj.py new file mode 100644 index 0000000..2ab1dea --- /dev/null +++ b/robots/hduoj.py @@ -0,0 +1,155 @@ +# coding=utf-8 +import re +import requests +from .robot import Robot +from .exceptions import AuthFailed, RequestFailed, RegexError, SubmitProblemFailed +from .utils import Language, Result + + +class HduojRobot(Robot): + def check_url(self, url): + regex = r"^http://acm.hdu.edu.cn/showproblem.php\?pid=\d{4}$" + return re.compile(regex).match(url) is not None + + def login(self, username, password): + r = self.post("http://acm.hdu.edu.cn/userloginex.php?action=login", + data={"username": username, + "userpass": password, + "login": "Sign In"}, + headers={"Content-Type": "application/x-www-form-urlencoded", + "Referer": "http://acm.hdu.edu.cn/"}) + # 登陆成功会重定向到首页,否则200返回错误页面 + if r.status_code != 302: + raise AuthFailed("Failed to login Hduoj") + + self.cookies = dict(r.cookies) + + @property + def is_logged_in(self): + r = self.get("http://acm.hdu.edu.cn/control_panel.php", cookies=self.cookies) + # 登录状态是200,否则302到登陆页面 + return r.status_code == 200 + + def get(self, url, headers=None, cookies=None, allow_redirects=False): + r = super().get(url, headers=headers, cookies=cookies, allow_redirects=allow_redirects) + r.encoding = "gb2312" + return r + + def _regex_page(self, url, regex): + r = self.get(url) + self.check_status_code(r) + data = {} + for k, v in regex.items(): + items = re.compile(v).findall(r.text) + if not items: + if k == "spj": + data[k] = False + elif k == "hint": + data["hint"] = None + else: + raise RegexError("No such data") + if k == "samples": + data[k] = [{"input": items[0], "output": items[1]}] + elif items: + if k == "spj": + data[k] = True + else: + data[k] = self._clean_html(items[0]) + data["memory_limit"] = int(data["memory_limit"]) // 1024 + data["time_limit"] = int(data["time_limit"]) + + return data + + def get_problem(self, url): + if not self.check_url(url): + raise RequestFailed("Invaild Hduoj url") + regex = {"title": r"
([\s\S]*)").findall(r.text))) + + return {"result": result, "cpu_time": cpu_time, "memory": memory, + "info": {"result_text": self._clean_html(data[0][1])}, "error": error} + diff --git a/robots/pat.py b/robots/pat.py index 110bd0c..fe84101 100644 --- a/robots/pat.py +++ b/robots/pat.py @@ -6,9 +6,12 @@ from .utils import Language, Result class PATRobot(Robot): - def __init__(self, cookies=None): + def __init__(self, cookies=None, token=""): super().__init__(cookies=cookies) - self.token = "" + self.token = token + + def save(self): + return {"cookies": self.cookies, "token": self.token} def check_url(self, url): regex = r"^https://www.patest.cn/contests/pat-(a|b|t)-practise/1\d{3}$" @@ -56,7 +59,13 @@ class PATRobot(Robot): "submit_url": r'