From 38c3ebf3837c104874d6f5510a31546336f70037 Mon Sep 17 00:00:00 2001 From: Keith Irwin Date: Sat, 7 May 2022 10:17:06 -0600 Subject: [PATCH] Initial commit, created get-prices --- .gitignore | 1 + LICENSE.md | 21 +++++++++++++++++ README.md | 39 ++++++++++++++++++++++++++++++ example.config.env | 16 +++++++++++++ get-prices | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 136 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 example.config.env create mode 100755 get-prices diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2549b3d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.env diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..dfa7edf --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +# MIT License + +**Copyright (c) 2022 Keith Irwin** + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..f326bb0 --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# hledger scripts + +Some scripts for hledger + +## get-prices + +Gets prices from Polygon.io api, and hopefully some alternatives in the future. Run this as a timer or cronjob to keep your prices file up-to-date with your assets. + +Create a config file: + +```sh +$ cp example.config.env config.env +``` + +Edit the config file, then run `./get-prices` regularly + +## License (MIT) + +**Copyright (c) 2022 Keith Irwin** + +``` +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +``` diff --git a/example.config.env b/example.config.env new file mode 100644 index 0000000..91e8e8c --- /dev/null +++ b/example.config.env @@ -0,0 +1,16 @@ +# Paths +PRICES_FILE="~/hledger/$(date +%Y).prices" + +# API KEYS +POLYGON_API_KEY='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' + +# Assets +declare -A assets; assets+=( + # Key is the name that goes in the journal + # Value is the ticker searched in the API + ["BTC"]="X:BTCUSD" + ["ETH"]="X:ETHUSD" + ["XMR"]="X:XMRUSD" + ["MKR"]="X:MKRUSD" + ["AMD"]="AMD" +) diff --git a/get-prices b/get-prices new file mode 100755 index 0000000..986879b --- /dev/null +++ b/get-prices @@ -0,0 +1,59 @@ +#!/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" \ + || echo "$line \$$price" >> "$PRICES_FILE" + sleep 10 # Optional, check API limits + fi + +done