Last active
December 28, 2022 03:16
-
-
Save falsycat/cc7150ea3a3e5cc6fc4e7ea2bb923c00 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ---- record manipulation | |
function normalize() { | |
cat - | \ | |
sed -nE -e '/^#/b' -e '/^\s/{H;$!b}' -e 'x; /^$/!{s/\s+/ /g;p}' | \ | |
awk -e '{for (i=3;i<NF;i+=2) print $1, $2, $i, $(i+1)}' | |
} | |
function calc_wallet_sums() { | |
cat - | \ | |
awk -e '{sum_of[$3] += $4}' -e 'END { for (w in sum_of) print w, sum_of[w] }' | |
} | |
function calc_currency_sums() { | |
cat - | \ | |
awk -e '{split($3,a,"@");sum_of[a[1]] += $4}' -e 'END { for (c in sum_of) print c, sum_of[c] }' | |
} | |
function calc_category_sums() { | |
cat - | \ | |
awk -e '{split($2,a,"/");split($3,b,"@");sum_of[b[1]"@"a[1]] += $4}' -e 'END { for (c in sum_of) print c, sum_of[c] }' | |
} | |
# ---- table manipulation | |
function make_total_table() { | |
cat - | \ | |
awk -e "BEGIN{$(make_price_awk_var)}" \ | |
-e '{split($1,a,"@"); names[NR]=$1; sum[NR]=$2; sum_jpy[NR]=$2*price_of[a[1]]; total+=sum_jpy[NR];}' \ | |
-e 'END{printf "%-24s %16s %16s %8s\n", "__NAME__", "AMOUNT", "VOLjpy", "VOL%"}' \ | |
-e 'END{for(i=1;i<=NR;++i)if(sum[i]!=0)printf "%-24s %16.03f %16.03f %8.03f\n", names[i], sum[i], sum_jpy[i], total!=0?sum_jpy[i]/total*100.:0}' \ | |
-e 'END{printf "%-24s %16s %16.03f %8s\n", "__TOTAL__", "--", total, "--"}' | |
} | |
# ---- fetching currency price | |
function fetch_usd_price() { | |
curl -sL "https://open.er-api.com/v6/latest/USD" | \ | |
sed -E -e 's/.*"JPY":([^,]+),.*/\1/' | |
} | |
function fetch_crypto_prices() { | |
curl -sL "https://api1.binance.com/api/v3/ticker/price" | \ | |
sed -E -e 's/^\[\{//' -e 's/\}\]$//' -e 's/\},\{/\n/g' | \ | |
sed -E -e 's/^"symbol":"(.*)","price":"(.*)"$/\1 \2/' \ | |
-e '/BUSD /!d' -e 's/^(\S+)BUSD /\1 /' | \ | |
awk -v "usd=$1" -e '{print tolower($1), $2*usd}' | |
} | |
function fetch_prices() { | |
usd=$(fetch_usd_price) | |
echo "jpy 1" | |
echo "usd $usd" | |
fetch_crypto_prices $usd | |
} | |
function make_price_awk_var() { | |
for n in ${!price_of[@]}; do | |
echo "price_of[\"$n\"]=${price_of[$n]};" | |
done | |
} | |
# ---- variable init | |
declare -A price_of | |
eval "price_of=($(fetch_prices | awk -e '{print "[\"" $1 "\"]=" $2}'))" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment