Photo by Detlev Klockow on Unsplash

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.

kosti marko
Oct 23, 2019 · 3 min read

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.

  1. Firebase Functions, to run a cron job every day to check for games.
  2. MySportsFeeds, a free service to get all of our Cubs data from.
  3. Moment.js and moment-timezone, because timezones suck.
  4. Axios, to make our HTTP request to MySportsFeeds.
  5. Nodemailer, to send out our email.

Shut up and code already

Gist: https://gist.github.com/kostimarko/659f073a903133a4c6f29bbc493c3bda

You’ll need to install the Firebase CLI:

npm install -g firebase-tools

Then create a new directory and initialize our firebase project

mkdir cubs-automation
cd cubs-automation
firebase init

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.

cd functions

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.

We can make use of the pubsub cron jobs to do a check every weekday. You can read more about it here and here.

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.

Another note: You will run into issues with Gmail. You’ll have to allow your email to use unsecure applications, more on that here and 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 startTime and 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.

Conclusion

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.

Firebase Developers

Tutorials, deep-dives, and random musings from Firebase…

kosti marko

Written by

Designer. Developer. All around hacker. You can read more about me here: https://www.kostimarko.design/

Firebase Developers

Tutorials, deep-dives, and random musings from Firebase developers all around the world. Views expressed are those of the authors and don’t necessarily reflect those of Firebase or its parent companies.

More From Medium

More from Firebase Developers

More from Firebase Developers

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade