Thursday, 14 December 2017

When I want to buy a shirt I write a shell script

Problem

I want to buy a shirt. It's a beautiful one. But unfortunately it is unavailable in my size. I need a solution that checks the product page regularly and notifies me when the shirt is available.

Input

The URL of the product page.

Output

Some kind of notification that lets me know my shirt is available.

Constraints

Don't spend more than a few minutes on this, write a throw-away solution. It doesn't need to be fool-proof or portable. Environment is cygwin.

Solution

Anything more than a simple shell script feels like an overkill. The notification can't be email because I have no access to an smtp server, let's use 'cygstart url'; it will open the product page in a browser. Wouldn't be acceptable if, for example, I used this computer for presentations, but it's a good solution in this case.
The script's first parameter is the product page including the product-specific query string, the second parameter is a html fragment that represents the product on that page.
#!/bin/sh

URL=$1
FRAGMENT=$2
 
while true; do
    NUM=`curl -s $URL | grep "$FRAGMENT" | wc -l`
    if [ "$NUM" -gt 0 ]
       then
           cygstart "$URL" 
           exit
       fi
    sleep 60m
done