def getProblemList(self, cid): ctype = globalVar.BASE_CONF.get('contest', 'ctype') cpass = globalVar.BASE_CONF.get('contest', 'cpass') data = {'id': cid, 'type': ctype} pwdProbleUrl = globalVar.OJ_CONF.get('urls', 'pwdProblems') resp = RequestUtil.doGet(pwdProbleUrl, data) \ if cpass == '1' else RequestUtil.doGet(AojApi.getUrl('problems'), data) # 解析网页数据 soup = BeautifulSoup(resp.text, "lxml") # 仅获取题目和id pList = [] allProblemDiv = soup.find_all('div', id=re.compile(r'title_\d*')) if not allProblemDiv: PrintUtil.warn("题目空了,你可能已经AK了.") sys.exit(0) for pdiv in allProblemDiv: p = Problem() p.pid = re.sub("\D", "", pdiv['id']) title = pdiv.find('div', class_='nav').string.split('.')[1].strip() tempStrs = title.split('(') p.score = '分数:' + re.sub("\D", "", tempStrs[len(tempStrs) - 1]).strip() p.title = title.split('(')[0].strip() pList.append(p) # 按分数排序 pList = sorted(pList, key=lambda pList: pList.score) return pList
def getPassedDetail(self, cid, pid): resp = RequestUtil.doGet(AojApi.getUrl('passedProblem'), {'id': cid}) soup = BeautifulSoup(resp.text, "lxml") p = soup.find('div', class_='nav', text=re.compile(r'.*' + pid + '.*')) if not p: PrintUtil.error("没有该题目...") sys.exit(0) infolist = [ 'title', 'content', 'descr_input', 'descr_output', 'ex_input', 'ex_output', 'code', 'score' ] j = 0 problem = Problem() problem.pid = '' problem.timeAndMem = '' for i in range(0, 31): s = '' if p.string is not None: s = p.string elif p.pre is not None: s = p.pre.string elif p.span is not None: s = p.span.string elif p.textarea is not None: s = p.textarea.string if s is not None and s.strip() != '': setattr(problem, infolist[j], s.strip()) j = j + 1 p = p.next_sibling return problem
def getProblemInfo(self, cid, pid): ctype = globalVar.BASE_CONF.get('contest', 'ctype') cpass = globalVar.BASE_CONF.get('contest', 'cpass') data = {'id': cid, 'type': ctype} pwdProbleUrl = globalVar.OJ_CONF.get('urls', 'pwdProblems') resp = RequestUtil.doGet(pwdProbleUrl, data) \ if cpass == '1' else RequestUtil.doGet(AojApi.getUrl('problems'), data) # 解析网页数据 soup = BeautifulSoup(resp.text, "lxml") pdiv = soup.find('div', id='title_' + pid) if not pdiv: PrintUtil.error("你已经AC了或者没有该题目") sys.exit(0) p = Problem() p.pid = re.sub("\D", "", pdiv['id']) p.title = pdiv.find('div', class_='nav').string.split('.')[1].strip() p.timeAndMem = pdiv.find('div', class_='common').string.strip() contentDiv = pdiv.find_all('div') p.content = contentDiv[3].pre.string p.descr_input = contentDiv[5].pre.string p.descr_output = contentDiv[7].pre.string p.ex_input = contentDiv[9].pre.string p.ex_output = contentDiv[11].pre.string p.code = '' return p
def getProblemInfo(self, cid, pid): resp = RequestUtil.doGet( AojApi.getUrl('problemBaseUrl') + cid + '/problem/' + pid) soup = BeautifulSoup(resp.text, "lxml") p = Problem() p.pid = pid p.title = soup.find('h3', class_='panel-title').string.split(':')[1] pDiv = soup.find('div', id='tl-problem-content') limitDiv = pDiv.find('div', class_='limit') limitSpan = limitDiv.find_all('span') p.timeAndMem = '时间限制: ' + limitSpan[0].string.strip() \ + ' 单点时限: '+ limitSpan[1].string.strip() \ + ' 内存限制: '+ limitSpan[2].string.strip() desDiv = pDiv.find('dl', class_='des') tagNum = 0 for tagNode in desDiv.div.contents: if tagNode.name == 'h3': tagNum = tagNum + 1 if tagNum == 1: p.content = p.content + tagNode.text.strip() elif tagNum == 2: p.descr_input = p.descr_input + tagNode.text.strip() elif tagNum == 3: p.descr_output = p.descr_output + tagNode.text.strip() p.ex_input = desDiv.find_all('dd')[0].pre.string.strip() p.ex_output = desDiv.find_all('dd')[1].pre.string.strip() return p
def isLogin(self): resp = RequestUtil.doGet(url=AojApi.getUrl('isLogin')) soup = BeautifulSoup(resp.text, "lxml") isNotLogin = soup.find_all(text='登录') if isNotLogin: return False else: return True
def isLogin(self): is_login_url = AojApi.getUrl('isLogin') resp = RequestUtil.doGet(url=is_login_url) soup = BeautifulSoup(resp.text, "lxml") islogin = soup.find_all(text='用户名') if islogin: return True else: return False
def saveContestInfo(self, cid): PrintUtil.info('正在获取比赛信息...') resp = RequestUtil.doGet(AojApi.getUrl('contest')) jdata = json.loads(resp.text) datalist = jdata.get('list') ctype = '1' # 类型 ...1代表c 0表示java cpass = '******' # 是否需要密码 0为不需要 # 根据比赛id查找比赛类型 for data in datalist: if str(data['id']) == cid: ctype = data['isjava'] break # 判断是否需要密码 data = {'id': cid, 'type': ctype} needPasswordTest = RequestUtil.doGet(AojApi.getUrl('problems'), data) # Struts Problem Report页面报错编码不是utf-8 if 'ISO-8859-1' in needPasswordTest.encoding: PrintUtil.error('没有该比赛 -_-\"') return try: # 如果不需要密码就没有这个头信息会抛异常,比之前的解析内容判断速度快,虽然不雅 hasKeyContentLength = needPasswordTest.headers['Content-Length'] cpass = '******' passwd = input(termcolor.colored(u'你需要输入密码参加该比赛: ', 'green')) joindata = {'password': passwd, 'id': cid} passwdisRight = RequestUtil.doGet(AojApi.getUrl('pwdProblems'), joindata) if passwdisRight.text == 'no': PrintUtil.error('密码错误!') return except: pass # 保存比赛信息 globalVar.BASE_CONF.set('contest', 'cid', cid) globalVar.BASE_CONF.set('contest', 'ctype', ctype) globalVar.BASE_CONF.set('contest', 'cpass', cpass) with open(globalVar.BASE_PATH + 'base.conf', 'w') as fw: globalVar.BASE_CONF.write(fw) self.saveProblemList() PrintUtil.info("设置比赛成功! 'coj list -p' 显示题目列表\n")
def login(self, username, password): data = {'email': username, 'passwd': password} resp = RequestUtil.doPost(url=AojApi.getUrl('login'), data=data) jdata = json.loads(resp.text) if jdata['code'] == 0 and jdata['response']['message'] == 'success': return True else: PrintUtil.error(jdata['errorMessage']) return False
def getLoginData(self, username, password): image = Image.open( BytesIO(RequestUtil.doGet(AojApi.getUrl('captcha')).content)) vcode = pytesseract.image_to_string(image) data = { 'user.username': username, 'user.userpassword': password, 'verifycode': vcode } return data
def getProblemList(self, cid): resp = RequestUtil.doGet( AojApi.getUrl('problemBaseUrl') + cid + '/problems') soup = BeautifulSoup(resp.text, "lxml") problemTr = soup.find_all('tbody') pList = [] for pInfo in problemTr: pTd = pInfo.find_all('td') p = Problem() p.pid = pTd[1].find('a')['href'].split('/')[-1] p.title = pTd[1].find('a').string.strip() p.desc = '通过率:' + pTd[2].string.strip( ) + ' 提交人数:' + pTd[4].string.strip() pList.append(p) return pList
def getContestList(self, containPassed): resp = RequestUtil.doGet(AojApi.getUrl('contest')) jdata = json.loads(resp.text) datalist = jdata.get('list') contestList = [] for data in datalist: if data['status'] == 'running' or containPassed: c = Contest() c.cid = data['id'] if data['isjava'] == '1': c.ctype = 'c' else: c.ctype = 'java' c.title = data['papername'] c.endTime = data['endtime'] c.teacherName = data['teachername'] contestList.append(c) return contestList
def getPassedList(self, cid): resp = RequestUtil.doGet(AojApi.getUrl('passedProblem'), {'id': cid}) soup = BeautifulSoup(resp.text, "lxml") titles = soup.find_all('div', class_='nav') if not titles: PrintUtil.error("你还没有做过此比赛的题目 :)") sys.exit(0) problemList = [] for t in titles: title = t.string.strip().split('.')[1] tempStrs = title.split('(') p = Problem() p.score = int(re.sub("\D", "", tempStrs[len(tempStrs) - 1])) p.title = title.split('(')[0].strip() p.pid = t.string.strip().split('.')[0] problemList.append(p) return problemList
def login(self, username, password): # 验证码识别率较低..索性尝试5次 tryloginTime = 5 while (tryloginTime > 0): resp = RequestUtil.doPost(url=AojApi.getUrl('login'), data=self.getLoginData( username, password)) soup = BeautifulSoup(resp.text, "lxml") divlist = soup.find_all('div', class_='user') if len(divlist) > 3: info = divlist[3].font.string if info != "验证码有误": PrintUtil.error(info) return False else: return True tryloginTime = tryloginTime - 1 if tryloginTime <= 0: PrintUtil.error("oooops...验证码识别失败,再试试?")
def getContestList(self, containPassed): #todo 加上当前正在进行的比赛 contestList = [] if containPassed: resp = RequestUtil.doGet(AojApi.getUrl('pastContest')) soup = BeautifulSoup(resp.text, "lxml") liList = soup.find_all('li', class_='md-summary') for pli in liList: contest = Contest() infoDiv = pli.find('div', class_='md-summary-cnt') problemInfo = infoDiv.find('a') contest.title = problemInfo.string.strip() contest.cid = problemInfo['href'].split('/')[-1] contest.ctype = 'All' endTimeP = pli.find('p', class_='md-summary-etime') contest.endTime = endTimeP.find('span', class_='htzc').string.strip() contest.desc = '报名人数: ' + infoDiv.find( 'p', class_='hiho-member').text.strip() contestList.append(contest) return contestList[::-1]
def getRankingList(self, cid): rankData = {"id": cid} resp = RequestUtil.doGet(AojApi.getUrl('rank'), rankData) soup = BeautifulSoup(resp.text, "lxml") rankingTr = soup.find_all('tr', id=re.compile('\d*')) if not rankingTr: PrintUtil.error("没有该比赛排名...重新选择比赛") sys.exit(0) rList = [] for tr in rankingTr: stu = UserInfo() td = tr.find_all('td') stu.rank = td[0].div.string stu.username = td[1].div.string stu.name = td[2].div.string stu.stuid = td[3].div.string stu.college = td[4].div.string stu.major = td[5].div.string stu.score = td[6].div.string stu.subTime = td[7].div.string rList.append(stu) return rList
def submitCode(self, code, pid): subData = { 'answer': code, 'id': pid, 'type': globalVar.BASE_CONF.get('contest', 'ctype') } resp = RequestUtil.doPost(AojApi.getUrl('submitCode'), subData) # {"id":"125","result":"Wrong Answer.","score":0,"time":"21:34:35"} try: jdata = json.loads(resp.text) result = jdata['result'] score = jdata['score'] time = jdata['time'] color = "red" if result == "Answer Correct.": color = "green" print( termcolor.colored(result, color) + '\n' + "得分:" + termcolor.colored(str(score), color) + '\n' + "提交时间:" + termcolor.colored(time, color)) except: PrintUtil.error('oops!提交出错了,请重新提交. *_*.')