How I got a Nintendo Switch using NodeJS
Regis Gaughan, III
Posted on December 23, 2020
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!
rgthree / pricewatch
An Amazon Price Watcher with phone alerting via Telegram
How it works.
The script is three pieces that work together:
- An
AmazonPriceChecker
that fetches the Amazon product web page by its id and, using JSDOM, looks for the current price - A
TelegramBot
that 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.
Set up
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
jsdom
andnode-fetch
. You can create apackage.json
file similar to below in your project directory and runnpm install
:
The AmazonPriceChecker
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.
One thing to keep in mind is we’re fetching the web page with our server. We’ll override the User-Agent so it looks like a browser, but the response back will be raw HTML and possibly different than the markup we see when using Amazon as JavaScript is likely modifying the page substantially after that raw HTML come back.
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:
Our TelegramBot
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:
Personal Telegram Bot for Alerting Your Phone (w/o code)
Regis Gaughan, III ・ Dec 9 '20
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 :)
Tying it all together
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.
Start it up
First, run the TypeScript compiler which will generate JavaScript files from our neatly typed TypeScript files:
tsc
And then run our server file with NodeJs:
node server.js
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!
Enhance!
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.
rgthree / pricewatch
An Amazon Price Watcher with phone alerting via Telegram
Posted on December 23, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.