-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbtc_sample.sh
More file actions
executable file
·23 lines (19 loc) · 840 Bytes
/
btc_sample.sh
File metadata and controls
executable file
·23 lines (19 loc) · 840 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env bash
set -euo pipefail
WORKDIR="${WORKDIR:-/home/bot/Workspace}"
STATE_DIR="${STATE_DIR:-$WORKDIR/state}"
OUT="$STATE_DIR/btc-prices.jsonl"
mkdir -p "$STATE_DIR"
resp="$(curl -fsSL --retry 6 --retry-all-errors --retry-delay 3 -H 'User-Agent: openclaw-btc-tracker/1.0' "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd,cad")"
printf '%s' "$resp" | python -c 'import sys, json, time
out_path = sys.argv[1]
obj = json.loads(sys.stdin.read())
btc = obj.get("bitcoin") or {}
btc_usd = float(btc["usd"])
btc_cad = float(btc["cad"])
usd_cad = btc_cad / btc_usd
rec = {"ts": int(time.time()), "btc_usd": btc_usd, "btc_cad": btc_cad, "usd_cad": usd_cad}
with open(out_path, "a", encoding="utf-8") as f:
f.write(json.dumps(rec, sort_keys=True) + "\n")
print(json.dumps(rec, sort_keys=True))
' "$OUT"