forked from clarkdave/DSACancellationChecker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DSAChecker.py
191 lines (135 loc) · 5.82 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
#!/usr/bin/python
"""
DSA Checker
For finding cancellations quickly and easily!
"""
import urllib.request, urllib.parse, urllib.error, urllib.request, urllib.error, urllib.parse, http.cookiejar, time, sys, os
from datetime import timedelta
from datetime import datetime
from bs4 import BeautifulSoup
from DSACheckerClasses import Page
##################################################################
# #
# Update the following variables with your own personal details: #
# #
##################################################################
# Driving license number (Example: MORGA657054SM9IJ)
licenceNumber = '****************'
# Application reference number
# (This number was given when you booked the test. It can be found on your confirmation email.)
theoryNumber = '********'
# Email sending details
# The email addresses you wish to send notifications to
emailAddresses = ['[email protected]', '[email protected]']
emailSubject = "DSA Cancellations"
emailFrom = "[email protected]"
# Enter your gmail account details here so that the script can send emails
emailUsername = '[email protected]'
emailPassword = 'mypassword' # the password to your "[email protected]" account
# Change this (at your own risk) if you don't use gmail (e.g. to hotmail/yahoo/etc smtp servers
emailSMTPserver = 'smtp.gmail.com'
# Put in your current test date in the format "Thursday 4 July 2013 2:00pm"; you will be alerted if an earlier slot appears
myTestDateString = 'Wednesday 12 June 2013 2:00pm'
##################################################################
# #
# 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()
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
import os
import re
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
def performUpdate():
# 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...')
launcher = Page(launchPage, cookieJar)
launcher.connect()
launcher.fields['username'] = licenceNumber
launcher.fields['password'] = theoryNumber
# check to see if captcha
captcha = launcher.html.find('div', id='recaptcha-check')
if captcha:
print('Captcha was present, retry later')
# TODO: implement something to solve these or prompt you for them
return
print('')
time.sleep(pauseTime)
launcher.connect()
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()
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()
# 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:')
soonerDates = []
for dt in availableDates:
if isBeforeMyTest(dt):
print ('-----> [CANCELLATION] %s' % (dt.strftime('%A %d %b %Y at %H:%M'),))
soonerDates.append(dt)
else:
print ('-----> %s' % (dt.strftime('%A %d %b %Y at %H:%M'),))
if len(soonerDates):
sendEmail(soonerDates)
performUpdate()
print('')