-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathSeat.py
210 lines (197 loc) · 6.83 KB
/
Seat.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# coding=utf-8
# Created by Deserts
# email: i@panjunwen.com
# site: https://panjunwen.com/
# date: 2016-08-19
import requests
import time
from bs4 import BeautifulSoup
import re
import random
import datetime
from captcha import captcha
class SeatReservation(object):
""" Seat class:
@ param url: Leosys' URL, e.g.http://202.206.242.87/, notice the "/"
!NOTICE: You'd better check the seat id (object.seat!=0)
when an object is created,
because if the param seat is invalid,
the variable seat of the object will be seted to 0.
"""
def __init__(self, url):
self.url = url
self.session = requests.session()
self.reserveStatus = False
self.date = self.getDate()
def login(self, username=None, password=None):
'''
return a status code.
0: login successfully
1: wrong username
2: wrong password
-1: network is unreachable
!NOTICE:
username and password MUST be provided the first time
'''
if username is not None:
self.userInfo = {
"username": username,
"password": password,
"captcha": ""
}
captchaUrl = self.url + "simpleCaptcha/captcha"
loginUrl = self.url + "auth/signIn"
while True:
try:
captchaImg = self.session.get(captchaUrl)
self.userInfo["captcha"] = captcha(captchaImg.content)
res = self.session.post(url=loginUrl, data=self.userInfo)
except requests.exceptions.ConnectionError, e:
print e
return -1
if self.loginStatusCheck(res):
return 0
msg = re.findall('showmsg."(.*)",', res.text)
if msg:
msg = msg[0]
print msg
if msg == u"登录失败: 用户不存在":
return 1
elif msg == u"登录失败: 密码不正确":
return 2
return -1
def loginStatusCheck(self, response=None):
'''
check if login successfully
'''
if response is None:
response = self.session.get(self.url)
return response.text[1] != "!"
def reserve(self, seat, start, end):
'''
reserve a seat.
!NOTICE:
@ param seat should be a string generated by getSeatID
@ param start, end: start and end time of minutes.
e.g. 8:00-13:00, start = 480, end = 780
return a status code.
0: reserve successfully
1: reached the limit, can't reserve
2: the specific seat has been reserved by another user
3: the system has not been opened up.
4: the account is limitted
-1: unkown error
'''
seatInfo = {
"date": self.date,
"seat": seat,
"start": start,
"end": end,
}
# post data and parse the response.
try:
res = self.session.post(url=(self.url + "selfRes"), data=seatInfo)
soup = BeautifulSoup(res.text, "lxml")
info = soup.dd.text
except:
info = None
print "post data: ", seatInfo
# deal with the response
infoSlice = info[6:13]
if info[:3] == u"凭证号" or infoSlice == u"已有1个有效预":
self.reserveStatus = True
return 0
elif infoSlice == u"网上预约请求过":
return 1
elif infoSlice == u"预约失败,请尽":
return 2
elif infoSlice == u"系统可预约时间":
return 3
elif infoSlice == u"对不起, 您的":
return 4
else:
return -1
def getSeatID(self, room, seat):
'''
return a seat id(string) which is used in the system.
if it returns 0, please make sure the room and seat
number is right.
@ param room: int
@ param seat: int
'''
dt = self.date
url = self.url + "mapBook/getSeatsByRoom?room=%d&date=%s" % (room, dt)
res = self.session.get(url)
soup = BeautifulSoup(res.text, "lxml")
li_tags = soup.find_all("li")
li_tags = [tag for tag in li_tags if tag.a is not None]
for tag in li_tags:
if int(tag.a.contents[0]) == seat:
return tag.get("id")[5:]
return 0
def getDate(self):
'''
return a string of date of the next day,
e.g. return "2016-08-20" when run this method on Aug 19.
'''
tm = time.localtime()
date = str(tm[0]) + "-" + str(tm[1]) + "-" + str(tm[2] + 1)
return date
def myReservation(self):
'''
return a text about my reservation
'''
url = self.url + "history?type=SEAT"
res = self.session.get(url)
soup = BeautifulSoup(res.text, "lxml")
return soup.dt.text + "\n" + soup.dd.text.strip()
def getSeatList(self, room):
'''
return a list of all available seats.
must provide a room id.
'''
rm = str(room)
dt = self.date
url = self.url + "mapBook/getSeatsByRoom?room=%s&date=%s" % (rm, dt)
res = self.session.get(url)
seatList = re.findall('>(\d+)</code', res.text)
if seatList:
return seatList
else:
return None
def randomReserve(self, room, start, end):
'''
reserve a seat randomly.
'''
seats = self.getSeatList(room)
if seats is not None:
random.shuffle(seats)
for seat in random:
seat = self.getSeatID()
status = self.reserve(seat, start, end)
if status < 2:
return status
return None
def autoReserve(self, schedtime, room, seat, start, end, randRsv=False):
'''
reserve the specific seat after timeDelta seconds.
@ param schedtime is a time, whose type is "datetime".
If the param randRsv is True, it will randomly
reserve a seat in the provided room.
'''
now = datetime.datetime.now()
if now > schedtime:
return None
timeDelta = (schedtime - now).seconds
time.sleep(timeDelta)
# sleep timeDelta seconds then reserve
# check account status
if self.loginStatusCheck() is False:
self.login()
while True:
status = self.reserve(seat, start, end)
if status == 2 and randRsv:
return self.randomReserve(room, start, end)
if status != 3:
break
return status