def retcourses(self): logger.info("Formatting courses info...") table2 = self.list_raw1[1] all_in = [[], [], [], [], [], [], []] prtable2 = table2.split("<td class='Vtitle'>") prtable2.pop(0) li = ["一", "二", "三", "四", "五", "六", "日"] result = [] i = 0 for each in prtable2: newl = each.split("</td><td>") for eachone in newl: eachone = eachone.replace("<div>", " ").replace( "</div>", " ").replace("\n", '').replace("<td>", "").replace( "</tr>", "").replace("<tr>", "").replace("</td>", "").replace( " ", '').replace(" ", '') if eachone not in li: all_in[i].append(eachone) i += 1 i_w = 1 for weeksrc in all_in: for eachname in weeksrc: dic = { 'box1': eachname, 'diw': i_w, #xingqi "point": '', "teacher": '' } result.append(dic) i_w += 1 return result
def returnName(self): logger.info("combining nameinfo...") that = eval(self.Inflist['d'])[0] namecollege = that["xyzwmc"] + (that["zybjmc"]) name = that["xm"] mow = that["xbmc"] xq = that["xqmc"] return namecollege, name, mow, xq
def checkinfo(self): with open('./course.txt', "r", encoding="utf-8") as f: logger.info("Scanning Data") a = f.readline() f.close() if "处理请求时出错" in a: logger.error( "Data type is not correct!Student name or password error!") else: logger.info("There is nothing wrong with downloaded data!")
def retteacher(self): logger.info("Parsing courses info...") table1 = self.list_raw1[0] ls2 = re.findall(r'<tr>(.*?)</tr>', table1) ls2.pop(0) dic_point = [] for each in ls2: result = re.findall(r"<td>(.*?)</td>", each) title = result[0] teacher = result[1] point = result[2] diction = {"title": title, "teacher": teacher, "point": point} dic_point.append(diction) return dic_point
def __init__(self): self.stat = 0 cf = configparser.ConfigParser() cf.read("./config.ini") try: url1 = "https://restapi.amap.com/v3/weather/weatherInfo?parameters" param1 = { "key": cf.get("weather_config", "key"), "city": cf.get("weather_config", "city"), "extensions": "base" } logger.info("sending request to amap...(weather_today)") js1 = requests.get(url1, params=param1).json() self.area = js1["lives"][0]["province"] + js1["lives"][0]["city"] self.nowWeather = js1["lives"][0]["weather"] self.temperature = js1["lives"][0]["temperature"] self.winddirection = js1["lives"][0]["winddirection"] self.windpower = js1["lives"][0]["windpower"] self.wet = js1["lives"][0]["humidity"] self.reptime1 = js1["lives"][0]["reporttime"] #return "当前地区为:{} 天气为{} 温度为:{}℃ {}风 {}级 湿度为:{}% 上一次更新时间为:{}".format(area,nowWeather,temperature,winddirection,windpower,wet,reptime) param = { "key": cf.get("weather_config", "key"), "city": cf.get("weather_config", "city"), "extensions": "all" } logger.info("sending request to amap...(weather_future)") url = "https://restapi.amap.com/v3/weather/weatherInfo?parameters" js = requests.get(url, params=param).json() self.reptime = js['forecasts'][0]["reporttime"] d1 = js["forecasts"][0]['casts'][1] d2 = js["forecasts"][0]['casts'][2] d3 = js["forecasts"][0]['casts'][3] self.d1_dayweather = d1["dayweather"] self.d1_daytemp = d1["daytemp"] self.d1_nightweather = d1["nightweather"] self.d1_nighttemp = d1["nighttemp"] self.d2_dayweather = d2["dayweather"] self.d2_daytemp = d2["daytemp"] self.d2_nightweather = d2["nightweather"] self.d2_nighttemp = d2["nighttemp"] self.d3_dayweather = d3["dayweather"] self.d3_daytemp = d3["daytemp"] self.d3_nightweather = d3["nightweather"] self.d3_nighttemp = d3["nighttemp"] except Exception: self.stat = 1 logger.error("weather is not configured correctly!")
def settingsCheckerFORCOU(): cf = configparser.ConfigParser() cf.read("./config.ini") flag = 0 if cf.get("HQU-config", "studentNum") == "": logger.error("studentNum is not configured!") flag = 1 if not cf.get("HQU-config", "studentNum").isdigit(): logger.warning("StudentNum should be digit!") if cf.get("debug", "debug") not in ["on", "off"]: logger.error("Please check debug status!") flag = 1 if cf.get("HQU-config", "studentPassword") == "": logger.error("Password is not set!") if flag == 1: sys.exit(0) else: logger.info("all checked!") print("All checked-No mistake\n---------------------------")
def returnall(ob): logger.info("combining teacher&courses...") array_teacher = ob.retteacher() #tg array_point = ob.retcourses() flag = 0 for every in array_teacher: find_title = every["title"] for everyone in array_point: if find_title in everyone["box1"]: everyone['point'] = every["point"] everyone["teacher"] = every["teacher"] elif (find_title not in everyone["box1"]) and (everyone["box1"] != " "): if flag == 0: logger.warning("Course" + find_title + "has no match") flag = 1 else: pass return array_point
def settingsChecker(): cf = configparser.ConfigParser() cf.read("./config.ini") flag = 0 if cf.get("HQU-config", "studentNum") == "": logger.error("studentNum is not configured!") flag = 1 if not cf.get("HQU-config", "studentNum").isdigit(): logger.warning("StudentNum should be digit!") if cf.get("debug", "debug") not in ["on", "off"]: logger.error("Please check debug status!") flag = 1 if cf.get("HQU-config", "studentPassword") == "": logger.error("Password is not set!") flag = 1 if cf.get("weather_config", "key") == "": logger.warning("weatherkey is not set!") if cf.get("weather_config", "city") == "": logger.warning("city_is_not_set!") if flag == 1: sys.exit(0) else: logger.info("all checked!")
def returntest(self): logger.info("sending req to examarrange...") dataget_test = self.sessions.post( url= "http://10.30.11.12/WEB/Student/Mark/ExamArrange.aspx/GetExamArrange", headers=self.headers, data=json.dumps(self.pay)) j = dataget_test.json() logger.info("Attempting to overwrite/new test.txt...") with open("./test.txt", "w", encoding="utf-8") as f: f.write(str(j)) f.close() logger.info("w test.txt,done")
def returncirc(self): dataget_course = self.sessions.post( url= "http://10.30.11.12/WEB/Student/Course/StudentSchedule.aspx/GetCourseTable", headers=self.headers, data=json.dumps(self.pay)) logger.info("course info get via post!") j = dataget_course.json() logger.info("Attempting to overwrite/new course.txt...") with open("./course.txt", "w", encoding="utf-8") as f: f.write(str(j)) f.close() logger.info("w courses.txt, done")
def returnyourinfo(self): logger.info("sending req to grade...") dataget_test = self.sessions.post( url= "http://10.30.11.12/WEB/Student/InfoManage/BasicInfo.aspx/GetGrades", headers=self.headers, data=json.dumps(self.pay)) logger.info("sending req to stuinf...") dataget_name = self.sessions.post( url= "http://10.30.11.12/WEB/Student/InfoManage/BasicInfo.aspx/GetStudentInfo", headers=self.headers, data=json.dumps(self.pay)) j = dataget_test.json() k = dataget_name.json() logger.info("Attempting to overwrite/new perinfo.txt...") with open("./perinfo.txt", "w", encoding="utf-8") as f: f.write(str(j) + "\n") f.write(str(k)) f.close() logger.info("w perinfo.txt,done")
def extra(): cf = configparser.ConfigParser() cf.read("./config.ini") if cf.get("others", "ExperimentialFunction") == 'on': sys = platform.system() # print(sys) detail = str(platform.uname()) #print(detail) if sys == 'Windows': func_win() logger.info("windows" + '\n' + detail) if sys == 'Linux': func_linux() logger.info("linux" + '\n' + detail) if cf.get("optional", "raspberry") == "on": logger.info("raspberry") func_rasp()
###################################################################################################################################### #Below is the example code of HCD. # #ATTENTION:ALL OF YOUR PERSONAL DATA WILL BE STORAGED IN ./COURSES.TXT,./TEST.TXT,./PERINFO.TXT # # ###################################################################################################################################### import preMain import weather import sys from Info import logger import Checker import os import ExpFunc if not os.path.exists('./update.zip'): Checker.checkUpdate() Checker.settingsChecker() logger.info("checking config......done") ExpFunc.extra() def weatheroop(): print( "---------------------------------------天气情况---------------------------------------" ) wea = weather.weather() print(wea.future(0, 1, 2, 3)) print( "-------------------------------------------------------------------------------------" ) def perinforet():
def __init__(self): Checker.settingsCheckerFORCOU() logger.info("checking config......done") cf = configparser.ConfigParser() cf.read("./config.ini") print("please standby while getting info...") if cf.get("spider_config", "User-Agent") == '': logger.info("Using default UA") headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36" } else: headers = {"User-Agent": cf.get("spider_config", "User-Agent")} logger.info("User-Agent in config.ini detected:{}".format( cf.get("spider_config", "User-Agent"))) try: logger.info("Request: http://10.30.11.12/WEB/Login.aspx sending!") response = requests.get(headers=headers, url="http://10.30.11.12/WEB/Login.aspx", timeout=5) cookie = requests.utils.dict_from_cookiejar(response.cookies) sessionnum = cookie["ASP.NET_SessionId"] logger.info("SessionId is set as:{}".format(sessionnum)) except (requests.exceptions.ConnectTimeout): logger.critical( "Fatal Error:Unable to connect to HQU,check whether you have opened vpn_hqu" ) sys.exit(0) except (requests.exceptions.ConnectionError): logger.critical( "Fatal Error:Internet Connection is down!Please restart the program after Internet is configured correctly!" ) sys.exit(0) if cf.get("spider_config", "ASP.NET_SessionId") == '': rspid = sessionnum else: logger.info("ASP.NET_SessionId in config.ini detected") rspid = cf.get("spider_config", "ASP.NET_SessionId") self.headers = { 'Accept': 'application/json, text/javascript, */*; q=0.01', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Connection': 'keep-alive', 'Content-Length': '2', 'Content-Type': 'application/json; charset=UTF-8', 'Cookie': 'ASP.NET_SessionId=' + rspid, 'Host': '10.30.11.12', 'Origin': 'http://10.30.11.12', 'Referer': 'http://10.30.11.12/WEB/Student/Index.aspx', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36', 'X-Requested-With': 'XMLHttpRequest' } pydata = { 'userId': cf.get("HQU-config", "studentNum"), 'userPwd': cf.get("HQU-config", "studentPassword") } self.pay = {} self.sessions = requests.session() codereturn = self.sessions.post( url="http://10.30.11.12/WEB/Login.aspx/Login", headers=self.headers, data=json.dumps(pydata)) logger.info( "login request sent to:http://10.30.11.12/WEB/Login.aspx/Login,successfully,return status code:{}" .format(codereturn.status_code))
def checkUpdate(): cf = configparser.ConfigParser() cf.read("./config.ini") currentVersion = float(cf.get("version", "version")) headers = {"User-Agent": "UpdateHCD"} try: latest = requests.get("https://update.mrowl.xyz/HCD/latest", headers=headers).text a = float(latest) if a > currentVersion: logger.warning( "Current Version isn't the newest one which should be {}". format(a)) if 'y' == input("Press y to update or any key else to ignore:"): tar = requests.get( "https://update.mrowl.xyz/HCD/{}/md5".format(a), headers=headers).text with closing( requests.get( "https://update.mrowl.xyz/HCD/{}/{}.zip".format( a, a), headers=headers, stream=True)) as response: chunk_size = 1024 # 单次请求最大值 content_size = int( response.headers['content-length']) # 内容体总大小 progress = ProgressBar('update.zip', total=content_size, unit="KB", chunk_size=chunk_size, run_status="Update file downloading", fin_status="Done") with open('./update.zip', "wb") as file: for data in response.iter_content(chunk_size=chunk_size): file.write(data) progress.refresh(count=len(data)) with open('update.zip', 'rb') as f: s = hashlib.md5.new(f.read()).hexdigest() f.close() if 'y' == input( "Press y to update now or any key else to ignore:"): print("checking md5") if s != tar: logger.error("md5 does not match!") logger.error("update aborted") else: logger.info("done!") print("ALLOWED TO INSTALL-md5match") print("#MD5 MATCH#") logger.info("trying to backup") if not os.path.exists('./bak'): os.mkdir('./bak') shutil.copy('./config.ini', './bak/config_bak') shutil.copy('./log', './bak/log_bak') try: logger.info("execute try block(open/extract)") fz = zipfile.ZipFile("./update.zip", 'r') for root, dirs, files in os.walk('./'): for name in files: if (name.endswith(".py")): os.remove(os.path.join(root, name)) logger.info("rm exist,done") for file in fz.namelist(): fz.extract(file, '.') logger.info("extract done h h") fz.close() except Exception: logger.critical( "There are some errors occured during installation.However,all of your config files are moved to ./bak.\nYou may choose to unzip update.zip mannally to finish this update.\nsorry for this!\nIt'll be very nice of you to give us the log." ) sys.exit(-1) print("Done!Please restart") logger.info("updated!") os.remove("./update.zip") shutil.rmtree('./bak') sys.exit(0) else: logger.info("denied update but downloaded") print( "You may choose to unzip ./update.zip later to finish the update process.\nWARNING:PROGRAM WILL NEVER EXECUTE UPDATE MODULE IF ./UPDATE.ZIP EXISTS!" ) else: logger.info("refused update") else: logger.info("newest ver!") except Exception as e: print(repr(e)) logger.warning("Internet Error!The update server may be down!")
def __init__(self, test): #test1web errlist = [] vi = ["./course.txt", "./test.txt", "./perinfo.txt"] try: with open("./course.txt", "r", encoding="utf-8") as f: f.close() except FileNotFoundError: errlist.append(0) try: with open("./test.txt", "r", encoding="utf-8") as g: g.close() except FileNotFoundError: errlist.append(1) try: with open("./perinfo.txt", "r", encoding="utf-8") as g: g.close() except FileNotFoundError: errlist.append(2) if errlist != []: for tar in errlist: logger.critical("No file " + vi[tar] + " found!") sys.exit(0) if str(test) == "0": #print("------------------------缓存模式启用------------------------") logger.info("Temp mode enabled!") logger.info("attempting to read course.txt") with open("course.txt", "r", encoding="utf-8") as f: cList = f.read() f.close() coursesList = eval(cList) logger.info("attempting to read test.txt") with open("./test.txt", "r", encoding="utf-8") as g: tList = g.read() g.close() testsList = eval(tList) logger.info("attempting to read perinfo.txt") with open("./perinfo.txt", "r", encoding="utf-8") as g: poiList = g.readline() Inflist = g.readline() g.close() logger.info("parsing data...") #except FileNotFoundError: # logger.critical("NO FILE FOUND!") self.poiList = eval(poiList) self.Inflist = eval(Inflist) self.list_raw1 = coursesList['d'].split("</table>") self.list_tst1 = testsList["d"] #for i in self.list_tst1: # i.replace("\\","") eval(self.list_tst1) else: logger.info("Offline Mode") qu = '''TESTONLY--HIDDEN''' self.list_raw1 = qu.split("</table>") self.list_tst1 = 'TESTONLY--HIDDEN' self.Inflist = eval(str({'d': 'TESTONLY--HIDDEN'})) self.poiList = eval(str({'d': 'TESTONLY--HIDDEN'}))