Monitor changes on a webpage

Say that you are waiting for some results, that will be announced of a web page some time during the day… You can spend the day hitting Ctrl-R on your navigator (to refresh the page), or you can try the script pasted below. It will check for changes on the web page every 15 minutes (you can change the timing), will stop once a change is detected, and will then open the web page so that you can see the new version.


#!/bin/bash

#--------------------------------------------------------------------------------
# Monitors the changes in a webpage
#
# To run it:
# ./monitorwebpage.sh http://www.thewebpage.io mywebpage
# where `http://www.thewebpage.io` is the address of the page you want to follow,
# and mywebpage a suffix (default: page), needed if you monitor multiple pages at once.
# -> The program checks the website every 15 minutes (timing can be changed),
#    and if there is a change, opens the new webpage.
#
#
#
# NOTES:
#
# This script works with Ubuntu;
# On a Mac, replace "xdg-open" by "open"
#
# Source of some of the code:
# http://bhfsteve.blogspot.fr/2013/03/monitoring-web-page-for-changes-using.html
#--------------------------------------------------------------------------------

# URL to monitor is entered as input
URL="$1"
# The second argument is optional, it is a suffix for the webpage
# if we want to monitor multiple webpages at once
SUFF=${2:-page}

# Error message if not argument is supplied
if [[ -z "$*"  ]] ; then
echo "Error: you need to enter a webpage as argument!
(don't forget http://)"
exit 0
fi

echo "Monitoring "${URL}

# Initialization: download the webpage once to create old.html
curl ${URL} -L --compressed -s > old${SUFF}.html

for (( ; ; )); do # (Infinite loop)
# Change the name of new.html into old.html,
# and erase potential error messages (2> /dev/null)
mv new${SUFF}.html old${SUFF}.html 2> /dev/null

# Print time at which the page is checked
THEDATE="$(date)"
echo "Last checked: ${THEDATE}"

# Download the webpage
curl ${URL} -L --compressed -s > new${SUFF}.html
#  -L means that we follow potential redirections
#  --compressed compresses,
#  -s for quiet mode,
#  and save the output in new.html.

# Compare the two versions
DIFF_OUTPUT="$(diff new${SUFF}.html old${SUFF}.html)"
# If there are differences:
if [ "${DIFF_OUTPUT}" != "" ]; then
# open the webpage
xdg-open ${URL}
break
fi
# Then wait for 15 minutes before checking the website again
sleep 15m
done
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s