Skip to content

Instantly share code, notes, and snippets.

@grymoire7
Last active January 9, 2026 23:56
Show Gist options
  • Select an option

  • Save grymoire7/ae37f2416bfe95d38f73e4c9b1278c32 to your computer and use it in GitHub Desktop.

Select an option

Save grymoire7/ae37f2416bfe95d38f73e4c9b1278c32 to your computer and use it in GitHub Desktop.
#!/bin/bash
CONFIG_FILE="$XDG_CONFIG_HOME/website_watch.json"
STATE_FILE="$XDG_STATE_HOME/website_watch_state.json"
LOCK_FILE="/tmp/website_watch.lock"
TMP_FILE=$(mktemp)
SCRIPT_PATH="$(readlink -f "${BASH_SOURCE[0]}")"
CRON_CMD="@daily bash $SCRIPT_PATH"
# Exit if another instance of the script is running
if [ -e "$LOCK_FILE" ]; then
echo "Script is already running. Exiting."
exit 1
fi
# Create a lock file and clean it up on exit
trap "rm -f $LOCK_FILE $TMP_FILE" EXIT
touch "$LOCK_FILE"
# Ensure configuration file exists
if [ ! -f "$CONFIG_FILE" ]; then
echo "Configuration file not found at $CONFIG_FILE"
exit 1
fi
# Ensure state file exists
if [ ! -f "$STATE_FILE" ]; then
echo "{}" > "$STATE_FILE"
fi
# Add script to cron if not already present
if ! crontab -l | grep -q "$CRON_CMD"; then
(crontab -l; echo "$CRON_CMD") | crontab -
fi
# Function to send notifications
send_notification() {
local message="$1"
local title="$2"
if command -v osascript &>/dev/null; then
osascript -e "display notification \"$message\" with title \"$title\""
elif command -v notify-send &>/dev/null; then
notify-send "$title" "$message"
else
echo "Notification: $title - $message"
fi
}
# Process each entry in the config file
jq -c '.[]' "$CONFIG_FILE" | while read -r entry; do
url=$(echo "$entry" | jq -r '.url')
regex=$(echo "$entry" | jq -r '.regex')
message=$(echo "$entry" | jq -r '.message // "Website change detected at $url"')
title=$(echo "$entry" | jq -r '.title // "Website Change Detected"')
# Fetch webpage
if ! curl -s "$url" -o "$TMP_FILE"; then
echo "Failed to fetch URL: $url"
continue
fi
# Check for regex match
if grep -qE "$regex" "$TMP_FILE"; then
prev_state=$(jq -r --arg url "$url" '.[$url] // "not_found"' "$STATE_FILE")
if [ "$prev_state" != "found" ]; then
send_notification "$message" "$title"
jq --arg url "$url" '.[$url] = "found"' "$STATE_FILE" > "${STATE_FILE}.tmp" && mv "${STATE_FILE}.tmp" "$STATE_FILE"
fi
else
jq --arg url "$url" '.[$url] = "not_found"' "$STATE_FILE" > "${STATE_FILE}.tmp" && mv "${STATE_FILE}.tmp" "$STATE_FILE"
fi
done
[
{
"url": "https://locations.dollartree.com/il/chicago",
"regex": "Some St",
"message": "Dollar Tree on Some Street in now open",
"title": "Dollar Tree Open"
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment