How find openings in rec.gov campsites using Dart and AWS Lambda

What a title right? The backstory is that I have a friend visiting from out of state and wanted to take them to Yosemite. The problem is that Yosemite requires reservations to enter the park due to COVID and all the dates are booked!

I don’t want to always go and check rec.gov to look for openings because the chances of finding one are miniscule. I’d prefer a bot to look through the listings automatically and let me know when there’s an opening.

So how can we grab data about reservation availability on rec.gov?

Well if we dig into the network section of inspector while pulling up the availability for a site, we can see rec.gov making a request to it’s campground API for the selected month!

If we take a closer look at the api call, we can see the campground ID after the campground path followed by the query date. Quick note: in the URL of the campsite registration, you can find the campground ID.

https://www.recreation.gov/api/camps/availability/campground/232450/month?start_date=2021-10-01T00%3A00%3A00.000Z

By calling the API we receive a response that looks something like this:

response from rec.gov api
response from rec.gov api
rec.gov api response
Halfway to getting a campsite! 🏕

This is perfect! Now we just have to call this api with the different campground ID’s we’re looking for and query the data for availabilities on the dates we’re looking for. Once we find matches, we’ll want some way to get notified so I think email is easiest for that. But first, how do we want to do all this processing???

Why Dart?

I’m choosing dart over other languages like Python and Javascript because I want to get more familiar with using dart on the backend and I really like the tooling and package management. Pub.dev is an amazing package resource and I really like how much better dart is at handling versions and dependencies than Python or Javascript.

Why Lambda?

As for lambda, this is a personal project and I’m cheap so I don’t really want to fire up a server 24/7 if I only need to run 2 seconds of code every minute. Lambda is great because it’s serverless and they give you 400,000 GB-Seconds per month for free! Now the function only needs around 128 mb for the VM so that means we have plenty of free time to run our function with! If we ran a 5 second function every minute of every day for a month, you can see the calculation as follows, but we’d only use 27,900 GB-Seconds. We could run this function every 15 seconds and still have plenty of free headroom!

The heart of the code through is really just iterating through different campground IDs and pulling the response apart to look for availability within any of the results between the start and end date.

To help facilitate the API call and emailing I’m using the http and the sendgrid_mailer package. These make the web functions super easy and leaves the hard part being the data processing.

Preparing code for Lambda

Once all the code functions appropriately on a local device and we’re ready to ship it up to a lambda function, we just need to do a few steps to prep it. Lambda functions run on a skim version of linux and as such, any code needs to be compiled on a similar OS. To accomplish this, it’s best to use a docker container provided by Google to to compile the code into a usable format for Lambda.

We also need to ensure that we have the appropriate handlers for the AWS events we expect. I’m using this function on a CloudWatch event and as such, I have a handler that looks something like this:

Once the function is in lambda and on a CloudWatch event, you can keep an eye on it with CloudWatch metrics included by default in the Lambda function!

CloudWatch metrics

If you want to give this code a try, go ahead and download it!

Thanks for taking a look and have a wonderful day!

Working to solve to worlds problems starting with technology in agriculture.