-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathmain.py
117 lines (102 loc) · 5.43 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import json
import time
import argparse
import os
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
from utils import reserve, get_user_credentials
get_current_time = lambda action: time.strftime("%H:%M:%S", time.localtime(time.time() + 8*3600)) if action else time.strftime("%H:%M:%S", time.localtime(time.time()))
get_current_dayofweek = lambda action: time.strftime("%A", time.localtime(time.time() + 8*3600)) if action else time.strftime("%A", time.localtime(time.time()))
SLEEPTIME = 0.2 # 每次抢座的间隔
ENDTIME = "07:01:00" # 根据学校的预约座位时间+1min即可
ENABLE_SLIDER = False # 是否有滑块验证
MAX_ATTEMPT = 5 # 最大尝试次数
RESERVE_NEXT_DAY = False # 预约明天而不是今天的
def login_and_reserve(users, usernames, passwords, action, success_list=None):
logging.info(f"Global settings: \nSLEEPTIME: {SLEEPTIME}\nENDTIME: {ENDTIME}\nENABLE_SLIDER: {ENABLE_SLIDER}\nRESERVE_NEXT_DAY: {RESERVE_NEXT_DAY}")
if action and len(usernames.split(",")) != len(users):
raise Exception("user number should match the number of config")
if success_list is None:
success_list = [False] * len(users)
current_dayofweek = get_current_dayofweek(action)
for index, user in enumerate(users):
username, password, times, roomid, seatid, daysofweek = user.values()
if action:
username, password = usernames.split(',')[index], passwords.split(',')[index]
if(current_dayofweek not in daysofweek):
logging.info("Today not set to reserve")
continue
if not success_list[index]:
logging.info(f"----------- {username} -- {times} -- {seatid} try -----------")
s = reserve(sleep_time=SLEEPTIME, max_attempt=MAX_ATTEMPT, enable_slider=ENABLE_SLIDER, reserve_next_day=RESERVE_NEXT_DAY)
s.get_login_status()
s.login(username, password)
s.requests.headers.update({'Host': 'office.chaoxing.com'})
suc = s.submit(times, roomid, seatid, action)
success_list[index] = suc
return success_list
def main(users, action=False):
current_time = get_current_time(action)
logging.info(f"start time {current_time}, action {'on' if action else 'off'}")
attempt_times = 0
usernames, passwords = None, None
if action:
usernames, passwords = get_user_credentials(action)
success_list = None
current_dayofweek = get_current_dayofweek(action)
today_reservation_num = sum(1 for d in users if current_dayofweek in d.get('daysofweek'))
while current_time < ENDTIME:
attempt_times += 1
# try:
success_list = login_and_reserve(users, usernames, passwords, action, success_list)
# except Exception as e:
# print(f"An error occurred: {e}")
print(f"attempt time {attempt_times}, time now {current_time}, success list {success_list}")
current_time = get_current_time(action)
if sum(success_list) == today_reservation_num:
print(f"reserved successfully!")
return
def debug(users, action=False):
logging.info(f"Global settings: \nSLEEPTIME: {SLEEPTIME}\nENDTIME: {ENDTIME}\nENABLE_SLIDER: {ENABLE_SLIDER}\nRESERVE_NEXT_DAY: {RESERVE_NEXT_DAY}")
suc = False
logging.info(f" Debug Mode start! , action {'on' if action else 'off'}")
if action:
usernames, passwords = get_user_credentials(action)
current_dayofweek = get_current_dayofweek(action)
for index, user in enumerate(users):
username, password, times, roomid, seatid, daysofweek = user.values()
if type(seatid) == str:
seatid = [seatid]
if action:
username ,password = usernames.split(',')[index], passwords.split(',')[index]
if(current_dayofweek not in daysofweek):
logging.info("Today not set to reserve")
continue
logging.info(f"----------- {username} -- {times} -- {seatid} try -----------")
s = reserve(sleep_time=SLEEPTIME, max_attempt=MAX_ATTEMPT, enable_slider=ENABLE_SLIDER)
s.get_login_status()
s.login(username, password)
s.requests.headers.update({'Host': 'office.chaoxing.com'})
suc = s.submit(times, roomid, seatid, action)
if suc:
return
def get_roomid(args1, args2):
username = input("请输入用户名:")
password = input("请输入密码:")
s = reserve(sleep_time=SLEEPTIME, max_attempt=MAX_ATTEMPT, enable_slider=ENABLE_SLIDER, reserve_next_day=RESERVE_NEXT_DAY)
s.get_login_status()
s.login(username=username, password=password)
s.requests.headers.update({'Host': 'office.chaoxing.com'})
encode = input("请输入deptldEnc:")
s.roomid(encode)
if __name__ == "__main__":
config_path = os.path.join(os.path.dirname(__file__), 'config.json')
parser = argparse.ArgumentParser(prog='Chao Xing seat auto reserve')
parser.add_argument('-u','--user', default=config_path, help='user config file')
parser.add_argument('-m','--method', default="reserve" ,choices=["reserve", "debug", "room"], help='for debug')
parser.add_argument('-a','--action', action="store_true",help='use --action to enable in github action')
args = parser.parse_args()
func_dict = {"reserve": main, "debug":debug, "room": get_roomid}
with open(args.user, "r+") as data:
usersdata = json.load(data)["reserve"]
func_dict[args.method](usersdata, args.action)