#!/bin/bash # hledger-prices # # Retrieve and update hledger prices file # Check bash version # https://askubuntu.com/a/916978/533341 if [ ! "${BASH_VERSINFO:-0}" -ge 4 ]; then echo "Bash version 4 or higher required!" echo "Your version: ${BASH_VERSION}" exit 1 fi # Import config.env if [ ! -f "config.env" ]; then echo "config.env not found!" exit 2 fi; . config.env if [ "${PRICES_FILE}" == "" ]; then echo "PRICES_FILE is not defined in config.env" exit 3 elif [ "${POLYGON_API_KEY}" == "" ]; then echo "POLYGON_API_KEY is not defined in config.env" exit 3 fi # Create prices file if it doesn't exist if [ ! -f "${PRICES_FILE}" ]; then mkdir -p "$(dirname PRICES_FILE)" 2>/dev/null touch "$PRICES_FILE" fi # Do the thing for asset in ${!assets[@]}; do echo "Checking $asset..." url="https://api.polygon.io/v2/aggs/ticker/${assets[${asset}]}/prev?apiKey=${POLYGON_API_KEY}" #echo " URL: $url" res=$(curl --silent "$url") # Check the result if [ "$(echo $res | jq -r '.status')" == "ERROR" ]; then echo " $(echo $res | jq -r '.error')" elif [ "$(echo $res | jq -r '.resultsCount')" == "0" ]; then echo " No results found!" elif [ "$(echo $res | jq -r '.results[0].vw')" == "null" ]; then echo " Result was null!" # Looks good; add it else price="\$$(echo $res | jq '.results[0].vw')" echo " Price: $price" line="P $(date -I --date=yesterday) $asset" grep "$line" "$PRICES_FILE" >/dev/null \ || echo "$line $price" >> "$PRICES_FILE" sleep 10 # Optional, check API limits fi done