Stock Checker Update: Run it on Windows

Background

My Stock Checker project had some interest from some Windows users. To be honest I should have seen it coming. The type of people looking for PC hardware that might find my script are most likely already on a PC (not a Mac) and most PC users use Windows. I think I didn't think about it as NodeJs development is almost exclusively in Unix environment. I even checked this in a previous post.

Since I had already done the groundwork, I knew that setting up and using WSL2 directly to run NodeJS was not that easy, so I needed another way.

I thought about using Docker to host it, which would allow it to be run anywhere, so I looked into that and ended up changing my script to using puppeteer instead of selenium webdriver, which is easier to use in Docker.

Installation

To run unix based Docker containers on Windows, 2 major components are required:

  • Docker
  • WSL2

Docker is a system which manages containers which are like minimal parts of virtual machines. Docker will read the Dockerfile in the codebase and set up the containers and run the script inside those containers.

WSL2 stands for Windows Subsystem for Linux 2, which allows Linux applications to run on Windows.

Installing WSL2

The link to the WSL2 installation can be found here on Microsoft's developer documentation site.

https://docs.microsoft.com/en-gb/windows/wsl/install-win10#step-4---download-the-linux-kernel-update-package

Step 4 is all that is needed for this project.

Virtualization might need to be turned on for your CPU to enable WSL2 and Docker.

Installing Docker

Get the Docker installation app from their website.

https://docs.docker.com/docker-for-windows/install/

When installing docker, make sure to enable the WSL2 and Hyper-V options.

Setting up Stock Checker

Download the code from github and unpack it wherever you want to keep the project.

Configuration

See the README file and the "Getting the XPaths that you need" part of the original Stock Checker post for specific information and tips on how to set up the config.json or sites.json files.

Make sure to set the following option in the config.json file or the run command will fail.

  "runHeadless": true,

Building with Docker

Once installed, a new container needs to be built by Docker.

If using PowerShell, navigate to the project directory and use the following command to achieve that:

docker build -t stock-checker .

To make life easier, I have created a Windows Batch file to do this instead.

Clicking a Batch file from random people on the internet is a bad idea, so please double check the content of the Batch file to see that it does the same docker build command.

Once you have double checked with a text editor, double click the docker-image-update.bat batch file to build with Docker.

Note: The container will have to be rebuilt anytime the config.json or sites.json files are changed.

Running with Docker

To run the stock checker, in PowerShell, navigate to the project directory and use the following command:

docker run stock-checker

Again, alternatively check the content of the batch file docker-run.bat and then double click it to run the Stock Checker instead.

If the script works, the terminal should open, do nothing for a while and close when done.

To test it, try adding some site configurations that will trigger a notification and see if the notification comes through.

Reminder: Every time the config.json or sites.json files are changed, the container needs to be rebuilt with Docker.

Scheduling Stock Checker

On Windows, the Task Scheduler is the best way to run things at given intervals.

Open the Task Scheduler in Windows.

Select "Create Basic Task".

Name the task something that makes sense and click "Next".

The Task should start running once you log in.

On the Action screen, select "Start a program".

For the "program/script" part, navigate to and select the docker-run.bat file.

Check the box to open properties and finish the task creation.

In the popup, edit the trigger.

Add:

  • 5 minute delay to allow Docker to get started up before running the script
  • Add a repeat time (15 minutes in this case). Keep in mind shorter delay might activate anti-DDOS protection on websites and they might block the script from accessing them after a few runs of the script. 15 minutes seems like a good balance for when I was using it.
  • Set "for a duration of" to "Indefinitely" to allow the script to keep repeating.

Make sure that Docker is running when this script runs by making Docker start on startup.

Done

So, that should be it and the script should run every 15 minutes or however often you have scheduled it.

Something to think about

I created this script to give people a chance against the scalpers. It won't beat the auto-checkout scripts that will buy things for you, but it will help to find out when things are in stock.

As with any tool, this could be used for evil and make things even worse.

I can't stop you from using this to get things online before anyone else gets them and then putting a massive markup on them, but please don't.

"With great power comes great responsibility" - Stan Lee