Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Inspired by https://gist.github.com/saikatbhadra/6629031
import mechanize
from bs4 import BeautifulSoup
from datetime import datetime
import random
import time
TGT_DATE = 'April 21, 2015' #i.e. 'September 30, 2014'
OFFICE_ID = '548' # officeid for RWC DMV. See HTML code for other office ids
TASK = 'DT'
form_url = 'https://www.dmv.ca.gov/wasapp/foa/findDriveTest.do'
def schedule(targetDate,name,officeId,telNumber,birthday,dlNumber,fromDate=None):
tels = telNumber.split('-')
births = birthday.split('-')
names = name.split(' ')
if not fromDate:
fromDate = datetime.today()
from_date = datetime.today().strftime("%B %d, %Y")
else:
from_date = datetime.strptime(fromDate,'%B %d, %Y')
tgt_date = datetime.strptime(targetDate,'%B %d, %Y')
# Browser
br = mechanize.Browser()
br.open(form_url)
br.select_form(name="ApptForm")
br['officeId']=[officeId]
br.find_control(name="requestedTask").value = ["DT"]
br['firstName'] = names[0]
br['lastName'] = names[1]
br['telArea'] = tels[0]
br['telPrefix'] = tels[1]
br['telSuffix'] = tels[2]
br['dlNumber'] = dlNumber
br['birthMonth'] = births[0]
br['birthDay'] = births[1]
br['birthYear'] = births[2]
result = br.submit()
result_html = br.response().read()
soup = BeautifulSoup(result_html)
results = soup.findAll('p',class_="alert")
logString = ""
for result in results:
if ('2015' in result.get_text()):
appt_text = result.get_text()
logString += "Earliest Appointment found is %s" %(appt_text)
appt_date = datetime.strptime(appt_text,'%A, %B %d, %Y at %I:%M %p')
if appt_date <= tgt_date and appt_date>=from_date:
logString+= "\nCongratulations! You've found a date earlier than your target. Scheduling the appointment..."
br.select_form(nr=4)
r = br.submit()
logString+= "\nConfirming the appointment..."
br.select_form(nr=4)
r = br.submit()
s = BeautifulSoup(br.response().read())
logString += "\nThe final step is to actually schedule it...Use it when necessary.\n"
br.select_form(nr=4)
r = br.submit()
s = BeautifulSoup(br.response().read())
print s
# logString += br #print out relevant info related to the appointment
print logString
with open("test.txt", "a") as myfile:
myfile.write(logString)
return True
else:
logString += "\nSorry there is no appointment available between date %s and %s"%(from_date,targetDate)
logString += "\n"
print logString
with open("test.txt", "a") as myfile:
myfile.write(logString)
return False
while not schedule(TGT_DATE,'FIRST LAST',OFFICE_ID,'111-222-3333','01-30-1980','DL00001','April 13, 2015'):
waitTime = random.randint(60,600)
time.sleep(waitTime)
@franklinadamau

This comment has been minimized.

Copy link

franklinadamau commented Feb 5, 2017

Could you plz update the script....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment