Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quick script to query the MA RMVs for their wait times every 60 seconds. I ended up using this to find the optimum time to go visit the RMV since I didn't want to wait long...
# -----------------------------------------------------------------------------
# Modifiable parameters
# -----------------------------------------------------------------------------
# Where to save the results to
save_file = "times.txt"
# The branch locations you care about
locations = ["Boston", "Watertown", "Natick", "Roslindale"]
# -----------------------------------------------------------------------------
# Some constants that probably don't need to be changed
# -----------------------------------------------------------------------------
# The hours that the RMVs are open (9-6) (usually 9-5, but some days are 10-6)
OPENING_HOURS = [9, 18]
# The URL to get the wait times
QUERY_URL = "https://www.massrmv.com/DesktopModules/BranchMapDNN/GetWaitTimes.aspx"
# The number of seconds to wait between successive queries
QUERY_INTERVAL = 60
# -----------------------------------------------------------------------------
# Don't change anything below here... unless you want to change the behavior
# -----------------------------------------------------------------------------
from datetime import date
from datetime import datetime
from datetime import time as dtime
import json
import re
import requests
from requests.exceptions import ConnectionError
import time
def parse_time(x):
"""Parse the time returned by the RMV call
:param x: The time string as returned by the web call.
E.g. "1 hour, 2 minutes, 54 seconds"
:return: A string of the form "HH:MM:SS"
"""
if x == u"No wait time":
return u"00:00:00"
if x == u"Closed":
return None
t = []
for part in ["hour", "minute", "second"]:
m = re.search(u"(\d+) {}".format(part), x)
if m:
t.append(u"{num:02d}".format(num=int(m.group(1))))
else:
t.append(u"00")
return u":".join(t)
# Convert the hours to actual datetimes
for i in range(len(OPENING_HOURS)):
OPENING_HOURS[i] = datetime.combine(date.today(), dtime(OPENING_HOURS[i]))
# Check to make sure that the RMV is actually open
if datetime.now() < OPENING_HOURS[0]:
print "RMVs aren't open yet."
if datetime.now() > OPENING_HOURS[1]:
print "RMVs are closed for the day."
# Keep querying for data while the RMV is open
while datetime.now() > OPENING_HOURS[0] and datetime.now() < OPENING_HOURS[1]:
try:
times = {}
for location in locations:
rv = requests.get("{}?Town={}".format(QUERY_URL, location))
cur_times = [parse_time(x) for x in rv.text.split(u"|")[:2]]
times[location] = {
'licensing': cur_times[0],
'registration': cur_times[1]
}
with open(save_file, "a") as f:
f.write(u"{}\t{}\n".format(datetime.now(), json.dumps(times)))
time.sleep(QUERY_INTERVAL)
except ConnectionError:
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment