import requests
from datetime import date
from datetime import timedelta
import json
import getopt
import sys
import time
global headers
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0',
'Host': '',
}
#登录函数
def login(id, pwd):
l_params = {
'id': id,
'pwd': pwd,
'act': 'login',
}
#自己分析得到的接口,下同
login_url = ''
s = requests.session()
re = s.post(url=login_url, headers=headers, params=l_params)
#返回登录成功的实例
return s
#预约函数
def yd(s, start, end, s_id):
y_params = {
"dialogid": "",
#预定座位
"dev_id": s_id,
"room_id":"",
"type":"dev",
"prop":"",
"number":"",
"classkind":"",
"test_name":"",
"start": start,
"end": end,
"start_time": "800",
"end_time": "2200",
}
y_url = ''
re = s.get(url=y_url, headers=headers, params=y_params)
print(re.text)
res = json.loads(re.text)
msg = res['msg']
if msg == '操作成功!':
print('success')
return 1
elif msg == '2020-09-11只能提前[1]天预约':
return 0
else:
print('fail')
return 2
#脚本输入提示信息
def useage():
print(
'''
Usage:
-i 学号(必填)
-p 密码
-s 座位id
-b 开始时间,格式13:00,下同
-e 结束时间
''')
if __name__ == '__main__':
id = pwd = None
#不填默认抢这个这个时间段的这个座位
s_id = '100458282'
begin = '8:00'
end = '22:00'
#处理输入
try:
opts, args = getopt.getopt(sys.argv[1:], 'i:p:s:b:e:')
for name, value in opts:
if name == '-i':
id = value
if name == '-p':
pwd = value
if name == '-s':
s_id = value
if name == '-b':
begin = value
if name == '-e':
end = value
except getopt.GetoptError:
useage()
if not id:
useage()
sys.exit(3)
if not pwd:
pwd = id
#先获取后天时间
aftertomorrow = date.isoformat(date.today() + timedelta(days=2))
while True:
hour = int(time.strftime('%H',time.localtime(time.time())))
m = int(time.strftime('%M', time.localtime(time.time())))
#如果现在的明天等与之前的后天,即到了12点,开始抢座
if date.isoformat(date.today() + timedelta(days=1)) == aftertomorrow:
s = login(id, pwd)
start = aftertomorrow + ' ' + begin
endtime = aftertomorrow + ' ' + end
result = yd(s, start, endtime, s_id)
if result == 1:
#预约成功
aftertomorrow = date.isoformat(date.today() + timedelta(days=2))
sleep_time = (23 - hour) * 3600 + (59 - m) * 60 + 35
print('程序休眠{}s'.format(sleep_time))
print(aftertomorrow)
time.sleep(sleep_time)
elif result == 2:
#被预约,抢下一个id的座位
s_id = str(int(s_id) - 1)
continue
else:
continue
else:
sleep_time = (23 - hour) * 3600 + (59 - m) * 60
time.sleep(sleep_time)