Warriors Alert Lights!

September 28, 2021

The What:

A raspberry pi Warriors games alerter. It will light up a series of seven lights, with the left-most light representing the current day and the next six lights being the next six days. If the Warriors are playing on those days, the lights will be turned on! If not, they remain off. I’d love to post a picture of the finished result… but the parts are coming in the mail. In the meantime, here are the coding bits!

The How:

So, it turns out that there are numerous websites that provide the NBA schedule in a handy format, but you have to pay for those. So, instead, I scraped the data from the NBA website and put them in a text document.

How did I do that?

I phoned a friend for help. It was great. I highly recommend making friends with people smarter than you. Anyways, here it is, for the curious:

These dates were what I wanted:

warriors dates

This is how I got them:

console

This was taken from the official NBA website, after the schedule was filtered for the Warriors. These dates were then unceremoniously dumped into a txt document and saved into my warriors notifier folder. Here is the github link if you’d like to see it: https://github.com/cat-does/warriors-notifier.git

So now I’ve got a text document with eighty something game dates. The first thing I wanted to do was to go down the text file, line by line, and convert it into an array of date strings.

const readFile = require("fs").readFile;

const monthMapping = {
  "January": 0,
  "February": 1,
  "March": 2,
  "April": 3,
  "May": 4,
  "June": 5,
  "July": 6,
  "August": 7,
  "September": 8,
  "October": 9,
  "November": 10,
  "December": 11,
}

readFile('schedule.txt', { encoding: "utf8" }, (err, data) => {
  if (err) throw err;

  // an array of date strings
  let dates = data.split("\n").map(dateString => {
    const month = dateString.split(" ")[1];
    const day = dateString.split(" ")[2];
    const monthIndex = monthMapping[month];
    const year = (monthIndex < 9) ? 2022 : 2021

    return new Date(year, monthIndex, parseInt(day)).toDateString();
  })

For more about readFile, I’d like to redirect your attention to this page.

I might be wrong, but I couldn’t find a way to automatically translate a string like “Monday, October 4” into a date string. To solve this, I created a hash and assigned the months to numbers, and fed the split parts into new Date in order to get the date time object I wanted. Wordy? Yeah probably, but at my level of coding experience, I’m sorry to say that everything that comes out of my keyboard is kinda sorta maybe a lot like that. I’m working on it, I swear.

Moving on!

  const today = new Date();
  const ourDates = [];
  for (let i = 0; i < 7; i++) {
    const nextDate = new Date(today.valueOf());
    nextDate.setDate(nextDate.getDate() + i);
    ourDates.push(nextDate);
  }

Now I wanted to get an array of today’s date plus six more days. valueOf() returns us the number of milliseconds between 1 January 1970, and the sepcified date. setDate sets the date to the number you feed it, and automatically changes the month if it goes past that month’s last day (28/30/31 for clarity, I realize that’s confusing wording). The reason why we want to use valueOf() is… honestly, I wish I could tell you. I just know that I kept getting the wrong values when I didn’t use it. I assume that converting the date into a primitive object allows it to be more easily manipulated. I’d phone my friend on this so I could write a better blog post but it’s 11:30pm and that would be a rude call, even by my standards.

Now, to translate this into whether lights should go on or off, at least until the lights come to my mailbox:

for (let i = 0; i < ourDates.length; i++) {
  if (dates.indexOf(ourDates[i].toDateString()) !== -1) {
    console.log('y');
  } else {
    console.log('n');
  }
}

Here, I’m iterating through our array of dates, finding out whether it exists in the array of scheduled Warriors games, and printing out a “y” if it exists and a “n” if it doesn’t.

And that’s it! Part two pending. :)