哈工大乐学网自动评分系统

帮你一师兄写的自动评分系统:

from urllib import request
from urllib import parse
from http import cookiejar
from html.parser import HTMLParser
import re
import os
import random

class NewsParser(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)
        self.comment = {}
        self.start = False
        self.option = False
        self.textarea = False
        self.commentGood = ["很好", "Very Good", "非常好"]
        
    def handle_starttag(self, tag, attrs):
        #print(tag)
        attrs = dict(attrs)
        if tag == "tr" and attrs.get("class", "no") == 'r1':
            self.start = True
        if tag == "select" and self.start:
            self.option = True
            self.selectName = attrs["name"]
        if self.option and tag == "option":
            if attrs.get("selected", "not") != "not":
                self.score = attrs['value']
                if self.score == "-1":
                    self.comment[self.selectName] = "0"
        if self.start and tag == "textarea":
            if self.score == "100" or self.score == "-1":
                self.commentName = attrs["name"]
                self.textarea = True
   
        if tag == "input":
            name = attrs.get("name", "no")
            if name == "id" or name == "mode" or name == "page" or name == "sesskey":
                self.comment[name] = attrs["value"]
     
    def handle_data(self, data):
        #print(data)
        if self.textarea:
            if self.score == "100":
                self.comment[self.commentName] = self.commentGood[random.randint(0, len(self.commentGood) - 1)]
            elif self.score == "-1":
                self.comment[self.commentName] = "没有提交作业"
            self.textarea = False
                
    def handle_endtag(self, tag):
        if self.start and tag == "table":
            self.start = False
        if self.option and tag == "select":
            self.option = False
            
    def getUserInfo(self):
        return self.comment
            
class Hit():
    def __init__(self, username, password):
        self.url = "https://cas.hit.edu.cn/login/index.php"
        self.username = username
        self.password = password
        self.cookie = cookiejar.LWPCookieJar()
        self.opener = request.build_opener(request.HTTPCookieProcessor(self.cookie))
        self.taskUrl = {}
        self.taskUrl["作业10-1"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=2030"
        self.taskUrl["实验1"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=2321"
        self.taskUrl["实验2-1"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=3075"
        self.taskUrl["实验2-2"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=3076"
        self.taskUrl["实验2-3"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=2341"
        self.taskUrl["实验3-1"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=3108"
        self.taskUrl["实验3-2"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=3109"
        self.taskUrl["实验3-3"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=3110"
        self.taskUrl["实验3-4"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=2484"
        self.taskUrl["实验4-1"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=2545"
        self.taskUrl["实验4-2"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=2531"
        self.taskUrl["实验4-3"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=3281"
        self.taskUrl["实验4-4"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=2546"
        #self.taskUrl["实验5-1"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=3321"
        self.taskUrl["实验5-2"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=2635"
        self.taskUrl["实验6-1"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=2679"

        #self.taskUrl["实验6-2"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=2682"
        self.taskUrl["实验6-3"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=2683"
        self.taskUrl["实验7-1"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=2689"
        self.taskUrl["实验7-2"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=2695"
        self.taskUrl["实验8-1"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=2715"
        self.taskUrl["实验8-2"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=2724"
        self.taskUrl["实验8-3"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=2907"
        self.taskUrl["实验9-1"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=2790"
        self.taskUrl["实验9-2"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=2791"
        self.taskUrl["实验9-3"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=2908"
        self.taskUrl["实验10-1"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=2872"
        self.taskUrl["实验10-2"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=2877"
        self.taskUrl["实验10-3"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=2878"
        self.taskUrl["实验10-4"] = "https://cms.hit.edu.cn/mod/assignment/submissions.php?id=2909"
        
    def autoLogin(self, task):
        url = "https://cas.hit.edu.cn/login?service=https%3A%2F%2Fcms.hit.edu.cn%2Flogin%2Findex.php%3FauthCAS%3DCAS"
        response = self.opener.open(request.Request("https://cms.hit.edu.cn/login/index.php?authCAS=CAS"))
        html = response.read().decode('utf-8')
        string = re.search('<input type="hidden" name="lt".+/>',html).group()
        lt=re.search('value=".*"',string).group().strip('value="')
        data = parse.urlencode({'username':self.username, 'password':self.password,'lt':lt,'rememberMe':'true','_eventId':'submit'})
        self.opener.open(request.Request(url, data.encode('utf-8')))

        while self.taskUrl.get(task, "no")  == "no":
            task = input("Not Find This Task, Input Task Again:")
        self.makeTask(task)
        self.makeTask(task)
  
        print ("Finish Task!")
        
    def makeTask(self, task):
        
        url = self.taskUrl[task] + "&group=1362"
        fd = self.opener.open(request.Request(url))
        myParser = NewsParser()
        myParser.feed(fd.read().decode('utf-8'))
        self.opener.open(request.Request(url,parse.urlencode(myParser.getUserInfo()).encode("utf-8")))
        
        
def main():
    task = input("Input Task:")
    hit = Hit(username,password)
    hit.autoLogin(task)

    
main()
posted @ 2012-05-24 14:53  icamel  阅读(1443)  评论(0编辑  收藏  举报