-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathDSAChecker.py
230 lines (171 loc) · 7.74 KB
/
DSAChecker.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/python3
"""
DSA Checker
For finding cancellations quickly and easily!
"""
from datetime import datetime
import http.cookiejar
import time
import random
from DSACheckerClasses import Page
##################################################################
# #
# Update the following variables with your own personal details #
# in info.py #
# #
##################################################################
from info import licenceNumber, theoryNumber, myTestDateString
# Email sending details
from info import emailAddresses, emailUsername, emailPassword
from find_cancellations_selenium import open_web
emailSubject = "DSA Cancellations"
emailFrom = "[email protected]"
# Change this (at your own risk) if you don't use gmail (e.g. to hotmail/yahoo/etc smtp servers
emailSMTPserver = 'smtp.gmail.com'
##################################################################
# #
# DO NOT MODIFY ANYTHING BELOW THIS LINE #
# #
##################################################################
myTestDate = datetime.strptime(myTestDateString, '%A %d %B %Y %I:%M%p')
# time to wait between each page request (set to a reasonable number
# to avoid hammering DSA's servers)
pauseTime = 5
cookieJar = http.cookiejar.CookieJar()
#control the number of appointments shown
max_shownum = 10 #
#choose the action when find an available datetime
#0: send an email
#1: open web directly
action_choosen = 1
def isBeforeMyTest(dt):
if dt <= myTestDate:
return True
else:
return False
def sendEmail(datetimeList):
# i should probably point out i pinched this from stackoverflow or something
SMTPserver = emailSMTPserver
sender = emailFrom
destination = emailAddresses
USERNAME = emailUsername
PASSWORD = emailPassword
# typical values for text_subtype are plain, html, xml
text_subtype = 'plain'
content = "Available DSA test slots at your selected test centre:\n\n"
for dt in datetimeList:
content += "* %s\n" % dt.strftime('%A %d %b %Y at %H:%M')
content += "\nChecked at [%s]\n\n" % time.strftime('%d-%m-%Y @ %H:%M')
subject = emailSubject
import sys
from smtplib import SMTP as SMTP # this invokes the secure SMTP protocol (port 465, uses SSL)
# from smtplib import SMTP # use this for standard SMTP protocol (port 25, no encryption)
from email.mime.text import MIMEText
try:
msg = MIMEText(content, text_subtype)
msg['Subject'] = subject
msg['From'] = sender # some SMTP servers will do this automatically, not all
conn = SMTP(SMTPserver, 587)
conn.set_debuglevel(False)
conn.ehlo()
conn.starttls() # Use TLS
conn.login(USERNAME, PASSWORD)
try:
conn.sendmail(sender, destination, msg.as_string())
finally:
conn.close()
except Exception as exc:
sys.exit("mail failed; %s" % str(exc)) # give a error message
soonerDates = []
baseWaitTime = 600
userAgents = [
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8',
'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
]
def performUpdate():
global baseWaitTime
global userAgents
global soonerDates
global max_shownum
global action_choosen
# this should point at the DSA login page
launchPage = 'https://driverpracticaltest.direct.gov.uk/login'
print('[%s]' % (time.strftime('%Y-%m-%d @ %H:%M'),))
print('---> Starting update...')
# use a random agent for each run through
agent = userAgents[random.randint(0, len(userAgents) - 1)]
print("---> Using agent " + agent)
launcher = Page(launchPage, cookieJar)
launcher.connect(agent)
launcher.fields['username'] = licenceNumber
launcher.fields['password'] = theoryNumber
# check to see if captcha
captcha = launcher.html.find('div', id='recaptcha-check')
if captcha:
# server is suspicious, back off a bit!
baseWaitTime *= 2
print('Captcha was present, increased baseline wait time to ' + str(baseWaitTime/60) + ' minutes')
# TODO: implement something to solve these or prompt you for them
return
print('')
time.sleep(pauseTime)
launcher.connect(agent)
if captcha:
print(launcher.html.find("Enter details below to access your booking"))
dateChangeURL = launcher.html.find(id="date-time-change").get('href')
# example URL: href="/manage?execution=e1s1&csrftoken=hIRXetGR5YAOdERH7aTLi14fHfOqnOgt&_eventId=editTestDateTime"
# i am probably screwing up the POST bit on the forms
dateChangeURL = 'https://driverpracticaltest.direct.gov.uk' + dateChangeURL
slotPickingPage = Page(dateChangeURL, cookieJar)
slotPickingPage.fields = launcher.fields
slotPickingPage.connect(agent)
e1s2URL = slotPickingPage.html.form.get('action')
e1s2URL = 'https://driverpracticaltest.direct.gov.uk' + e1s2URL
datePickerPage = Page(e1s2URL, cookieJar)
datePickerPage.fields['testChoice'] = 'ASAP'
datePickerPage.fields['drivingLicenceSubmit'] = 'Continue'
datePickerPage.fields['csrftoken'] = dateChangeURL.split('=')[3]
datePickerPage.connect(agent)
# earliest available date
availableDates = []
for slot in datePickerPage.html.find_all(class_='SlotPicker-slot'):
try:
availableDates.append(datetime.strptime(slot['data-datetime-label'].strip(), '%A %d %B %Y %I:%M%p'))
except Exception as ex:
print("".join(traceback.format_exception(etype=type(ex), value=ex, tb=ex.__traceback__)))
print ('---> Available slots:')
newSoonerDates = []
#control the number of appointments shown
shownum = 0
for dt in availableDates:
# only show / send new appointments
if isBeforeMyTest(dt) and (dt not in soonerDates):
print ('-----> [CANCELLATION] %s' % (dt.strftime('%A %d %b %Y at %H:%M'),))
soonerDates.append(dt)
newSoonerDates.append(dt)
else:
shownum += 1
#control the number of appointments shown
if shownum < max_shownum:
print ('-----> %s' % (dt.strftime('%A %d %b %Y at %H:%M'),))
if len(newSoonerDates):
if action_choosen == 1:
open_web()
elif action_choosen == 0:
print('---> Sending to ' + ', '.join(emailAddresses))
sendEmail(newSoonerDates)
if baseWaitTime > 300:
# decrease the baseline wait time as this was a success
baseWaitTime = int(baseWaitTime / 2)
while True:
print('***************************************')
performUpdate()
# wait for baseline + random time so its less robotic
sleepTime = baseWaitTime + random.randint(60, 300)
print('---> Waiting for ' + str(sleepTime / 60) + ' minutes...')
time.sleep(int(sleepTime))