-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.sh
More file actions
executable file
·960 lines (837 loc) · 30.6 KB
/
Copy pathscript.sh
File metadata and controls
executable file
·960 lines (837 loc) · 30.6 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
#!/bin/bash
# ==============================================================================
# web-watcher
# Watch APIs & Websites for changes — Get notified instantly from your terminal
# ==============================================================================
set -euo pipefail
# --- Configuration & Defaults ---
VERSION="1.1.1"
DEFAULT_INTERVAL=30
DEFAULT_THRESHOLD=0
DEFAULT_METHOD="GET"
DEFAULT_TIMEOUT=15
DEFAULT_RETRIES=3
DEFAULT_RETRY_DELAY=5
DEFAULT_MODE="auto"
DEFAULT_USER_AGENT="web-watcher/$VERSION"
# --- Runtime State ---
INTERVAL="$DEFAULT_INTERVAL"
THRESHOLD="$DEFAULT_THRESHOLD"
METHOD="$DEFAULT_METHOD"
TIMEOUT="$DEFAULT_TIMEOUT"
RETRIES="$DEFAULT_RETRIES"
RETRY_DELAY="$DEFAULT_RETRY_DELAY"
MODE="$DEFAULT_MODE"
USER_AGENT="$DEFAULT_USER_AGENT"
URL=""
BODY=""
JQ_FILTER=""
SELECTOR=""
HEADERS=()
COOKIES=""
AUTH=""
LOG_FILE=""
SNAPSHOT_DIR=""
VERBOSE=false
QUIET=false
SOUND=true
NO_COLOR=false
FOLLOW_REDIRECTS=true
INSECURE=false
SHOW_DIFF=false
ONCE=false
MAX_RUNS=0
STRIP_HTML=false
HAS_DIFF=false
BASELINE_FILE=""
SLACK_WEBHOOK=""
DISCORD_WEBHOOK=""
TELEGRAM_TOKEN=""
TELEGRAM_CHAT_ID=""
# --- Colors ---
setup_colors() {
if [ -t 1 ] && [ "$NO_COLOR" = false ]; then
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
MAGENTA='\033[0;35m'
CYAN='\033[0;36m'
BOLD='\033[1m'
DIM='\033[2m'
NC='\033[0m'
else
RED='' GREEN='' BLUE='' YELLOW='' MAGENTA='' CYAN='' BOLD='' DIM='' NC=''
fi
}
# --- Helper Functions ---
log_info() {
[ "$QUIET" = true ] && return
printf "${BLUE}[INFO]${NC} %s\n" "$1"
}
log_success() {
printf "${GREEN}[OK]${NC} %s\n" "$1"
}
log_warn() {
printf "${YELLOW}[WARN]${NC} %s\n" "$1" >&2
}
log_error() {
printf "${RED}[ERROR]${NC} %s\n" "$1" >&2
}
log_change() {
printf "${MAGENTA}[CHANGE]${NC} %s\n" "$1"
}
log_verbose() {
if [ "$VERBOSE" = true ]; then
printf "${DIM}[DEBUG] %s${NC}\n" "$1"
fi
}
log_to_file() {
if [ -n "$LOG_FILE" ]; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE"
fi
}
# --- Notification ---
send_notification() {
local title="$1"
local message="$2"
# Terminal bell
if [ "$SOUND" = true ]; then
printf '\a'
fi
# OS-level notification
if [[ "$OSTYPE" == "darwin"* ]]; then
osascript -e "display notification \"$message\" with title \"$title\" sound name \"Glass\"" 2>/dev/null || true
elif command -v notify-send &>/dev/null; then
notify-send "$title" "$message" 2>/dev/null || true
fi
# Webhook notifications
[ -n "$SLACK_WEBHOOK" ] && send_slack "$title" "$message"
[ -n "$DISCORD_WEBHOOK" ] && send_discord "$title" "$message"
[ -n "$TELEGRAM_TOKEN" ] && [ -n "$TELEGRAM_CHAT_ID" ] && send_telegram "$title" "$message"
# Always print to terminal
echo ""
echo -e "${RED}${BOLD}╔══════════════════════════════════════════════════════════════╗${NC}"
echo -e "${RED}${BOLD}║ 🔔 CHANGE DETECTED ║${NC}"
echo -e "${RED}${BOLD}╚══════════════════════════════════════════════════════════════╝${NC}"
echo -e " ${BOLD}URL:${NC} $URL"
echo -e " ${BOLD}Time:${NC} $(date '+%Y-%m-%d %H:%M:%S')"
echo -e " ${BOLD}Info:${NC} $message"
echo ""
}
# --- Webhook Notifications ---
send_slack() {
local title="$1" message="$2"
curl -s -X POST -H 'Content-Type: application/json' \
-d "{\"text\":\"*${title}*\n${message}\n${URL}\"}" \
"$SLACK_WEBHOOK" >/dev/null 2>&1 || log_warn "Slack notification failed"
}
send_discord() {
local title="$1" message="$2"
curl -s -X POST -H 'Content-Type: application/json' \
-d "{\"content\":\"**${title}**\n${message}\n${URL}\"}" \
"$DISCORD_WEBHOOK" >/dev/null 2>&1 || log_warn "Discord notification failed"
}
send_telegram() {
local title="$1" message="$2"
curl -s -X POST \
"https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendMessage" \
-d "chat_id=${TELEGRAM_CHAT_ID}&text=${title}
${message}
${URL}&parse_mode=Markdown" >/dev/null 2>&1 || log_warn "Telegram notification failed"
}
# --- Banner ---
print_banner() {
[ "$QUIET" = true ] && return
echo -e "${CYAN}"
echo " ██╗ ██╗███████╗██████╗ ██╗ ██╗ █████╗ ████████╗ ██████╗██╗ ██╗███████╗██████╗ "
echo " ██║ ██║██╔════╝██╔══██╗ ██║ ██║██╔══██╗╚══██╔══╝██╔════╝██║ ██║██╔════╝██╔══██╗"
echo " ██║ █╗ ██║█████╗ ██████╔╝█████╗██║ █╗ ██║███████║ ██║ ██║ ███████║█████╗ ██████╔╝"
echo " ██║███╗██║██╔══╝ ██╔══██╗╚════╝██║███╗██║██╔══██║ ██║ ██║ ██╔══██║██╔══╝ ██╔══██╗"
echo " ╚███╔███╔╝███████╗██████╔╝ ╚███╔███╔╝██║ ██║ ██║ ╚██████╗██║ ██║███████╗██║ ██║"
echo " ╚══╝╚══╝ ╚══════╝╚═════╝ ╚══╝╚══╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝"
echo -e "${NC}"
echo -e " Watch APIs & Websites for changes v$VERSION"
echo ""
}
# --- Usage ---
print_usage() {
echo "Usage: web-watcher [options] <url>"
echo ""
echo "Arguments:"
echo " url URL to watch (API endpoint or website)"
echo ""
echo "Request Options:"
echo " -X, --method <METHOD> HTTP method: GET, POST, PUT, etc. (default: GET)"
echo " -H, --header <header> Add a custom header (repeatable)"
echo " Example: -H 'Authorization: Bearer token123'"
echo " -d, --data <body> Request body for POST/PUT requests"
echo " -C, --cookie <cookie> Cookie string or file path"
echo " -A, --user-agent <ua> Custom User-Agent string"
echo " --auth <user:pass> Basic authentication credentials"
echo " --timeout <secs> Request timeout in seconds (default: 15)"
echo " --no-follow Do not follow redirects"
echo " --insecure Allow insecure SSL connections"
echo ""
echo "Watch Options:"
echo " -i, --interval <secs> Seconds between checks (default: 30)"
echo " -p, --threshold <percent> Min change % to trigger notification (default: 0 = any change)"
echo " -n, --max-runs <num> Stop after N checks (0 = unlimited, default: 0)"
echo " --once Run a single check then exit (useful for cron)"
echo " --baseline-file <file> Persist baseline to disk (required for --once to detect changes)"
echo " --retries <num> Number of retries on failure (default: 3)"
echo " --retry-delay <secs> Delay between retries in seconds (default: 5)"
echo ""
echo "Content Options:"
echo " -m, --mode <mode> Watch mode: api, website, auto (default: auto)"
echo " api = compare raw response (JSON, XML, etc.)"
echo " website = extract text content, strip HTML tags"
echo " auto = detect from Content-Type header"
echo " -f, --filter <jq_expr> jq filter for JSON responses (e.g., '.data.price')"
echo " -s, --selector <pattern> Grep pattern to extract specific content from HTML"
echo " --strip-html Force HTML tag stripping (useful with --mode api)"
echo ""
echo "Notification Options:"
echo " --slack <url> Slack incoming webhook URL"
echo " --discord <url> Discord webhook URL"
echo " --telegram-token <token> Telegram bot token"
echo " --telegram-chat <chat_id> Telegram chat ID"
echo ""
echo "Output Options:"
echo " -l, --log <file> Log changes to a file"
echo " --snapshot-dir <dir> Save snapshots of each response"
echo " --diff Show unified diff of changes"
echo " --no-sound Disable terminal bell on change"
echo " -q, --quiet Suppress info output (only show changes)"
echo " -v, --verbose Verbose debug output"
echo " --no-color Disable colored output"
echo " -h, --help Show this help message"
echo " --version Show version"
echo ""
echo "Examples:"
echo " # Watch a JSON API for any change, check every 60s"
echo " web-watcher -i 60 https://api.example.com/products"
echo ""
echo " # Watch sneaker stock API with auth header, filter price field"
echo " web-watcher -i 10 -H 'Authorization: Bearer mytoken' \\"
echo " -f '.products[].price' https://api.sneakers.com/v1/stock"
echo ""
echo " # Watch a website, notify if more than 5% content changes"
echo " web-watcher -m website -p 5 -i 120 https://www.nike.com/launches"
echo ""
echo " # Watch with POST request and custom body"
echo " web-watcher -X POST -H 'Content-Type: application/json' \\"
echo " -d '{\"query\": \"jordan 1\"}' https://api.shop.com/search"
echo ""
echo " # Watch and log changes, save snapshots"
echo " web-watcher -i 30 -l changes.log --snapshot-dir ./snapshots \\"
echo " https://api.example.com/data"
echo ""
echo " # Watch specific part of a webpage"
echo " web-watcher -m website -s 'class=\"product-price\"' -i 60 \\"
echo " https://www.shop.com/product/123"
echo ""
echo " # Single check with persistent baseline (for cron jobs)"
echo " web-watcher --once --baseline-file /tmp/ww_status.txt https://api.example.com/status"
}
# --- Dependency Check ---
check_dependencies() {
local missing=()
if ! command -v curl &>/dev/null; then
missing+=("curl")
fi
if [ -n "$JQ_FILTER" ] && ! command -v jq &>/dev/null; then
missing+=("jq (required for --filter)")
fi
if command -v diff &>/dev/null; then
HAS_DIFF=true
else
HAS_DIFF=false
fi
if [ ${#missing[@]} -gt 0 ]; then
log_error "Missing required dependencies: ${missing[*]}"
echo ""
echo "Install them with:"
if [[ "$OSTYPE" == "darwin"* ]]; then
echo " brew install ${missing[*]}"
elif command -v apt-get &>/dev/null; then
echo " sudo apt-get install ${missing[*]}"
elif command -v dnf &>/dev/null; then
echo " sudo dnf install ${missing[*]}"
elif command -v pacman &>/dev/null; then
echo " sudo pacman -S ${missing[*]}"
fi
exit 1
fi
}
# --- Argument Parsing ---
parse_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
-X|--method)
METHOD="${2:?'--method requires a value'}"
shift 2
;;
-H|--header)
HEADERS+=("${2:?'--header requires a value'}")
shift 2
;;
-d|--data)
BODY="${2:?'--data requires a value'}"
shift 2
;;
-C|--cookie)
COOKIES="${2:?'--cookie requires a value'}"
shift 2
;;
-A|--user-agent)
USER_AGENT="${2:?'--user-agent requires a value'}"
shift 2
;;
--auth)
AUTH="${2:?'--auth requires a value'}"
shift 2
;;
--timeout)
TIMEOUT="${2:?'--timeout requires a value'}"
shift 2
;;
--no-follow)
FOLLOW_REDIRECTS=false
shift
;;
--insecure)
INSECURE=true
shift
;;
-i|--interval)
INTERVAL="${2:?'--interval requires a value'}"
if ! [[ "$INTERVAL" =~ ^[0-9]+$ ]] || [ "$INTERVAL" -lt 1 ]; then
log_error "Interval must be a positive integer (seconds)"
exit 1
fi
shift 2
;;
-p|--threshold)
THRESHOLD="${2:?'--threshold requires a value'}"
if ! [[ "$THRESHOLD" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
log_error "Threshold must be a number (percentage)"
exit 1
fi
shift 2
;;
-n|--max-runs)
MAX_RUNS="${2:?'--max-runs requires a value'}"
shift 2
;;
--once)
ONCE=true
shift
;;
--baseline-file)
BASELINE_FILE="${2:?'--baseline-file requires a value'}"
shift 2
;;
--retries)
RETRIES="${2:?'--retries requires a value'}"
shift 2
;;
--retry-delay)
RETRY_DELAY="${2:?'--retry-delay requires a value'}"
shift 2
;;
-m|--mode)
MODE="${2:?'--mode requires a value'}"
if [[ ! "$MODE" =~ ^(api|website|auto)$ ]]; then
log_error "Mode must be one of: api, website, auto"
exit 1
fi
shift 2
;;
-f|--filter)
JQ_FILTER="${2:?'--filter requires a value'}"
shift 2
;;
-s|--selector)
SELECTOR="${2:?'--selector requires a value'}"
shift 2
;;
--strip-html)
STRIP_HTML=true
shift
;;
--slack)
SLACK_WEBHOOK="${2:?'--slack requires a value'}"
shift 2
;;
--discord)
DISCORD_WEBHOOK="${2:?'--discord requires a value'}"
shift 2
;;
--telegram-token)
TELEGRAM_TOKEN="${2:?'--telegram-token requires a value'}"
shift 2
;;
--telegram-chat)
TELEGRAM_CHAT_ID="${2:?'--telegram-chat requires a value'}"
shift 2
;;
-l|--log)
LOG_FILE="${2:?'--log requires a value'}"
shift 2
;;
--snapshot-dir)
SNAPSHOT_DIR="${2:?'--snapshot-dir requires a value'}"
shift 2
;;
--diff)
SHOW_DIFF=true
shift
;;
--no-sound)
SOUND=false
shift
;;
-q|--quiet)
QUIET=true
shift
;;
-v|--verbose)
VERBOSE=true
shift
;;
--no-color)
NO_COLOR=true
shift
;;
-h|--help)
print_banner
print_usage
exit 0
;;
--version)
echo "web-watcher v$VERSION"
exit 0
;;
-*)
log_error "Unknown option: $1"
echo "Use --help for usage information."
exit 1
;;
*)
if [ -z "$URL" ]; then
URL="$1"
else
log_error "Unexpected argument: $1"
exit 1
fi
shift
;;
esac
done
if [ -z "$URL" ]; then
log_error "No URL provided."
echo "Use --help for usage information."
exit 1
fi
# Validate URL scheme
if [[ ! "$URL" =~ ^https?:// ]]; then
log_error "Invalid URL: '$URL' — must start with http:// or https://"
exit 1
fi
}
# --- Build curl command ---
# Response delimiter — unlikely to appear in real content
RESPONSE_DELIM="__WW_DELIM_7f3a9e__"
build_curl_cmd() {
CURL_CMD=(curl -s -w "\n${RESPONSE_DELIM}HTTP_CODE:%{http_code}\n${RESPONSE_DELIM}CONTENT_TYPE:%{content_type}" --max-time "$TIMEOUT")
# Method
CURL_CMD+=(-X "$METHOD")
# Headers
if [ ${#HEADERS[@]} -gt 0 ]; then
for h in "${HEADERS[@]}"; do
CURL_CMD+=(-H "$h")
done
fi
# User-Agent
CURL_CMD+=(-A "$USER_AGENT")
# Body
if [ -n "$BODY" ]; then
CURL_CMD+=(-d "$BODY")
fi
# Cookies
if [ -n "$COOKIES" ]; then
CURL_CMD+=(--cookie "$COOKIES")
fi
# Auth
if [ -n "$AUTH" ]; then
CURL_CMD+=(-u "$AUTH")
fi
# Follow redirects
if [ "$FOLLOW_REDIRECTS" = true ]; then
CURL_CMD+=(-L)
fi
# Insecure
if [ "$INSECURE" = true ]; then
CURL_CMD+=(-k)
fi
# -- separates options from URL (prevents URL starting with - being treated as flag)
CURL_CMD+=(-- "$URL")
}
# --- Fetch URL ---
fetch_url() {
local attempt=1
build_curl_cmd
while [ "$attempt" -le "$RETRIES" ]; do
log_verbose "Attempt $attempt/$RETRIES — fetching $URL"
local raw_output
raw_output=$("${CURL_CMD[@]}" 2>/dev/null) || {
log_warn "Request failed (attempt $attempt/$RETRIES)"
if [ "$attempt" -lt "$RETRIES" ]; then
log_verbose "Retrying in ${RETRY_DELAY}s..."
sleep "$RETRY_DELAY"
fi
attempt=$((attempt + 1))
continue
}
# Parse response using our custom delimiters
local http_code content_type response
http_code=$(echo "$raw_output" | grep "^${RESPONSE_DELIM}HTTP_CODE:" | sed "s/^${RESPONSE_DELIM}HTTP_CODE://")
content_type=$(echo "$raw_output" | grep "^${RESPONSE_DELIM}CONTENT_TYPE:" | sed "s/^${RESPONSE_DELIM}CONTENT_TYPE://")
response=$(echo "$raw_output" | grep -v "^${RESPONSE_DELIM}")
# Check HTTP status
if [[ "$http_code" =~ ^[45] ]]; then
log_warn "HTTP $http_code received (attempt $attempt/$RETRIES)"
if [ "$attempt" -lt "$RETRIES" ]; then
sleep "$RETRY_DELAY"
fi
attempt=$((attempt + 1))
continue
fi
log_verbose "HTTP $http_code — Content-Type: $content_type"
# Return results via global variables
LAST_RESPONSE="$response"
LAST_HTTP_CODE="$http_code"
LAST_CONTENT_TYPE="$content_type"
return 0
done
log_error "All $RETRIES attempts failed for $URL"
log_to_file "FETCH_FAILED — All $RETRIES attempts failed"
return 1
}
# --- Content Processing ---
detect_mode() {
local content_type="$1"
if [ "$MODE" != "auto" ]; then
echo "$MODE"
return
fi
if echo "$content_type" | grep -qi 'application/json\|text/json\|application/.*+json'; then
echo "api"
elif echo "$content_type" | grep -qi 'text/html\|application/xhtml'; then
echo "website"
else
echo "api"
fi
}
strip_html_tags() {
# Remove script/style blocks, then HTML tags, then normalize whitespace
# Use character classes [Ss] for portability (BSD sed has no case-insensitive flag)
sed -E 's/<[Ss][Cc][Rr][Ii][Pp][Tt][^>]*>[^<]*<\/[Ss][Cc][Rr][Ii][Pp][Tt]>//g' |
sed -E 's/<[Ss][Tt][Yy][Ll][Ee][^>]*>[^<]*<\/[Ss][Tt][Yy][Ll][Ee]>//g' |
sed -E 's/<[^>]+>//g' |
sed 's/ / /g; s/&/\&/g; s/</</g; s/>/>/g; s/"/"/g' |
tr -s '[:space:]' '\n' |
sed '/^$/d'
}
process_content() {
local content="$1"
local resolved_mode="$2"
# Apply jq filter for JSON
if [ -n "$JQ_FILTER" ]; then
local filtered
filtered=$(echo "$content" | jq -r "$JQ_FILTER" 2>/dev/null) || {
log_warn "jq filter failed, using raw content"
echo "$content"
return
}
content="$filtered"
fi
# Apply grep selector
if [ -n "$SELECTOR" ]; then
local selected
selected=$(echo "$content" | grep -i "$SELECTOR" 2>/dev/null) || {
log_warn "Selector pattern not found, using full content"
echo "$content"
return
}
content="$selected"
fi
# Strip HTML if website mode or forced
if [ "$resolved_mode" = "website" ] || [ "$STRIP_HTML" = true ]; then
content=$(echo "$content" | strip_html_tags)
fi
echo "$content"
}
# --- Change Detection ---
calculate_change_percent() {
local old="$1"
local new="$2"
local old_len=${#old}
local new_len=${#new}
if [ "$old_len" -eq 0 ] && [ "$new_len" -eq 0 ]; then
echo "0"
return
fi
if [ "$old_len" -eq 0 ]; then
echo "100"
return
fi
# Use diff to count changed lines
local old_lines new_lines changed_lines
old_lines=$(echo "$old" | wc -l | tr -d ' ')
new_lines=$(echo "$new" | wc -l | tr -d ' ')
if [ "$old_lines" -eq 0 ]; then
old_lines=1
fi
# Count differing lines (each side separately to avoid double-counting)
local removed_count added_count
removed_count=$(diff <(echo "$old") <(echo "$new") 2>/dev/null | grep -c '^<' || true)
added_count=$(diff <(echo "$old") <(echo "$new") 2>/dev/null | grep -c '^>' || true)
if [ "$removed_count" -gt "$added_count" ]; then
changed_lines="$removed_count"
else
changed_lines="$added_count"
fi
# Percentage relative to total lines
local total_lines
if [ "$old_lines" -gt "$new_lines" ]; then
total_lines="$old_lines"
else
total_lines="$new_lines"
fi
if [ "$total_lines" -eq 0 ]; then
total_lines=1
fi
# Use awk for floating point
awk "BEGIN { printf \"%.2f\", ($changed_lines / $total_lines) * 100 }"
}
show_diff() {
local old="$1"
local new="$2"
if [ "$HAS_DIFF" = true ]; then
echo -e "${DIM}--- previous${NC}"
echo -e "${DIM}+++ current${NC}"
diff <(echo "$old") <(echo "$new") 2>/dev/null | tail -n +3 || true
else
echo -e "${YELLOW}(diff not available — install diffutils)${NC}"
fi
}
# --- Snapshot ---
save_snapshot() {
local content="$1"
local label="$2"
if [ -z "$SNAPSHOT_DIR" ]; then
return
fi
mkdir -p "$SNAPSHOT_DIR"
local timestamp
timestamp=$(date '+%Y%m%d_%H%M%S')
local filename="${SNAPSHOT_DIR}/snapshot_${timestamp}_${label}.txt"
echo "$content" > "$filename"
log_verbose "Snapshot saved: $filename"
}
# --- Progress / Status ---
print_watch_config() {
[ "$QUIET" = true ] && return
echo -e "${BOLD}Configuration:${NC}"
echo -e " ${CYAN}URL:${NC} $URL"
echo -e " ${CYAN}Method:${NC} $METHOD"
echo -e " ${CYAN}Mode:${NC} $MODE"
echo -e " ${CYAN}Interval:${NC} ${INTERVAL}s"
if [ "$THRESHOLD" != "0" ]; then
echo -e " ${CYAN}Threshold:${NC} ${THRESHOLD}%"
else
echo -e " ${CYAN}Threshold:${NC} any change"
fi
if [ ${#HEADERS[@]} -gt 0 ]; then
echo -e " ${CYAN}Headers:${NC} ${#HEADERS[@]} custom header(s)"
fi
if [ -n "$JQ_FILTER" ]; then
echo -e " ${CYAN}Filter:${NC} $JQ_FILTER"
fi
if [ -n "$SELECTOR" ]; then
echo -e " ${CYAN}Selector:${NC} $SELECTOR"
fi
if [ -n "$BODY" ]; then
echo -e " ${CYAN}Body:${NC} (${#BODY} bytes)"
fi
if [ -n "$LOG_FILE" ]; then
echo -e " ${CYAN}Log:${NC} $LOG_FILE"
fi
if [ -n "$SNAPSHOT_DIR" ]; then
echo -e " ${CYAN}Snapshots:${NC} $SNAPSHOT_DIR"
fi
if [ -n "$BASELINE_FILE" ]; then
echo -e " ${CYAN}Baseline:${NC} $BASELINE_FILE"
fi
if [ "$MAX_RUNS" -gt 0 ]; then
echo -e " ${CYAN}Max runs:${NC} $MAX_RUNS"
fi
# Notification channels
local channels=()
[ -n "$SLACK_WEBHOOK" ] && channels+=("Slack")
[ -n "$DISCORD_WEBHOOK" ] && channels+=("Discord")
[ -n "$TELEGRAM_TOKEN" ] && [ -n "$TELEGRAM_CHAT_ID" ] && channels+=("Telegram")
if [ ${#channels[@]} -gt 0 ]; then
local IFS=', '
echo -e " ${CYAN}Notify:${NC} ${channels[*]}"
fi
echo ""
}
print_countdown() {
local remaining="$1"
if [ "$QUIET" = true ]; then
sleep "$remaining"
return
fi
while [ "$remaining" -gt 0 ]; do
printf "\r${DIM} ⏳ Next check in %ds... ${NC}" "$remaining"
sleep 1
remaining=$((remaining - 1))
done
printf "\r \r"
}
# --- Cleanup ---
cleanup() {
echo ""
log_info "Stopped watching. Goodbye!"
exit 0
}
# --- Main Watch Loop ---
main() {
setup_colors
parse_args "$@"
# Re-setup colors in case --no-color was set
setup_colors
print_banner
check_dependencies
print_watch_config
# Setup cleanup trap
trap cleanup SIGINT SIGTERM
local previous_content=""
local run_count=0
local change_count=0
local first_run=true
local resolved_mode=""
local start_time
start_time=$(date +%s)
log_info "Starting watch on $URL"
log_info "Press Ctrl+C to stop"
echo ""
log_to_file "START — Watching $URL (interval=${INTERVAL}s, threshold=${THRESHOLD}%)"
while true; do
run_count=$((run_count + 1))
# Fetch
if ! fetch_url; then
if [ "$ONCE" = true ]; then
exit 1
fi
print_countdown "$INTERVAL"
continue
fi
# Detect mode on first successful response
if [ -z "$resolved_mode" ]; then
resolved_mode=$(detect_mode "$LAST_CONTENT_TYPE")
log_verbose "Resolved mode: $resolved_mode"
fi
# Process content
local current_content
current_content=$(process_content "$LAST_RESPONSE" "$resolved_mode")
if [ "$first_run" = true ]; then
first_run=false
# In --once mode, try to load previous baseline from file
if [ "$ONCE" = true ] && [ -n "$BASELINE_FILE" ] && [ -f "$BASELINE_FILE" ]; then
previous_content=$(cat "$BASELINE_FILE")
log_verbose "Loaded baseline from $BASELINE_FILE (${#previous_content} bytes)"
# Don't skip comparison — fall through to compare
else
previous_content="$current_content"
save_snapshot "$current_content" "initial"
log_success "Baseline captured (HTTP $LAST_HTTP_CODE, ${#current_content} bytes, mode: $resolved_mode)"
log_to_file "BASELINE — HTTP $LAST_HTTP_CODE, ${#current_content} bytes"
# Save baseline to file for future --once runs
if [ -n "$BASELINE_FILE" ]; then
echo "$current_content" > "$BASELINE_FILE"
log_verbose "Baseline saved to $BASELINE_FILE"
fi
if [ "$ONCE" = true ]; then
log_info "Baseline saved. Next --once run will compare against it."
exit 0
fi
print_countdown "$INTERVAL"
continue
fi
fi
# Compare
if [ "$current_content" = "$previous_content" ]; then
local ts
ts=$(date '+%H:%M:%S')
local elapsed=$(( $(date +%s) - start_time ))
local elapsed_fmt
elapsed_fmt=$(printf '%02d:%02d:%02d' $((elapsed/3600)) $((elapsed%3600/60)) $((elapsed%60)))
[ "$QUIET" = false ] && printf "\r ${DIM}[%s] Check #%d — No change (uptime: %s)${NC}\n" "$ts" "$run_count" "$elapsed_fmt"
log_verbose "No change (${#current_content} bytes)"
else
# Calculate change percentage
local change_pct
change_pct=$(calculate_change_percent "$previous_content" "$current_content")
log_verbose "Change detected: ${change_pct}% (threshold: ${THRESHOLD}%)"
# Check threshold
local exceeds_threshold
exceeds_threshold=$(awk "BEGIN { print ($change_pct >= $THRESHOLD) ? 1 : 0 }")
if [ "$exceeds_threshold" -eq 1 ]; then
change_count=$((change_count + 1))
send_notification "Web Watcher — Change Detected" \
"Change of ${change_pct}% detected on ${URL}"
log_to_file "CHANGE #${change_count} — ${change_pct}% change (HTTP $LAST_HTTP_CODE)"
# Show diff
if [ "$SHOW_DIFF" = true ]; then
show_diff "$previous_content" "$current_content"
fi
# Save snapshot
save_snapshot "$current_content" "change_${change_count}"
# Update baseline to current
previous_content="$current_content"
else
local ts
ts=$(date '+%H:%M:%S')
[ "$QUIET" = false ] && printf " ${DIM}[%s] Check #%d — Minor change (%.2f%% < %s%% threshold)${NC}\n" \
"$ts" "$run_count" "$change_pct" "$THRESHOLD"
fi
fi
# Check max runs
if [ "$MAX_RUNS" -gt 0 ] && [ "$run_count" -ge "$MAX_RUNS" ]; then
echo ""
log_info "Reached max runs ($MAX_RUNS). Stopping."
log_info "Total changes detected: $change_count"
log_to_file "STOP — Reached $MAX_RUNS runs, $change_count changes detected"
exit 0
fi
# Single run mode
if [ "$ONCE" = true ]; then
# Update baseline file with latest content
if [ -n "$BASELINE_FILE" ]; then
echo "$current_content" > "$BASELINE_FILE"
fi
if [ "$change_count" -gt 0 ]; then
exit 2 # Exit code 2 = change detected
fi
exit 0
fi
# Countdown
print_countdown "$INTERVAL"
done
}
# --- Entry Point ---
main "$@"