自动化AC器(带界面版)

第一次写这种东西,写起来尤其的生疏。要不是py的课设有点赶不及,也不会来做这个···
总体而言并不难,但之前一直在写算法题,导致对应用这方面的东西几乎是位0的了解。
界面用的是tkinter,自己写的也是丑的出奇。界面逻辑也很混乱,但总归是写出来了
(我估计我一周后绝对看不懂我写的是什么了····
代码不长,逻辑也很简单,将就着看吧

from tkinter import *
from urllib.request import urlopen
from bs4 import BeautifulSoup
import HTMLParser,requests,re,time,os,sys
html_parser = HTMLParser.HTMLParser()
host_url = 'http://acm.hdu.edu.cn'
head = { 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36' }
s = requests.session()

def Get_code(url):
    try:
        html =urlopen(url)
    except ValueError as e:
        # print("ValueError")
        return None
    bsobj = BeautifulSoup(html,"html.parser")
    codelist = bsobj.findAll("pre",{"class":"cpp"})
    if len(codelist)>0:
        text = codelist[0].get_text()
        rul = re.compile('freopen.*?\);')
        text = rul.sub('',text)
        #print(text)
        #text.replace(rul,'')
        return text

# 从CDSN爬取代码
def Csdn_Search(number):
    csdn_url = 'https://so.csdn.net/so/search/s.do?q='
    url = csdn_url+'HDU'+number
    html = urlopen(url)
    bsobj = BeautifulSoup(html,"html.parser")
    bsobj=bsobj.findAll('dl',class_="search-list J_search")
    linklist = list()
    for i in bsobj:
        x = i.find('a').get('href')
        linklist.append(x)
    code =list()
    for link in linklist:
        #print(link)
        res = Get_code(link)
        if res != None:
            code.append(res)
    return code

# 判断提交代码是否成功AC
def Is_AC(user,number):
    check_url = 'http://acm.hdu.edu.cn/status.php?first=&pid='+number+'&user='+user
    #print(check_url)
    html = urlopen(check_url)
    bsobj = BeautifulSoup(html,"html.parser")
    bsobj = bsobj.findAll('font',{'color':'red'})
    if len(bsobj) == 0:
        return 'WA'
    else:
        return 'AC'
# 判断提交代码时的语言
def select_language(code):
    if 'import java' in code:
        return '5'
    else:
        return '0'

# 向HDU提交评测代码
def submit(codelist,user,number):
    path = sys.path[0]
    filename = path+'\HDU'+number
    s.get(host_url,headers=head)
    sub_url = 'http://acm.hdu.edu.cn/submit.php?action=submit'
    for code in codelist:# 遍历所有爬到的代码
        language=select_language(code)
        datas = {'check':'0','problemid':number,'language':language,'usercode':code}
        r = s.post(sub_url,headers=head,data=datas)
        time.sleep(10)# 等待评测机判题
        res = Is_AC(user,number)
        print('submit successful')
        if res == 'AC':
            if language == '5':
                filename = filename +'.java'
            else:
                filename = filename +'.cpp'
            print(filename)
            fp = open(filename,'w')
            fp.write(code)
            return True
    return False

# 提交的子窗口
def sub(usr_name):
    AC.title('Submit')
    AC.geometry('350x300')
    # 背景画布
    can = Canvas(AC,height=450,width=400)
    Submit_File=PhotoImage(file=sys.path[0]+'\login.png') 
    can.create_image(0,0,anchor='nw',image=Submit_File)
    can.pack(side=TOP)
    id =StringVar()
    Label(AC,text="Problem id: ").place(x=30,y=93)
    global problem
    # 题号输入框
    problem = Entry(AC,textvariable = id,width=15)
    problem.place(x=130,y=95)
    
    # 信息更新窗口
    info = Text(AC,width=40,height=8)
    info.config(state=DISABLED)
    info.place(x=30,y=140)
    def get_id():
        p_id = problem.get()
        succ =True
        try:
            x=int(p_id)
        except ValueError as e:
            info.config(state=NORMAL)
            info.insert(END,'invail id\n')
            info.config(state=DISABLED)# 让text不能被编辑
            succ=False
        if succ:
            if x >= 6297 or x < 1000:# ID的合法性
                info.config(state=NORMAL)
                info.insert(END,'wrong id\n')
                info.config(state=DISABLED)
            else:
                info.config(state=NORMAL)
                info.insert(END,'Loading......\n')
                code=Csdn_Search(p_id)
                n = len(code) 
                if n==0:
                    info.insert(END,"'Sorry , Can't find the code\n")
                else:
                    info.insert(END,"Find %d codes\n"%n)
                    if submit(code,usr_name,p_id):
                        info.insert(END,"%s Successful AC!\n" %p_id)
                    else:
                        info.insert(END,"Sorry,All codes can't AC\n")
    
                info.config(state=DISABLED)
    s = Button(AC,text='submit!',command=get_id)
    s.place(x=270,y=90)
    AC.mainloop()#弹出新的窗口
def login(user,psw):
    
    post_url = 'http://acm.hdu.edu.cn/userloginex.php?action=login'
    test_url = 'http://acm.hdu.edu.cn/submit.php?pid=1000'
    s.get(host_url,headers=head)
    data = {'username':user,'userpass':psw,'login':'Sign In'}
    s.post(post_url,data=data,headers=head)
    res = s.post(test_url)
    res = res.content.decode('utf-8')
    if 'Source Code' in res: # 判断是否登录成功
        return True
    else:
        return False

def user_T_login():# 新窗口
    user = usr_name.get()
    psw = usr_psw.get()
    if login(user,psw):
        T_login.destroy()
        global AC
        AC=Tk()
        
        sub(user)
        
    else:
        sta.set('Wrong Password')

T_login = Tk()
T_login.title('LOGIN IN')
T_login.geometry('350x300')
# 背景图片
can = Canvas(T_login,height=450,width=400)
Image_File =PhotoImage(file=sys.path[0]+'\login.png')
can.create_image(0,0,anchor='nw',image=Image_File)
can.pack(side=TOP)

Label(T_login,text="User name: ").place(x=30,y=150)
Label(T_login,text="Password: ").place(x=40,y=190)
usr_name = StringVar()
usr_psw = StringVar()
en_n = Entry(T_login,textvariable=usr_name)# 名字输入框
en_n.place(x=140,y=150)
en_p= Entry(T_login,textvariable=usr_psw,show='*')# 密码输入框
en_p.place(x=140,y=190)

btn = Button(T_login,text="login",command=user_T_login)# 登录按钮
btn.place(x=150,y=250)

sta = StringVar()
Label(T_login,textvariable=sta,width=50,height=1).place(x=40,y=220)# 信息提示框
T_login.mainloop()

用到的图片在这里,需要自取

posted @ 2018-06-15 17:48  SCaryon  阅读(321)  评论(0编辑  收藏  举报