2021-05-22 17:12:17 +08:00

96 lines
4.6 KiB
Python

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