-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_and_push.sh
More file actions
executable file
·63 lines (52 loc) · 1.58 KB
/
Copy pathfetch_and_push.sh
File metadata and controls
executable file
·63 lines (52 loc) · 1.58 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
#!/bin/bash
# NSE Data Fetcher — runs every 5 min via launchd
# Fetches NSE option chain data and pushes to GitHub
cd /Users/ravivijnan/nse-data-fetcher
LOGFILE="fetch.log"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOGFILE"
}
# Check if market is open (9:15 AM - 3:30 PM IST, Mon-Fri)
IST_HOUR=$(python3 -c "
from datetime import datetime, timezone, timedelta
ist = timezone(timedelta(hours=5, minutes=30))
now = datetime.now(ist)
print(now.hour)
")
IST_MIN=$(python3 -c "
from datetime import datetime, timezone, timedelta
ist = timezone(timedelta(hours=5, minutes=30))
now = datetime.now(ist)
print(now.minute)
")
DOW=$(python3 -c "
from datetime import datetime, timezone, timedelta
ist = timezone(timedelta(hours=5, minutes=30))
now = datetime.now(ist)
print(now.weekday())
")
IST_TOTAL=$((IST_HOUR * 60 + IST_MIN))
if [ "$DOW" -ge 5 ]; then
exit 0
fi
if [ "$IST_TOTAL" -lt 555 ] || [ "$IST_TOTAL" -gt 930 ]; then
exit 0
fi
log "Starting fetch..."
python3 nse.py >> "$LOGFILE" 2>&1
if [ -f docs/nse_data.json ]; then
STOCKS=$(python3 -c "import json; d=json.load(open('docs/nse_data.json')); print(d.get('count',0))")
log "Fetched $STOCKS stocks"
if [ "$STOCKS" -gt "0" ]; then
git add docs/nse_data.json
git diff --staged --quiet || {
git commit -m "Update data $(date '+%Y-%m-%d %H:%M')"
git push origin main >> "$LOGFILE" 2>&1
log "Pushed to GitHub"
}
fi
fi
# Keep log file small (last 500 lines)
if [ -f "$LOGFILE" ]; then
tail -500 "$LOGFILE" > "$LOGFILE.tmp" && mv "$LOGFILE.tmp" "$LOGFILE"
fi