Unlock the Power of Automation: A Step-by-Step Guide to Booking Badminton Courts

The Journey Begins

As I settled into my new life in Redmond, WA, I knew I needed to stay active and engaged. That’s when I discovered the popularity of badminton in the area. I decided to join a local academy, but soon realized that booking a court was no easy feat. The prime time slots (4-8 pm) were always snatched up within minutes of opening on Saturday mornings. As a programmer, I knew there had to be a way to automate this process. In this article, I’ll walk you through the manual booking process and how I successfully automated it using Python and Selenium.

Understanding the Manual Booking Process

Before we dive into automation, it’s essential to grasp the manual workflow. The Northwest Badminton Academy’s reservation system is outsourced to Zen Planner, which means our automation will work for other places using the same platform. Here’s a breakdown of the rules:

  • Book only 1 slot per day
  • Limit of 3 slots on weekdays (Mon-Fri)
  • Book 1 slot per day on weekends (Sat-Sun)

The calendar page displays available time slots. Clicking on an open slot takes you to the reservation page, where you can book your court.

Getting Started with Automation

To automate the booking process, we’ll use Python 3.9 and Selenium. Let’s set up our project:

  1. Create a new folder and virtual environment.
  2. Install Selenium and download the ChromeDriver executable for your operating system.
  3. Create an app.py file and start coding!

Automating Login

We’ll use Selenium to interact with the login page. We need to identify the username and password fields, and the submit button. We can use various methods to locate these elements, such as find_element_by_id, find_element_by_name, or find_element_by_xpath. In this case, we’ll use find_element_by_name and find_element_by_xpath.

Navigating to the Calendar Page

Once logged in, we need to navigate to the calendar page. We’ll locate the calendar button using XPath and click on it. Then, we’ll extract the PERSON identifier from the URL to recreate the date-specific URL pattern.

Booking a Court

Now, we’ll automate the court booking process. We’ll locate the available time slots, check if our preferred time is available, and book it if possible. We’ll also add logic to handle cases where our preferred time is not available.

Finalizing the Automation

We’ve covered all the necessary steps. Let’s put them together in our app.py file and add some structure. The final code is divided into four functions: login, navigate to calendar, book court, and main.

Tips and Tricks

To further develop this project, consider the following:

  • Run Chrome in headless mode to avoid displaying the browser window.
  • Schedule the script to run automatically on Saturday mornings using crontab or GitHub Actions.
  • Add better error handling and parallel processing to check multiple days simultaneously.

Download the complete source code for the bot from GitHub and explore more fun projects on my blog!

Leave a Reply

Your email address will not be published. Required fields are marked *