def get_raw(remote_oj, remote_id): problem = Controller(remote_oj).get_problem(remote_id, account=Account('robot4test', 'robot4test')) if problem and problem.status: problem.status = problem.status.value return '<xmp>' + str( json.dumps(problem.__dict__, sort_keys=True, indent=4)) + '</xmp>' return 'No Such OJ'
def get(self, request, remote_oj, remote_id, **kwargs): remote_oj = Controller.get_real_remote_oj(remote_oj) if not Controller.is_support(remote_oj): return Response(res_format('remote_oj not valid', status=Message.ERROR), status=status.HTTP_200_OK) if not remote_id.isalnum(): return Response(res_format('remote_id not valid', status=Message.ERROR), status=status.HTTP_200_OK) try: problem = Problem.objects.get(remote_oj=remote_oj, remote_id=remote_id) if problem.request_status in [ Spider_Problem.Status.STATUS_SUBMIT_FAILED.value, Spider_Problem.Status.STATUS_PROBLEM_NOT_EXIST.value, Spider_Problem.Status.STATUS_NO_ACCOUNT.value, Spider_Problem.Status.STATUS_PARSE_ERROR.value ]: get_problem_task.delay(problem.id) except ObjectDoesNotExist: try: problem = Problem( remote_oj=remote_oj, remote_id=remote_id, request_status=Spider_Problem.Status.STATUS_PENDING.value) problem.save() get_problem_task.delay(problem.id) except IntegrityError: return Response(res_format('System error', Message.ERROR), status=status.HTTP_200_OK) return Response(res_format(ProblemSerializer(problem).data), status=status.HTTP_200_OK)
def submit(self): if self.submission.retry_count > 10: return if self.submission.status == JudgeRequest.status['PENDING'] or \ self.submission.status == JudgeRequest.status['SEND_FOR_JUDGE_ERROR']: account = self.choose_account(self.submission.problem.remote_oj) # print('account', account, self.submission.problem.remote_oj) if not account: self.retry() raise SubmissionException success_submit = Controller.submit_code( self.submission.problem.remote_oj, Account(username=account.username, password=account.password), self.submission.source, self.submission.language, self.submission.problem.remote_id) # print('success_submit', success_submit) if success_submit: result = Controller.get_result( self.submission.problem.remote_oj, Account(username=account.username, password=account.password), self.submission.problem.remote_id) # print('result', result) # 结果返回有误 if not result: if self.submission.status == JudgeRequest.status[ 'SEND_FOR_JUDGE_ERROR']: self.submission.status = JudgeRequest.status['RETRY'] else: self.submission.status = JudgeRequest.status[ 'SEND_FOR_JUDGE_ERROR'] self.release_account(account.id) self.retry() raise Submission self.submission.remote_run_id = result.origin_run_id self.submission.judge_result = result.verdict self.submission.execute_memory = result.execute_memory self.submission.execute_time = result.execute_time self.submission.save() if self.waiting_for_judge(): print('submit_success') self.update_statistic() else: self.submission.status = JudgeRequest.status['RETRY'] self.release_account(account.id) self.retry() raise SubmissionException else: # 提交失败 if self.submission.status == JudgeRequest.status[ 'SEND_FOR_JUDGE_ERROR']: self.submission.status = JudgeRequest.status['RETRY'] else: self.submission.status = JudgeRequest.status[ 'SEND_FOR_JUDGE_ERROR'] self.retry() self.release_account(account.id) raise SubmissionException self.release_account(account.id)
def waiting_for_judge(self): for i in range(10): result = Controller.get_result_by_rid( self.submission.problem.remote_oj, self.submission.remote_run_id) if Controller.is_waiting_for_judge( self.submission.problem.remote_oj, result.verdict): self.submission.status = JudgeRequest.status['JUDGING'] # self.submission.retry_count = self.submission.retry_count + 1 self.submission.save() time.sleep(1) else: self.submission.judge_result = result.verdict self.submission.execute_memory = result.execute_memory self.submission.execute_time = result.execute_time self.submission.status = JudgeRequest.status['SUCCESS'] self.submission.save() return True return False
def submit(): source_code = request.files['source_code'] if request.form.get('remote_oj') and request.form.get('language') and request.form.get( 'remote_id') and source_code: remote_oj = request.form['remote_oj'] remote_id = request.form['remote_id'] language = request.form['language'] ans = False tries = 3 print('start') account = Account('robot4test', 'robot4test') while ans is False and tries > 0: tries -= 1 controller = Controller(str(remote_oj)) ans = controller.submit_code(pid=remote_id, account=account, code=source_code.read(), language=language) account.set_cookies(controller.get_cookies()) if ans is False: return "SUBMIT FAILED" controller = Controller(remote_oj) result = controller.get_result(account=account, pid=remote_id) account.set_cookies(controller.get_cookies()) print('end') tries = 5 while result.status == Result.Status.STATUS_RESULT and tries > 0: time.sleep(2) if Controller(remote_oj).is_waiting_for_judge(result.verdict): result = Controller(remote_oj).get_result_by_rid_and_pid(rid=result.origin_run_id, pid=remote_id) else: break tries -= 1 if result.status == Result.Status.STATUS_RESULT: return str(result.__dict__) return result.status.name return "Error"
def get(self, request, **kwargs): problems = Problem.objects.filter( request_status=Spider_Problem.Status.STATUS_CRAWLING_SUCCESS.value ).order_by('-update_time') if request.GET.get('remote_oj'): remote_oj = Controller.get_real_remote_oj( request.GET.get('remote_oj')) problems = problems.filter(remote_oj=remote_oj) if request.GET.get('remote_id'): problems = problems.filter( remote_id__contains=request.GET.get('remote_id')) if request.GET.get('title'): problems = problems.filter( title__contains=request.GET.get('title')) return Response(res_format( ProblemListSerializer(problems, many=True).data), status=status.HTTP_200_OK)
def test_get_problem_with_proxies(self): account = Account('robot4test', 'robot4test') WUST = Controller('WUST') self.assertIsNotNone(WUST) problem = WUST.get_problem(account=account, pid='1039') print(problem.__dict__) self.assertIsNotNone(problem) WUST = Controller('WUST', proxies='socks5://127.0.0.1:1086') self.assertIsNotNone(WUST) problem = WUST.get_problem(account=account, pid='1039') print(problem.__dict__) self.assertIsNotNone(problem)
def test_get_problem(self): account = Account('robot4test', 'robot4test') POJ = Controller('POJ') self.assertIsNotNone(POJ) POJ.get_result(account=account, pid='1379') POJ.get_problem(account=account, pid='1379')
def test_homeurl(self): self.assertEqual( Controller('HDU').get_home_page_url(), 'http://acm.hdu.edu.cn/')
def test_get_problem(self): hdu = Controller('HDU') account = Account('robot4test', 'robot4test') self.assertIsNotNone(hdu) hdu.get_problem(account=account, pid='1001')
def supports(): return json.dumps({ 'supports': Controller.get_supports() })
def problem(remote_oj, remote_id): problem = Controller(remote_oj).get_problem(remote_id, account=Account('robot4test', 'robot4test')) if problem.status == Problem.Status.STATUS_CRAWLING_SUCCESS: return problem.html return problem.status.name
def language(remote_oj): return json.dumps({ 'languages': Controller(remote_oj).find_language(account=Account('robot4test', 'robot4test')) })
def test_get_problem(self): account = Account('robot4test', 'robot4test') Aizu = Controller('Aizu') self.assertIsNotNone(Aizu) # Aizu.get_result(account=account,pid='0001') Aizu.get_problem(account=account, pid='0001')
def test_get_problem(self): account = Account('robot4test', 'robot4test') FZU = Controller('FZU') self.assertIsNotNone(FZU) FZU.get_result(account=account, pid='1039') FZU.get_problem(account=account, pid='1039')
def test_get_problem(self): account = Account('robot4test', 'robot4test') WUST = Controller('WUST') self.assertIsNotNone(WUST) problem = WUST.get_problem(account=account, pid='1039') self.assertIsNotNone(problem)