-
Notifications
You must be signed in to change notification settings - Fork 11
/
pagewatch
115 lines (85 loc) · 3.38 KB
/
pagewatch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/bash
#----------------------------------------------------------------------------------
# Project Name - miscellaneous/pagewatch
# Started On - Wed 18 Oct 19:27:40 BST 2017
# Last Change - Tue 19 Dec 12:08:49 GMT 2017
# Author E-Mail - terminalforlife@yahoo.com
# Author GitHub - https://github.com/terminalforlife
#----------------------------------------------------------------------------------
# This came to be because I got excited and impatient waiting for Ubuntu 17.10 to
# be released and therefor available on the Ubuntu website.
#
# I devised a simple way to quickly and automatically check for changes on a given
# webpage, which for me was the download page, where I had hoped to see the new
# 17.10 download link.
#----------------------------------------------------------------------------------
XERR(){ printf "[L%0.4d] ERROR: %s\n" "$1" "$2" 1>&2; exit 1; }
ERR(){ printf "[L%0.4d] ERROR: %s\n" "$1" "$2" 1>&2; }
declare -i DEPCOUNT=0
for DEP in /bin/sleep /usr/bin/{md5sum,wget}; {
[ -x "$DEP" ] || {
ERR "$LINENO" "Dependency '$DEP' not met."
DEPCOUNT+=1
}
}
[ $DEPCOUNT -eq 0 ] || exit 1
shopt -s extglob
USAGE(){
while read -r; do
echo "$REPLY"
done <<-EOF
PAGEWATCH (19th December 2017)
Written by terminalforlife (terminalforlife@yahoo.com)
Watch a webpage for signs of change by checkings its md5sum.
SYNTAX: pagewatch [OPTS] URL COMMANDS
OPTS: --help|-h|-? - Displays this help information.
--debug|-D - Enables the built-in bash debugging.
--quiet|-q - Runs in quiet mode. Errors still output.
--interval|-i N - Check for changes every N seconds. The
default is to check every 60 seconds.
NOTE: Where COMMANDS would be your command(s) to execute upon detection of
any changes to the specified webpage. See below for examples.
EXAMPLE:
pagewatch --interval 10m "https://www.ubuntu.com" notify-send "Ready!"
CAVEAT: Some webpages update so frequently that pagewatch would for them be
no use. Such pages tend to have content like counters or timers.
EOF
}
while [ -n "$1" ]; do
case "$1" in
--help|-h|-\?)
USAGE; exit 0 ;;
--debug|-D)
DEBUGME="true" ;;
--quiet|-q)
BEQUIET="true" ;;
--interval|-i)
shift; INTERVAL="$1" ;;
-*|--*)
XERR "$LINENO" "Incorrect argument(s) specified." ;;
*)
break ;;
esac
shift
done
URL="$1"
shift
COMMANDS="$@"
[ -z "$URL" ] && XERR "$LINENO" "A webpage address (URL) is required."
[ $UID -eq 0 ] && XERR "$LINENO" "Root access isn't required."
[ "$BEQUIET" == "true" ] && exec 1> /dev/null
[ "$DEBUGME" == "true" ] && set -xeu
printf -v BUFFER "/tmp/pagewatch_%(%F_%X)T.md5" "-1"
[ -d /tmp ] || XERR "$LINENO" "Directory /tmp is missing or inaccessible."
/usr/bin/wget -q "$URL" -O - | /usr/bin/md5sum > "$BUFFER" 2> /dev/null
declare -i COUNT=0
while :; do
/usr/bin/wget -q "$URL" -O -\
| /usr/bin/md5sum -c "$BUFFER" &> /dev/null || ($COMMANDS)
COUNT+=1
#TODO - Somewhere here is an error (incorrect number of options?) when
# a change is detected with md5sum. COMMANDS also didn't happen.
printf "CHECK[#%0.4d]: %(%F (%X))T\n" "$COUNT"
/bin/sleep "${INTERVAL:-1m}"
done
# vim: noexpandtab colorcolumn=84 tabstop=8 noswapfile nobackup