From 5b40de742f491c17844d1afc2e39f394797f2cd8 Mon Sep 17 00:00:00 2001 From: RainSun Date: Sat, 22 May 2021 17:12:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=A9=BA=E6=95=99=E5=AE=A4?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EmptyRoom/main.py | 95 +++++++++++++++++++++++++++++++++++++++++++++ EmptyRoom/param.py | 97 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 192 insertions(+) create mode 100644 EmptyRoom/main.py create mode 100644 EmptyRoom/param.py diff --git a/EmptyRoom/main.py b/EmptyRoom/main.py new file mode 100644 index 0000000..6df992e --- /dev/null +++ b/EmptyRoom/main.py @@ -0,0 +1,95 @@ +import json +import requests +from urllib.parse import quote +import base64 +from bs4 import BeautifulSoup +import sys +from param import getParam, IDLIST, COURSELIST +import time + +class EmptyRoom(object): + def __init__(self, username, password, phone=''): + self.__phone = phone + self.__session = None + self.__ip = None + self.__getEmptyUrl = 'http://jwgls0-cust-edu-cn-8080-p.webvpn.cust.edu.cn:8118/api/ClientTeacher/QueryService/EmptyRoomQueryApi/GetEmptyRoomDataByPage?sf_request_type=ajax' + self.connection(username, password) + + # 链接教务 ----------------------------------------------------------------------------- + def connection(self, username, password): + try: + self.__session = requests.Session() + # 获取统一身份系统的网页 + r = self.__session.get( + url='http://mysso-cust-edu-cn-s.webvpn.cust.edu.cn:8118/cas/login?service=https%3A%2F%2Fwebvpn.cust.edu.cn%2Fauth%2Fcas_validate%3Fentry_id%3D1') + soup = BeautifulSoup(r.text, 'html.parser') + execution = soup.find_all(name='input')[6]['value'] + formdata = { + 'username': username, + 'password': password, + 'execution': execution, + '_eventId': 'submit', + 'geolocation': '' + } + r = self.__session.post( + url='http://mysso-cust-edu-cn-s.webvpn.cust.edu.cn:8118/cas/login?service=https%3A%2F%2Fwebvpn.cust.edu.cn%2Fauth%2Fcas_validate%3Fentry_id%3D1', data=formdata) + r = self.__session.get( + url='http://portal-cust-edu-cn-s.webvpn.cust.edu.cn:8118/custp/index') + soup = BeautifulSoup(r.text, 'html.parser') + try: + self.__ip = soup.findAll(name='a')[7]['href'][7:].split("-") + except: + return ('账号或者密码错误', 510) + r = self.__session.get(url='http://mysso-cust-edu-cn-s.webvpn.cust.edu.cn:8118/cas/login?service=http://' + + self.__ip[0] + '.' + self.__ip[1] + '.' + self.__ip[2] + '.' + self.__ip[3] + ':8080/welcome', allow_redirects=False) + ticket = r.headers['Location'][72:] + asp_net_sessionid_param = {'Ticket': ticket, 'Url': 'http://' + + self.__ip[0] + '.' + self.__ip[1] + '.' + self.__ip[2] + '.' + self.__ip[3] + ':8080/welcome'} + asp_net_sessionid_param = base64.b64encode( + quote(json.dumps(asp_net_sessionid_param)).encode('utf-8')).decode('utf-8') + asp_net_sessionid_param = {'param': asp_net_sessionid_param} + headers = {'Content-Type': 'application/json'} + r = self.__session.post(url='http://' + self.__ip[0] + '-' + self.__ip[1] + '-' + self.__ip[2] + '-' + self.__ip[3] + + '-8080-p.webvpn.cust.edu.cn:8118/api/LoginApi/LGSSOLocalLogin?sf_request_type=ajax', data=json.dumps(asp_net_sessionid_param), headers=headers) + data = json.loads(r.content.decode('utf-8')) + # 提示未建立教务信息 + if data['state'] != 0: + raise Exception('登录出错') + except Exception as e: + print(e) + return ('教务挂了', 512) + + # 获取空教室信息 + def getEmptyInfo(self, SJ, JSs, Addr): + param = '' + try: + param = getParam(SJ, JSs, Addr) + except Exception as e: + print(e) + return ('数据校验失败', 400) + Headers = {'Content-Type': 'application/json; charset=utf-8'} + formdata = {"param": param, "__permission": {"MenuID": "E93957BB-C05C-4D97-90F6-7839E1A77B62", "Operation": 0}, + "__log": {"MenuID": "E93957BB-C05C-4D97-90F6-7839E1A77B62", "Logtype": 6, "Context": "查询"}} + try: + resp = self.__session.post( + self.__getEmptyUrl, data=json.dumps(formdata), headers=Headers) + data = resp.json()['data']['PagingResult']['Rows'] + list = [] + for content in data: + list.append(content['JSMC']) + return list + except Exception as e: + print(e) + + def getEmptyList(self): + today = time.strftime("%Y-%m-%d", time.localtime()) + totalList = {} + for building in IDLIST.keys(): + totalList[building] = {} + for course in COURSELIST: + totalList[building][course] = self.getEmptyInfo(today, [course], building) + print(totalList) + +c = EmptyRoom('2017002372', '623910ert&') +# c.getEmptyInfo('2020-09-06', ['0102'], 'etb1') +c.getEmptyList() diff --git a/EmptyRoom/param.py b/EmptyRoom/param.py new file mode 100644 index 0000000..94ad5c7 --- /dev/null +++ b/EmptyRoom/param.py @@ -0,0 +1,97 @@ +from urllib.request import quote, unquote +import base64 +import json + +# 西区第一教学楼 +WESTID = { + # 西区第一教学楼 + 'teaching_building_1': 'f74dac26-c58a-4eae-bc46-ff2055b2de19', + # 西区第二教学楼 + 'teaching_building_2': '201a429b-df6d-489e-9a1d-de7c85f0081e', + # 西区图书馆 + 'library': '3adb80f2-7e27-4058-a60c-fbb32cb36587' +} +EASTID = { + # 东区第一教学楼 + 'teaching_building_1': 'd91cc53c-a9ad-4be3-becf-7f3ed62e8762', + # 东区第二教学楼 + 'teaching_building_2': 'e14b90bd-0c92-422e-b299-7009118104b9', + # 东区第三教学楼 + 'teaching_building_3': '3534f8ce-f10b-4058-a818-95a116d9bca4', + # 东区前楼 + 'front_building': '6accca4d-b092-4bdc-b2e0-0c1941782eec' +} +SOUTHID = { + # 南区研究生教学楼 + 'graduate_building': '20a207f7-65ef-4ae4-9286-2a2b5a73e1c9', + # 南区实训楼 + 'practical_training_building': 'cb5265e8-84a1-41ed-985b-3920449738aa' +} + +IDLIST = { + 'wtb1': WESTID['teaching_building_1'], + 'wtb2': WESTID['teaching_building_2'], + 'wl': WESTID['library'], + 'etb1': EASTID['teaching_building_1'], + 'etb2': EASTID['teaching_building_2'], + 'etb3': EASTID['teaching_building_3'], + 'efb': EASTID['front_building'], + 'sgb': SOUTHID['graduate_building'], + 'sptb': SOUTHID['practical_training_building'], +} + +COURSELIST = ['0102', '0304', '0506', '0708', '0910', '1112'] + +# JSON转base64 +def btoa(content): + return base64.b64encode(quote(content).encode()) + +# base64转JSON +def atob(content): + return unquote(base64.b64decode(content).decode()) + +def getParam(SJ, JSs, Addr): + try: + checkData(SJ, JSs, Addr) + except Exception as e: + print(e) + raise Exception(str(e)) + param = { + "EmptyRoomParam": { + "SJ": SJ, + "JCs": JSs + }, + "PagingParam": { + "IsPaging": 1, + "Offset": 0, + "Limit": 500, + "Conditions": { + "PropertyParams": [ + { + "Field": "BDJXLXXID", + "Value": IDLIST[Addr] + } + ] + } + } + } + return str(btoa(json.dumps(param)))[2:-1] + +# 校验数据 +def checkData(sj, jss, addr): + # 校验sj + sj_split = sj.split('-') + if len(sj_split) != 3: + raise Exception('SJ错误') + for content in sj_split: + if len(content) == 0: + raise Exception('SJ错误') + # 校验jss + if not isinstance(jss,list): + raise Exception('JSs错误') + for content in jss: + if content not in COURSELIST: + raise Exception('JSs错误') + # 校验addr + if addr not in IDLIST: + raise Exception('Addr错误') \ No newline at end of file