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.
By calling the API we receive a response that looks something like this:
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???
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.
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!
If you want to give this code a try, go ahead and download it!
This project is meant to scrape the rec.gov api for openings in select campgrounds between certain dates and email you…
Thanks for taking a look and have a wonderful day!