-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch-url.sh
executable file
·77 lines (66 loc) · 1.47 KB
/
watch-url.sh
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
#!/usr/bin/env bash
set -euo pipefail
filter=cat
once=false
test=false
interval=60
action=mail
usage () {
echo "Usage: ${0##*/} [-mp] [-f filter] [-i sec] [-o] url address [address ...]"
echo " ${0##*/} [-f filter] -t url"
}
help () {
cat <<-EOF
Watch an URL and if it changes send a mail
The first non option argument is the url that will be watched.
Following arguments are email addresses that are notified on changes.
Options:
-t run in test mode
-i set the interval
-f filter to parse html before comparing
-o exit after the first change was found
EOF
}
fetch () {
curl --location --silent --connect-timeout 10 "$@" | $filter
}
while getopts hf:i:mopt FLAG; do
case $FLAG in
h) usage; help; exit;;
f) filter=$OPTARG;;
i) interval=$OPTARG;;
m) action=mail;;
o) once=true;;
p) action=print;;
t) test=true interval=1;;
*) usage >&2; exit 2;;
esac
done
shift $((OPTIND-1))
url=$1
shift
addresses=("$@")
dir=$(mktemp -d)
trap 'rm -rf "$dir"' EXIT
cd "$dir"
fetch "$url" > new
if "$test"; then
mv new old
sleep "$interval"
fetch "$url" > new
diff old new
exit
fi
while sleep "$interval"; do
mv new old
fetch "$url" > new
if ! diff -q old new; then
date "+%F %T: change detected"
if [[ $action = mail ]]; then
mail -s "URL Watcher: $url changed" "${addresses[@]}" <<-EOF
The URL $url has changed during the last $interval seconds.
EOF
fi
if "$once"; then exit; fi
fi
done