Automatic Submitter for HUSTOJ

2022-09-19 14:13:24 浏览数 (1)

Automatic Submitter for HUSTOJ

GitHub: yzxoi/Automatic-Submitter-for-HUSTOJ

为 HUSTOJ 打造的自动提交机

特性

  • 自动化 - 使用 Selenium with Python 实现自动化交题
  • 同步化 - 支持自动爬取大号提交记录以提交至小号
  • 定制化 - 支持自定义选择题目提交

开始

Tips: 推荐使用 Python 3.10 版本构建运行。

安装 Python 及依赖库:

代码语言:javascript复制
$ choco install python
$ pip install selenium
$ pip install requests
$ pip install lxml

打开终端,运行:

代码语言:javascript复制
$ git clone https://github.com/yzxoi/Automatic-Submitter-for-HUSTOJ.git

修改 main.py 内的配置文件:

代码语言:javascript复制
$ vi Automatic-Submitter-for-HUSTOJ/main.py

运行 main.py:

代码语言:javascript复制
$ python main.py

配置

  1. 填写 HUSTOJ 网址 URL。
  2. 填写主账号提交者 MAIN_SUBMITTER。该账号应含有某一种语言所有正确提交记录。
  3. 填写提交语言 LANGUAGE。(对应代码表见附录)
  4. 填写子账号(bot 账号) USER_ID。
  5. 填写子账号(bot 账号) PASSWORD。
  6. 填写主账号 Cookie:替换 <cookie>
代码语言:javascript复制
URL = "http://syzoj.hustoj.com/"
MAIN_SUBMITTER = "std"
LANGUAGE = "6"
USER_ID = "spider"
PASSWORD = "spider123456"

headers = {
	"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36",
	"Cookie": "<cookie>"
}

cookie 查找方法:

登录主账号,打开 F12 开发者管理工具,打开控制台 Console,输入:

代码语言:javascript复制
document.cookie

其所返回的 PHPSESSID=qwertyuiop 即为 cookie。

注意当运行本程序时要确保主账号处于登录状态。

附录

语言

代码

C

0

C

1

Java

3

Python

6

PHP

7

C#

9

JavaScript

16

Go

17

SQL

18

Source Code

代码语言:javascript复制
import selenium
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium import webdriver
import requests, time, re
from lxml import etree

URL = "http://syzoj.hustoj.com/"
MAIN_SUBMITTER = "std"
LANGUAGE = "6"
USER_ID = "spider"
PASSWORD = "spider123456"

headers = {
	"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36",
	"Cookie": "<cookie>"
}


vis = {}
def main():
	url = URL   "status.php?problem_id=&user_id="   MAIN_SUBMITTER   "&language="   LANGUAGE   "&jresult=4"
	response = requests.get(url=url, headers=headers)
	text = response.content.decode("utf-8")
	html = etree.HTML(text)
	links = html.xpath('//*[@id="result-tab"]/tbody/tr/td[1]/b/text()')
	probs = html.xpath('//*[@id="result-tab"]/tbody/tr/td[4]/b/div/a/text()')

	print("搜索到的提交记录对应题目:",end='')
	print(probs)
	
	driver = webdriver.Chrome()
	url = URL   "loginpage.php"
	driver.get(url)
	driver.find_element(By.NAME,"user_id").send_keys(USER_ID)
	driver.find_element(By.NAME,"password").send_keys(PASSWORD)
	time.sleep(1)
	driver.find_element(By.NAME,"submit").click()

	for i in probs:
		vis[i] = 0

	cnt = 0
	for link in links:
		if vis[probs[cnt]] == 0:
			vis[probs[cnt]] = 1
			url = URL   "showsource.php?id="   link
			response = requests.get(url=url, headers=headers)
			text = response.content.decode("utf-8")
			html = etree.HTML(text)
			code = html.xpath('//pre/text()')[0]
			url = URL   "submitpage.php?id="   probs[cnt]
			driver.get(url)
			sel = driver.find_element(By.ID,"language")
			Select(sel).select_by_value(LANGUAGE)
			time.sleep(1)
			t= ""
			for j in range(0,len(code)): # 转义字符问题
				if ord(code[j])==10:
					t  = "\n"
				elif ord(code[j])==13:
					t ="\t"
				elif code[j]=="'":
					t ="\'"
				elif code[j]=="/" and code[j 1]=='*':
					break # 去掉注释
				else:
					t =code[j]
			stri = "editor.setValue('"   str(t)   "')"
			driver.execute_script(stri)
			time.sleep(1)
			driver.find_element(By.ID,"Submit").click()
			time.sleep(10)
		cnt = cnt   1
		print("cur progress: "   str(cnt)   "/"   str(len(links)))

if __name__ == '__main__':
	main()

0 人点赞