-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoco
executable file
·140 lines (114 loc) · 3.45 KB
/
moco
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env bash
# syntax: moco <amount> <from> {<to>}
MOCO_DIR=/home/besnn/_git/moco-cli
GREEN='\033[1;32m'
RED='\033[1;31m'
YELLOW='\033[1;33m'
UNDERLINE='\033[4m'
RESET_COLOR='\033[0m'
declare -r API_KEY='e29d7cafb2376f82e1bb' # needed for api validation
declare amount
declare from_currency
declare -a to_currencies
declare -a currencies=(aed afn all amd ang aoa ars aud awg azn bam bbd bdt bgn bhd bif
bmd bnd bob brl bsd btc btn bwp byn byr bzd cad cdf chf clf clp cny cop crc cuc
cup cve czk djf dkk dop dzd egp ern etb eur fjd fkp gbp gel ggp ghs gip gmd gnf
gtq gyd hkd hnl hrk htg huf idr ils imp inr iqd irr isk jep jmd jod jpy kes kgs
khr kmf kpw krw kwd kyd kzt lak lbp lkr lrd lsl lvl lyd mad mdl mga mkd mmk mnt
mop mro mur mvr mwk mxn myr mzn nad ngn nio nok npr nzd omr pab pen pgk php pkr
pln pyg qar ron rsd rub rwf sar sbd scr sdg sek sgd shp sll sos srd std svc syp
szl thb tjs tmt tnd top try ttd twd tzs uah ugx usd uyu uzs vef vnd vuv wst xaf
xag xcd xdr xof xpf yer zar zmk zmw zwl) # 166 currencies
case $1 in
"help"|"h"|"-h"|"--help"|"list"|"l"|"-l"|"--list")
if [ -f $MOCO_DIR/utils/.help ] || [ -f .help ]; then
echo -e $RED"Please, try one of the valid arguments below\n"$RESET_COLOR
< utils/.help tr ' ' ' ' | pr -t -1
fi
exit 1;;
"give"|"find")
if [[ $2 ]]; then
grep -iE "$2" $MOCO_DIR/utils/.help || (echo -e $RED"Couldn't find currency. Sorry.\n"$RESET_COLOR 1>&2 && exit 1)
echo
exit 0
fi
echo -e $RED"Try: moco find <currency>"$RESET_COLOR 1>&2
echo
exit 1
esac
if ! [[ $1 =~ ^[[:digit:]].?+$ ]]; then
echo -e $RED"[AMOUNT] not valid" 1>&2
echo -e "Has to be decimal" 1>&2
echo -e "\n"$RESET_COLOR
exit 1
fi
# check if parameters are inputted
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
echo -e $RED"Usage: <alias> [AMOUNT] [FROM_CURRENCY] {TO_CURRENCY}"$RESET_COLOR 1>&2
echo -e "\n" 1>&2
exit 1
fi
amount=$1
from_currency=$2
shift; shift
to_currencies=($@)
# check if arguments are valid
found_from=false
declare -i count=0
for i in ${currencies[@]}
do
if [ "$from_currency" = "$i" ]; then
found_from=true
fi
done
if [ "$found_from" = false ]; then
echo -e $RED"[FROM_CURRENCY] not valid" 1>&2
echo -e "type <alias> help for valid currencies" 1>&2
echo -e "\n"$RESET_COLOR
exit 1
fi
for i in ${to_currencies[@]}
do
for j in ${currencies[@]}
do
to_found=false
if [ "$i" = "$j" ]; then
to_found=true; break
fi
done
if [ $to_found = false ]; then
echo -e $RED"[TO_CURRENCY] not valid" 1>&2
echo -e "type <alias> help for valid currencies" 1>&2
echo -e "\n"$RESET_COLOR
exit 1
fi
done
from_currency=$( echo $from_currency | tr '[:lower:]' '[:upper:]' )
declare -i position=0
for curr in ${to_currencies[@]}
do
curr=$( echo $curr | tr '[:lower:]' '[:upper:]' )
IDs[$position]="$from_currency""_""$curr"
position+=1
IDs[$position]=","
position+=1
done
unset IDs[$(expr $position - 1)]
declare -i length=${#to_currencies[@]}
declare -a rates
declare request
for i in ${IDs[@]}
do
request=$request"$i"
done
get=$(curl --silent https://free.currconv.com/api/v7/convert\?q\=${request}\&compact\=ultra\&apiKey\=${API_KEY})
rates=($(echo $get | egrep -o '*[0-9.]*[0-9]'))
for ((i=0; i < length; i++))
do
echo -en "$GREEN$from_currency" "to" "$(echo -en ${to_currencies[$i]} | tr '[:lower:]' '[:upper:]')"
echo -en ": "$UNDERLINE
LC_NUMERIC="en_US.UTF-8" printf "%0.3f\n" $(echo "${rates[$i]} * $amount" | bc -l)
echo -en $RESET_COLOR
done
echo -en '\n'
exit 0