-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.sh
More file actions
executable file
·141 lines (123 loc) · 4.22 KB
/
monitor.sh
File metadata and controls
executable file
·141 lines (123 loc) · 4.22 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
#!/usr/bin/env bash
set -euo pipefail
# --- Config ---
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" # absolute path of script dir
REPO_DIR="$SCRIPT_DIR/binance-spot-api-docs"
LOG_FILE="$SCRIPT_DIR/changes.diff" # log file always relative to script dir
# MONITOR_BINANCE_SPOT_REST_DOCS_DISCORD_WEBHOOK="https://discord.com/api/webhooks/XXXX/XXXX"
# --- Helper functions ---
log() {
echo "$1" | tee -a "$LOG_FILE"
}
log_separator() {
log "------------------------------------------------------------"
}
check_cmd() {
command -v "$1" >/dev/null 2>&1 || {
echo "❌ Dependency '$1' is missing. Please install it before running this script."
exit 1
}
}
# --- Help ---
show_help() {
cat <<EOF
Usage: ./monitor.sh [options]
Options:
--check-deps Check that all required dependencies are installed and exit
--test-log Write a test entry to the log file
--test-discord Send a test message to Discord (if webhook is set)
-h, --help Show this help message and exit
Environment:
MONITOR_BINANCE_SPOT_REST_DOCS_DISCORD_WEBHOOK Discord webhook URL (optional).
If not set, only local logging is used.
Description:
This script monitors the Binance Spot REST API docs (rest-api.md) for changes.
- Logs changes into $LOG_FILE
- Optionally sends changes to Discord if a webhook is configured
EOF
}
# --- Check dependencies ---
check_deps() {
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
log "Checking dependencies..."
check_cmd git
check_cmd bash
if [ -n "${MONITOR_BINANCE_SPOT_REST_DOCS_DISCORD_WEBHOOK:-}" ]; then
check_cmd curl
check_cmd python3
fi
log "$TIMESTAMP 🔵 All required dependencies are installed."
}
# --- Argument handling ---
case "${1:-}" in
-h | --help)
show_help
exit 0
;;
--check-deps)
check_deps
log_separator
exit 0
;;
--test-log)
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
log "$TIMESTAMP 🔵 Log test message from monitor script"
log_separator
exit 0
;;
--test-discord)
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
TEST_MSG="$TIMESTAMP 🔵 Discord test message from monitor script"
if [ -n "${MONITOR_BINANCE_SPOT_REST_DOCS_DISCORD_WEBHOOK:-}" ]; then
ESCAPED_MSG=$(printf '%s' "$TEST_MSG" | python3 -c 'import json,sys; print(json.dumps(sys.stdin.read()))')
curl -s -H "Content-Type: application/json" \
-X POST \
-d "{\"content\": $ESCAPED_MSG}" \
"$MONITOR_BINANCE_SPOT_REST_DOCS_DISCORD_WEBHOOK" &>/dev/null
log "$TIMESTAMP 🔵 Discord test sent successfully"
else
log "$TIMESTAMP 🟡 Discord webhook not set, skipping test"
fi
log_separator
exit 0
;;
esac
# --- Clone repo if needed ---
if [ ! -d "$REPO_DIR" ]; then
git clone https://github.com/binance/binance-spot-api-docs.git "$REPO_DIR"
fi
cd "$REPO_DIR"
# --- Fetch latest remote changes ---
git fetch origin
# --- Timestamp ---
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
# --- Check for changes ---
if git diff --quiet HEAD..origin/master -- rest-api.md; then
log "$TIMESTAMP 🟢 No changes in rest-api.md"
else
HEADER="$TIMESTAMP 🔴 Changes detected in rest-api.md"
log "$HEADER"
# Save diff to log
DIFF=$(git diff HEAD..origin/master -- rest-api.md)
echo "$DIFF" >>"$LOG_FILE"
# --- Optional: truncate large diffs for Discord ---
MAX_LENGTH=1800
TRUNCATED_DIFF="$DIFF"
if [ ${#DIFF} -gt $MAX_LENGTH ]; then
TRUNCATED_DIFF="${DIFF:0:$MAX_LENGTH}\n... (truncated for Discord)"
fi
# --- Send Discord notification if webhook is set ---
if [ -n "${MONITOR_BINANCE_SPOT_REST_DOCS_DISCORD_WEBHOOK:-}" ]; then
PAYLOAD="$HEADER\n\`\`\`\n$TRUNCATED_DIFF\n\`\`\`"
ESCAPED_PAYLOAD=$(printf '%s' "$PAYLOAD" | python3 -c 'import json,sys; print(json.dumps(sys.stdin.read()))')
curl -s -H "Content-Type: application/json" \
-X POST \
-d "{\"content\": $ESCAPED_PAYLOAD}" \
"$MONITOR_BINANCE_SPOT_REST_DOCS_DISCORD_WEBHOOK" &>/dev/null
else
log "$TIMESTAMP 🟡 Discord webhook not set, skipping notification"
fi
fi
log_separator
# --- Update local file to match remote ---
git merge --ff-only origin/master