Have you missed out on a hot holiday gift? Me too... until I used NodeJS to send my phone an alert to buy!
This holiday season I saw a deal for a Nintendo Switch bundle which was perfect for my brother and his family. It was on sale for $299 but, unfortunately, by the time I went to get it there were no more available and third-party scalpers were selling it for over $500. Bummer.
However, I noticed that Amazon’s stock would occasionally come available with that $299 price tag every now and then; but I was never lucky enough to check at the right time.
So what does a software engineer do? Write a script to monitor the availability and send a message to my phone when it’s available to buy. And, it actually worked!
The script is three pieces that work together:
AmazonPriceCheckerthat fetches the Amazon product web page by its id and, using JSDOM, looks for the current price
TelegramBotthat simply encapsulates making a simple request to alert my phone via my personal Telegram bot.
- And a main server file that ties it all together to run the check loop, check the price threshold, and utilize the bot to alert my phone.
For this project, we'll need very few dependencies.
- If you haven't already, you'll need to install NodeJS
You'll also need TypeScript. I recommend installing globally:
npm install -g typescript
You'll then need
node-fetch. You can create a
package.jsonfile similar to below in your project directory and run
All we need this to do is fetch the Amazon product's web page by its product id and, using JSDOM, look for the current price in the DOM, and return it if found along with the url of the product itself.
So, to find how to scrape the price we'll use the view-source feature in a browser to see exactly what our script will see instead of the DevTools.
Luckily, it wasn't too hard to find Amazon is wrapping the price in an element with the id of
priceblock_ourprice. (At least for the Nintendo; it's possible other products have different markup.)
All together, our
AmazonPriceChecker looks like this:
For the next part of our script we want to alert our phone. I've previously written about how to create a personal Telegram Bot here:
All we need is our bot’s Api Key and the chat id our bot belongs to that we'll ping.
NOTE: You don't have to use Telegram to alert yourself. You can modify the code here to send an email, or trigger IFTTT in some way, etc. Options are up to you, I've chosen to use Telegram because I already had a personal bot setup to ping my phone :)
Now that we have our two separate pieces, we'll tie them together in our main server file where we'll loop to check every two minutes.
And then run our server file with NodeJs:
And that's it! This triggered an alert on my phone in about 4 hours or so and I was able to open Amazon directly to the product page and get the Switch, which was good because when I checked again four minutes later it shot back up to $500!
This is just a base that worked for me. We can take this add more checkers for other online stores, different products, turn it into a full "bot" that actually buys the product, etc. Consider this just a starting point of where we can go from here. Maybe for next holiday season.