Skip to content

Instantly share code, notes, and snippets.

@wbadart
Created February 4, 2021 19:14
Show Gist options
  • Save wbadart/8798fab9ae6a31a7a6c377487305401e to your computer and use it in GitHub Desktop.
Save wbadart/8798fab9ae6a31a7a6c377487305401e to your computer and use it in GitHub Desktop.
#!/bin/sh
security_endpoint="query?function=TIME_SERIES_DAILY&symbol=%s&apikey=$ALPHAVANTAGE_API_KEY"
security_jsonpath='.["Time Series (Daily)"] as $res
| ($res | keys | last) as $latest
| $res[$latest] | .["4. close"]'
crypto_endpoint="query?function=CURRENCY_EXCHANGE_RATE&from_currency=%s&to_currency=USD&apikey=$ALPHAVANTAGE_API_KEY"
crypto_jsonpath='.["Realtime Currency Exchange Rate"]
| .["8. Bid Price"]'
print_entry() {
printf "P $(date +%Y/%m/%d) $1 USD "
curl -LSs "https://www.alphavantage.co/$(printf "$2" "$1")" | jq -cr "$3"
sleep 13 # 5 requests per minute, with a little buffer
}
echo "; Prices collected $(date +'%Y-%m-%d %H:%M:%S')"
hledger commodities \
| grep -Ev "($STOCKS_EXCLUDE)" \
| while read -r symbol; do
print_entry "$symbol" "$security_endpoint" "$security_jsonpath"
done
print_entry BTC "$crypto_endpoint" "$crypto_jsonpath"
print_entry ETH "$crypto_endpoint" "$crypto_jsonpath"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment