Skip to content

Commit 29cfd8b

Browse files
author
8go
authored
various updates
- checks for API status codes, - enables crypto and metals as currencies - more error handling - version number updated
1 parent 5d21eab commit 29cfd8b

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

coinbash.sh

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ VERSION="false" # for --version argument;
180180
DOONLYCLEANUP="false" # for --cleanup argument;
181181
# shellcheck disable=SC2034
182182
COUNTER=0 # this is a counter of arguments
183-
VERSIONDESC="2017-NOV-27"
183+
VERSIONDESC="2020-OCT-03"
184184
MYAPP=jq # this package is required, a json parser
185185
MYAPP2=curl # this package is required
186186
# https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?start=1&convert=USD&limit=10
@@ -196,7 +196,7 @@ TORIFY="false"
196196
TOPDEFAULT=10 # default
197197
TOP=$TOPDEFAULT # init
198198
FIAT="USD" # default fiat currency
199-
FIATARRAY=(AUD BRL CAD CHF CLP CNY CZK DKK EUR GBP HKD HUF IDR ILS INR JPY KRW MXN MYR NOK NZD PHP PKR PLN RUB SEK SGD THB TRY TWD USD ZAR)
199+
FIATARRAY=(BTC ETH USDT XRP BCH BNB DOT LINK CRO BSV LTC XAU XAG XPT XPD AUD BRL CAD CHF CLP CNY CZK DKK EUR GBP HKD HUF IDR ILS INR JPY KRW MXN MYR NOK NZD PHP PKR PLN RUB SEK SGD THB TRY TWD USD ZAR)
200200
EUR="false"
201201
CCSYMBOLSARRAY=()
202202
CCSYMBOLSLIST="" # string of crypt currencies seperated by comma (,) without spaces ( ). E.g. btc,eth,ltc
@@ -226,7 +226,10 @@ function usage() {
226226
echo "${0##*/}: If necessary it installs packages $MYAPP and $MYAPP2."
227227
echo "${0##*/}: Inspiration and basic idea from ${bold}https://github.com/bichenkk/coinmon${reset}"
228228
echo "${0##*/}: Real-time market data from ${bold}https://www.coinmarketcap.com${reset}"
229-
echo "${0##*/}: The default currency is USD and it supports AUD, BRL, CAD, CHF, CLP, CNY, CZK, "
229+
echo "${0##*/}: The default currency is USD and it supports "
230+
echo "${0##*/}: BTC, ETH, USDT, XRP, BCH, BNB, DOT, LINK, CRO, BSV, LTC, "
231+
echo "${0##*/}: XAU, XAG, XPT, XPD, "
232+
echo "${0##*/}: AUD, BRL, CAD, CHF, CLP, CNY, CZK, "
230233
echo "${0##*/}: DKK, EUR, GBP, HKD, HUF, IDR, ILS, INR, JPY, KRW, MXN, MYR, NOK, NZD, "
231234
echo "${0##*/}: PHP, PKR, PLN, RUB, SEK, SGD, THB, TRY, TWD, ZAR."
232235
echo "${0##*/}: ${0##*/} uses a temporary file $JSONFILE which gets automatically removed."
@@ -239,6 +242,8 @@ function usage() {
239242
echo "${0##*/}: Example: ${0##*/} -t -n 7 -e ... uses Tor, prints market info of top 7 crypto currencies, "
240243
echo "${0##*/}: uses EUR for prices"
241244
echo "${0##*/}: Example: ${0##*/} -e ... shortcut for -f EUR, uses Euro for prices"
245+
echo "${0##*/}: Example: ${0##*/} -f BTC ... gives prices in Bitcoin (BTC)"
246+
echo "${0##*/}: Example: ${0##*/} -f XAU ... gives prices in Gold Troy ounces (XAU)"
242247
echo "${0##*/}: Example: ${0##*/} -f AUD ... gives prices in Australian Dollars (AUD)"
243248
echo "${0##*/}: Example: ${0##*/} -l btc ... lists only BTC"
244249
echo "${0##*/}: Example: ${0##*/} -l btc,eth,ltc ... lists BTC, ETH and LTC "
@@ -262,6 +267,7 @@ function usage() {
262267
echo "${0##*/}: DO-ONLY-CLEANUP: Performs only cleanup, then exits [type: flag]"
263268
echo "${0##*/}: ${bold}--torify, -t${reset}"
264269
echo "${0##*/}: TORIFY: Request the data via Tor onion network [type: flag]"
270+
echo "${0##*/}: Was disabled in latest Coinmarketcap.com API."
265271
echo "${0##*/}: ${bold}--top, -n${reset}"
266272
echo "${0##*/}: TOP: How many crypto currencies should be displayed [type: integer] [default: $TOP]"
267273
echo "${0##*/}: ${bold}--fiat, -f${reset}"
@@ -364,10 +370,10 @@ function printHeader() {
364370
max_supply="Max-Supply"
365371
percent_change_1h="1h-Change"
366372
# dont put anything like ${bold} ${reset} here, it confuses `column` later
367-
printf "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%17s\t%17s\t%17s\t%17s\t%17s\n" "$rank" "$symbol" "$name" "$price" "$price_btc" "$percent_change_1h" "$percent_change_24h" "$percent_change_7d" "$a24h_volume" "$available_supply" "$total_supply" "$max_supply" "$market_cap"
373+
printf "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%17s\t%17s\t%17s\t%17s\t%17s\n" "$rank" "$symbol" "$name" "$price" "$percent_change_1h" "$percent_change_24h" "$percent_change_7d" "$a24h_volume" "$available_supply" "$total_supply" "$max_supply" "$market_cap"
368374
else
369375
# dont put anything like ${bold} ${reset} here, it confuses `column` later
370-
printf "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%17s\n" "$rank" "$symbol" "$name" "$price" "$price_btc" "$percent_change_24h" "$percent_change_7d" "$market_cap"
376+
printf "%s\t%s\t%s\t%s\t%s\t%s\t%17s\n" "$rank" "$symbol" "$name" "$price" "$percent_change_24h" "$percent_change_7d" "$market_cap"
371377
fi
372378
}
373379

@@ -454,7 +460,8 @@ function processEntry() {
454460
#price="${price%\"}"
455461
#price="${price#\"}"
456462
[ $seperator == "," ] && price=${price//./,} # replace all dots
457-
price_btc="---" # no longer offered in same API call
463+
# price_btc=$(echo "$1" | jq ".quote.BTC.price") ==> Free API key does NOT support more than 1 currency!
464+
price_btc="---"
458465
#price_btc="${price_btc%\"}"
459466
#price_btc="${price_btc#\"}"
460467
[ $seperator == "," ] && price_btc=${price_btc//./,} # replace all dots
@@ -495,9 +502,9 @@ function processEntry() {
495502
#percent_change_1h="${percent_change_1h%\"}"
496503
#percent_change_1h="${percent_change_1h#\"}"
497504
[ $seperator == "," ] && percent_change_1h=${percent_change_1h//./,} # replace all dots
498-
printf "%d\t%s\t%s\t%'7.2f\t%3s\t%+6.1f%%\t%+6.1f%%\t%+6.1f%%\t%'17.f\t%'17.f\t%'17.f\t%'17.f\t%'17.f\n" "$rank" "$symbol" "$name" "$price" "$price_btc" "$percent_change_1h" "$percent_change_24h" "$percent_change_7d" "$a24h_volume" "$available_supply" "$total_supply" "$max_supply" "$market_cap" 2>/dev/null
505+
printf "%d\t%s\t%s\t%'1.8f\t%+6.1f%%\t%+6.1f%%\t%+6.1f%%\t%'17.f\t%'17.f\t%'17.f\t%'17.f\t%'17.f\n" "$rank" "$symbol" "$name" "$price" "$percent_change_1h" "$percent_change_24h" "$percent_change_7d" "$a24h_volume" "$available_supply" "$total_supply" "$max_supply" "$market_cap" 2>/dev/null
499506
else
500-
printf "%d\t%s\t%s\t%'7.2f\t%3s\t%+6.1f%%\t%+6.1f%%\t%'17.f\n" "$rank" "$symbol" "$name" "$price" "$price_btc" "$percent_change_24h" "$percent_change_7d" "$market_cap" 2>/dev/null
507+
printf "%d\t%s\t%s\t%'1.8f\t%+6.1f%%\t%+6.1f%%\t%'17.f\n" "$rank" "$symbol" "$name" "$price" "$percent_change_24h" "$percent_change_7d" "$market_cap" 2>/dev/null
501508
fi
502509

503510
if [ "$useccsymbolslist" == "true" ]; then
@@ -512,7 +519,8 @@ function processEntry() {
512519

513520
function main() {
514521
if [ "$COINMARKETCAP_API_KEY" == "" ]; then
515-
echo "${0##*/}: ${red}${bold}Coinmarketcap-API-key missing.${reset} Global environment variable \"COINMARKETCAP_API_KEY\" is not set. Go to https://coinmarketcap.com and get your personal API key. Then assign it to environment variable \"COINMARKETCAP_API_KEY\"."
522+
echo "${0##*/}: ${red}${bold}Coinmarketcap-API-key missing.${reset} Global environment variable \"COINMARKETCAP_API_KEY\" is not set."
523+
echo "${0##*/}: Go to https://coinmarketcap.com and get your personal API key. Then assign it to environment variable \"COINMARKETCAP_API_KEY\"."
516524
exit 13
517525
fi
518526
# process arguments
@@ -535,6 +543,9 @@ function main() {
535543
;;
536544
torify | t | tor | -torify | -t | -tor | --torify | --t | --tor)
537545
TORIFY="true"
546+
echo "${0##*/}: ${red}${bold}This feature has been disabled by the API. It requires a captcha. Sorry.${reset}"
547+
echo "${0##*/}: ${red}${bold}TOR use no longer allowed in the new API version.${reset}"
548+
exit 33
538549
;;
539550
top | n | number | -top | -n | -number | --top | --n | --number)
540551
if ! [[ "$2" =~ ^[0-9]+$ ]]; then
@@ -623,6 +634,7 @@ function main() {
623634
# shellcheck disable=SC2034
624635
FIATLC=${FIAT,,} # lowercase
625636
fi
637+
# I tried this: `CONVERT="?convert=BTC,${FIATUC}"` but it gave error `Free version does not support more than 1 currency`.
626638
CONVERT="?convert=${FIATUC}"
627639
[ "$DEBUG" == "true" ] && echo "${0##*/}: ${green}${bold}Converting to fiat $FIATUC.${reset}"
628640

@@ -715,6 +727,18 @@ function main() {
715727
for name in "${CCNAMESARRAY[@]}"; do
716728
[ "$DEBUG" == "true" ] && echo "${0##*/}: DEBUG: $TORIFYCMD curl -H \"X-CMC_PRO_API_KEY: $COINMARKETCAP_API_KEY\" -H \"Accept: application/json\" -s \"${DATAURL}quotes/latest${CONVERT}&slug=${name}\" > \"${JSONFILE}.part\""
717729
$TORIFYCMD curl -H "X-CMC_PRO_API_KEY: $COINMARKETCAP_API_KEY" -H "Accept: application/json" -s "${DATAURL}quotes/latest${CONVERT}&slug=${name}" >"${JSONFILE}.part"
730+
errorstatus=$(cat "${JSONFILE}.part" | jq "[.status.error_code][]")
731+
if [ "$errorstatus" != "0" ]; then
732+
echo "${0##*/}: ${red}Error: The https://coinmarketcap.com/ API returned error code \"$errorstatus\". Aborting.${reset}"
733+
echo "${0##*/}: 400 400 Bad Request"
734+
echo "${0##*/}: 1002 401 Unauthorized"
735+
echo "${0##*/}: 1006 403 Forbidden"
736+
echo "${0##*/}: 1008 429 Too Many Requests"
737+
echo "${0##*/}: 500 500 Internal Server Error "
738+
echo "${0##*/}: Timestamp: $(cat "${JSONFILE}.part" | jq "[.status.timestamp][]")"
739+
echo "${0##*/}: Message: $(cat "${JSONFILE}.part" | jq "[.status.error_message][]")"
740+
exit "$ret"
741+
fi
718742
# shellcheck disable=SC2046
719743
if [ $(grep -c "id not found" "${JSONFILE}.part") -eq 1 ]; then
720744
echo "${0##*/}: ${yellow}WARNING: No crypto currency with name \"$name\" was not found.${reset} Skipping it."
@@ -736,6 +760,18 @@ function main() {
736760
LIMIT="&limit=${TOP}"
737761
[ "$DEBUG" == "true" ] && echo "${0##*/}: DEBUG: $TORIFYCMD curl -H \"X-CMC_PRO_API_KEY: $COINMARKETCAP_API_KEY\" -H \"Accept: application/json\" s \"${DATAURL}listings/latest${CONVERT}${LIMIT}&start=1\" > \"${JSONFILE}\""
738762
$TORIFYCMD curl -H "X-CMC_PRO_API_KEY: $COINMARKETCAP_API_KEY" -H "Accept: application/json" -s "${DATAURL}listings/latest${CONVERT}${LIMIT}&start=1" >"${JSONFILE}"
763+
errorstatus=$(cat "${JSONFILE}" | jq "[.status.error_code][]")
764+
if [ "$errorstatus" != "0" ]; then
765+
echo "${0##*/}: ${red}Error: The https://coinmarketcap.com/ API returned error code \"$errorstatus\". Aborting.${reset}"
766+
echo "${0##*/}: 400 400 Bad Request"
767+
echo "${0##*/}: 1002 401 Unauthorized"
768+
echo "${0##*/}: 1006 403 Forbidden"
769+
echo "${0##*/}: 1008 429 Too Many Requests"
770+
echo "${0##*/}: 500 500 Internal Server Error "
771+
echo "${0##*/}: Timestamp: $(cat "${JSONFILE}" | jq "[.status.timestamp][]")"
772+
echo "${0##*/}: Message: $(cat "${JSONFILE}" | jq "[.status.error_message][]")"
773+
exit "$ret"
774+
fi
739775
fi
740776
setSeperator
741777
ii=0

0 commit comments

Comments
 (0)