scripts

Scripts
git clone git://git.wimdupont.com/scripts.git
Log | Files | Refs | LICENSE

coin (1351B)


      1 #!/bin/bash
      2 # requires jq (https://github.com/stedolan/jq)
      3 
      4 while getopts ":hf:" arg; do
      5   case $arg in
      6     f) dataFilter=($OPTARG);;
      7     h) echo "
      8 	CoinGecko API Client
      9 
     10 	-f {v}	:	{v} filter result data
     11 	-h 	:	for this help menu 
     12 	
     13 	additional arguments can be provided as names of cryptocurrencies, these will
     14 	be used as API parameters
     15 
     16 	EXAMPLES:
     17 	$ coin monero bitcoin
     18 	$ coin -f 'name current_price price_change_24h' monero
     19 	$ coin -h
     20 	"
     21 	exit 0;;
     22   esac
     23 done
     24 
     25 shift $((OPTIND-1))
     26 
     27 # function to join arguments with the first one being the separator
     28 
     29 join_by_string() {
     30   local separator="$1"
     31   shift
     32   local first="$1"
     33   shift
     34   printf "%s" "$first" "${@/#/$separator}"
     35 }
     36 
     37 # turns script arguments into api currency params if not empty
     38 
     39 if [[ ! -z "${@// }" ]] ; then
     40 	currencyParam=$(join_by_string ',' "$@")
     41 fi
     42 
     43 # grab data from coinggecko api
     44 
     45 data=$(curl -X 'GET' \
     46   'https://api.coingecko.com/api/v3/coins/markets?vs_currency=eur&ids='${currencyParam}'&order=market_cap_desc&per_page=100&page=1&sparkline=false' \
     47   -H 'accept: application/json')
     48 
     49 # format data with jq
     50 # filters data with jq if not empty by removing array, prepending datafilter elements with
     51 # '.', and joining them with ', ' as separator
     52 
     53 if [ -z "$dataFilter" ]; then
     54 	echo ${data} | jq 
     55 else
     56 	echo ${data} | jq ".[] | $(join_by_string ', ' ${dataFilter[@]/#/.})"
     57 fi