添加空教室代码

This commit is contained in:
RainSun 2021-05-22 17:12:17 +08:00
parent 7b56cf30a1
commit 5b40de742f
2 changed files with 192 additions and 0 deletions

95
EmptyRoom/main.py Normal file
View File

@ -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()

97
EmptyRoom/param.py Normal file
View File

@ -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错误')