commit e4be295cde904d3b8616d1bc31a921d1cf619eee
parent a4a1493a43d4e31e14f8033b89b1917080918aa1
Author: Wim Dupont <wim@wimdupont.com>
Date: Fri, 8 Apr 2022 21:41:42 +0200
added coingecko api script
Diffstat:
A | bin/coin | | | 56 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 56 insertions(+), 0 deletions(-)
diff --git a/bin/coin b/bin/coin
@@ -0,0 +1,56 @@
+#!/bin/bash
+# requires jq (https://github.com/stedolan/jq)
+
+while getopts ":hf:" arg; do
+ case $arg in
+ f) dataFilter=($OPTARG);;
+ h) echo "
+ CoinGecko API Client
+
+ -f {v} : {v} filter result data
+ -h : for this help menu
+
+ additional arguments can be provided as names of cryptocurrencies, these will
+ be used as API parameters
+
+ EXAMPLES:
+ $ coin monero bitcoin
+ $ coin -f 'name current_price price_change_24h' monero
+ $ coin -h
+ "
+ exit 1;;
+ esac
+done
+
+shift $((OPTIND-1))
+currencies=$@
+
+# function to join arguments with the first one being the separator
+
+joinByString() {
+ local separator="$1"
+ shift
+ local first="$1"
+ shift
+ printf "%s" "$first" "${@/#/$separator}"
+}
+
+# turns script arguments into api currency params
+
+currencyParam=$(joinByString '%2C' "${currencies}")
+
+# grab data from coinggecko api
+
+data=$(curl -X 'GET' \
+ 'https://api.coingecko.com/api/v3/coins/markets?vs_currency=eur&ids='${currencyParam}'&order=market_cap_desc&per_page=100&page=1&sparkline=false' \
+ -H 'accept: application/json')
+
+# format data with jq
+# filters data with jq if not empty by removing array, prepending datafilter elements with
+# '.', and joining them with ', ' as separator
+
+if [ -z "$dataFilter" ]; then
+ echo ${data} | jq
+else
+ echo ${data} | jq ".[] | $(joinByString ', ' ${dataFilter[@]/#/.})"
+fi