How an automated email saved me from getting parking tickets
TL;DR, I wrote an automated email using Firebase and a few other tools to remind me to never park in a restricted parking area.
My office is within walking distance of the Cubs stadium, Wrigley Field. During night games, most of the street parking is permitted after 5 pm. Which leave me in a predicament: I have to move my car before 5 pm on night games so I don’t get a ticket. Last year someone in the office racked up $300 in parking tickets.
The first thing I thought to do was to add all the night games in my calendar, but there are tons of night games and I was lazy, so manual wasn’t gonna work. I could have scripted it to get all the night games and populate my calendar but there was a lot of google calendar issues I ran into around authentication.
So, I decided to write a cron job to check against an API to see whether or not there was a night game, if there was, I would send an automated email in the morning reminding myself not to park there.
Here is what we’ll be using to build this automated email system.
Shut up and code already
You’ll need to install the Firebase CLI:
npm install -g firebase-tools
Then create a new directory and initialize our firebase project
Go through the project setup and choose functions for the time being
Next, we’ll need to install a bunch of packages to make use of all this goodness.
npm install axios moment moment-timezone nodemailer --save
At the top of your
index.js file, you’ll need to load your dependencies
Because I needed the function to run every day, I created a pubsub function to go off every day at 6:45 am.
I’m in Chicago so I chose that timezone.
We get the current date and some other options for nodemailer to send the email to us.
Note: In a production application, the worst thing you could do is save all of your authentication data in plain text as I have done here. The thing you should be doing is saving those as config variables. You can read more about that here.
We’ll use a
try/catch because no one likes promise hell. You’ll have to get your own API key from MySportsFeeds. Notice that I’m using the v2 of the API, v1 has different data types that won’t work using this code. We only really need the
id of the game and venue. The
startTime in v2 comes in as UTC which we have to format for our timezone and time format. We create a constant of
timeCheck for the earliest an evening game would start. We create
intTime so we can easily compare
timeCheck to the
gameTime. We’ll use
parseInt to do so. We need to check to see if the game is at home. In this case, Wrigley Field has an
id of 133. We then await for the
transporter function to send out the email.
This has saved me from so many tickets. I wake up and instantly know where not to park.
I added everyone in the office to the list and the parking tickets went to 0. So, don’t remember stuff, automate it so you have more time to drink mimosas while you nurse a hangover.