Last active
October 24, 2024 22:11
-
-
Save aido/83edd2d3d4f0433877962771b352c407 to your computer and use it in GitHub Desktop.
Using only Bash built-ins send an IFTTT notification when a webpage is updated
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
declare -r CHECK_HOST=www.mtgox.com | |
declare -r CHECK_RESOURCE=/ | |
declare -r IFTTT_EVENT=MtGOX | |
declare -r IFTTT_KEY=<CHANGE ME> | |
declare -r IFTTT_HOST=maker.ifttt.com | |
declare -r IFTT_RESOURCE=/trigger/${IFTTT_EVENT}/with/key/${IFTTT_KEY} | |
declare -r IFTT_REQUEST="HEAD ${IFTT_RESOURCE} HTTP/1.1\r\nHost: ${IFTTT_HOST}\r\nConnection: Close\r\n\r\n" | |
declare -r DEBUG=off | |
declare -r -i SLEEP=300 | |
declare LAST_MODIFIED | |
echo_debug() { | |
[[ "$DEBUG" == "on" ]] && echo -e "$@" | |
} | |
get_Last-Modified() { | |
local -r REQUEST="HEAD ${CHECK_RESOURCE} HTTP/1.1\r\nHost: ${CHECK_HOST}\r\nConnection: Close\r\n\r\n" | |
LAST_MODIFIED="" | |
printf "%(%F %T)T Getting Last-Modified date\n" | |
echo_debug "\nRequest:\n$REQUEST" | |
exec {RESOURCE}<>"/dev/tcp/${CHECK_HOST}/80" | |
echo -e $REQUEST >&$RESOURCE | |
read -u $RESOURCE RESPONSE | |
echo_debug "\nResponse:\n${RESPONSE}" | |
# check for HTTP OK status | |
if [[ $RESPONSE =~ "200 OK" ]] ; then | |
while read -u $RESOURCE -a RESPONSE ; do | |
echo_debug "${RESPONSE[@]}" | |
[[ ${RESPONSE[0]} =~ "Last-Modified:" ]] && LAST_MODIFIED=${RESPONSE[@]:1} | |
done | |
fi | |
exec {RESOURCE}>&- | |
if [[ -z "$LAST_MODIFIED" ]] ; then | |
printf "%(%F %T)T Webpage does not seem to have a Last-Modified date\n" | |
else | |
printf "%(%F %T)T Got Last-Modified date ${LAST_MODIFIED}\n" | |
fi | |
} | |
check_If-Modified() { | |
local -r REQUEST="HEAD ${CHECK_RESOURCE} HTTP/1.1\r\nHost: ${CHECK_HOST}\r\nIf-Modified-Since: ${LAST_MODIFIED}\r\nConnection: Close\r\n\r\n" | |
local -i RET=1 | |
printf "%(%F %T)T Checking If-Modified-Since $LAST_MODIFIED\n" | |
echo_debug "\nRequest:\n$REQUEST" | |
exec {RESOURCE}<>"/dev/tcp/${CHECK_HOST}/80" | |
echo -e $REQUEST >&$RESOURCE | |
read -u $RESOURCE RESPONSE | |
exec {RESOURCE}>&- | |
echo_debug "\nResponse:\n${RESPONSE}" | |
# check for HTTP Not Modified or OK status | |
if [[ $RESPONSE =~ "304 Not Modified" ]] ; then | |
printf "%(%F %T)T Webpage has not been modified since $LAST_MODIFIED\n" | |
elif [[ $RESPONSE =~ "200 OK" ]] ; then | |
printf "%(%F %T)T Webpage has been modified since $LAST_MODIFIED\n" | |
LAST_MODIFIED="" | |
RET=0 | |
fi | |
return $RET | |
} | |
while true ; do | |
get_Last-Modified | |
while [[ ! -z "$LAST_MODIFIED" ]] ; do | |
read -t $SLEEP -N 0 | |
if check_If-Modified ; then | |
printf "%(%F %T)T Triggering IFTTT notification" | |
echo_debug "\nRequest:\n${IFTT_REQUEST}" | |
exec {RESOURCE}<>"/dev/tcp/${IFTTT_HOST}\/80" | |
echo -e $IFTT_REQUEST >&$RESOURCE | |
read -u $RESOURCE RESPONSE | |
exec {RESOURCE}>&- | |
echo_debug "\nResponse:\n${RESPONSE}" | |
fi | |
done | |
read -t $SLEEP -N 0 | |
done | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment