1
0
mirror of https://github.com/Llewellynvdm/CoinRate.git synced 2024-05-28 22:10:48 +00:00

fixed messaging bundeling, also improved some dynamic selecting to only send one message per currency pair

This commit is contained in:
Llewellyn van der Merwe 2017-12-30 05:12:04 +02:00
parent 9c6661a1d5
commit dc971e9c00
No known key found for this signature in database
GPG Key ID: CAD7B16D27AF28C5
2 changed files with 82 additions and 50 deletions

View File

@ -107,8 +107,10 @@ function runMain () {
# get active currency target # get active currency target
function getActiveCurrencyTarget () { function getActiveCurrencyTarget () {
# get price if not already set
get_Price
# get the price value # get the price value
value=$(get_Price "${API}${Currency}/${Target}") value="${CurrencyPair[${Currency}${Target}]}"
# set send key # set send key
setSendKey setSendKey
# set target values and perform action if only TargetValue given # set target values and perform action if only TargetValue given
@ -209,11 +211,11 @@ function setMessage () {
local current_value="$2" local current_value="$2"
local target_value="$3" local target_value="$3"
# build message # build message
message="${Currency} is ${target_type} ${target_value} ${Target} at ${current_value} ${Target}" message="${Currency} is ${target_type} ${target_value} ${Target} at ${current_value} ${Target}" &&
# first send to comand line # first send to comand line
echoTweak "${message} - ${Datetimenow} " echoTweak "${message} - ${Datetimenow} " &&
# is it send time # is it send time
sendTime "$target_type" "$target_value" sendTime "$target_type" "$target_value" &&
# set to messages # set to messages
setMessages "$message" "$target_type" "$target_value" setMessages "$message" "$target_type" "$target_value"
} }
@ -226,14 +228,38 @@ function setMessages () {
local value="$3" local value="$3"
# check if we should set messages # check if we should set messages
if (( "$send" == 1 )); then if (( "$send" == 1 )); then
# we can set message # we can set message
if [ "$type" == "below" ]; then if [ "$type" == "below" ]; then
# set below message # set below message
belowMessages["$value"]="$message" belowMessages["${Currency}${Target}${value}"]="$message"
elif [ "$type" == "above" ]; then # check if we have this array declared
# set above message if [[ -z "${belowKeys[${Currency}${Target}]+unset}" ]]; then
aboveMessages["$value"]="$message" # load the value
belowKeys["${Currency}${Target}"]="$value"
else
# test if we should load the value
local cValue="${belowKeys[${Currency}${Target}]}"
local updateValue=$(echo "$cValue > $value" | bc -l)
if (( "$updateValue" == 1 )); then
belowKeys["${Currency}${Target}"]="$value"
fi
fi fi
elif [ "$type" == "above" ]; then
# set above message
aboveMessages["${Currency}${Target}${value}"]="$message"
# check if we have this array declared
if [[ -z "${aboveKeys[${Currency}${Target}]+unset}" ]]; then
# load the value
aboveKeys["${Currency}${Target}"]="$value"
else
# test if we should load the value
local cValue="${aboveKeys[${Currency}${Target}]}"
local updateValue=$(echo "$cValue < $value" | bc -l)
if (( "$updateValue" == 1 )); then
aboveKeys["${Currency}${Target}"]="$value"
fi
fi
fi
fi fi
} }
@ -247,9 +273,9 @@ function sendMessages () {
IFS=$'\n' IFS=$'\n'
local messages="${Messages[*]}" local messages="${Messages[*]}"
# send Telegram messages if allowed # send Telegram messages if allowed
sendTelegram "${messages}" sendTelegram "${messages}" &&
# show linux messages if allowed # show linux messages if allowed
showLinuxMessage "${messages}" showLinuxMessage "${messages}" &&
# send SMS messages if allowed # send SMS messages if allowed
sendSMSMessage "${messages}" sendSMSMessage "${messages}"
fi fi
@ -257,42 +283,33 @@ function sendMessages () {
# filter messages to only send the lowest-below and the highest-above # filter messages to only send the lowest-below and the highest-above
function filterMessages () { function filterMessages () {
# check if lower value is found # load a currency pair only once (above/below)
if [ ${#belowMessages[@]} -gt 0 ]; then declare -A oncePer
# get lowest
activeKey=$( getActiveBelowMessages )
# set to messages
Messages+=("${belowMessages[$activeKey]}")
fi
# check if higher value is found # check if higher value is found
if [ ${#aboveMessages[@]} -gt 0 ]; then if [ ${#aboveMessages[@]} -gt 0 ]; then
# get highest for i in "${!aboveKeys[@]}"
activeKey=$( getActiveAboveMessage ) do
# set to messages # set it
Messages+=("${aboveMessages[$activeKey]}") oncePer["$i"]="$i"
# get the value
local valKey="${aboveKeys[$i]}"
# set to messages
Messages+=("${aboveMessages[$i$valKey]}")
done
fi
# check if lower value is found
if [ ${#belowMessages[@]} -gt 0 ]; then
for i in "${!belowKeys[@]}"
do
#check if it was set already
if [[ -z "${oncePer[$i]+unset}" ]]; then
# get the value
local valKey="${belowKeys[$i]}"
# set to messages
Messages+=("${belowMessages[$i$valKey]}")
fi
done
fi fi
}
# array sort
function getActiveBelowMessages () {
# start the search
local keys+=$(for i in "${!belowMessages[@]}"
do
echo $i
done | sort -n)
# return keys
echo $( echo "${keys}" | head -n1 )
}
# array sort
function getActiveAboveMessage () {
# start the search
local keys+=$(for i in "${!aboveMessages[@]}"
do
echo $i
done | sort -rn)
# return keys
echo $( echo "${keys}" | head -n1 )
} }
# show message in linux (will not work on server) # show message in linux (will not work on server)
@ -367,10 +384,21 @@ function setSendKey () {
# getting the price from CEX.io (API) # getting the price from CEX.io (API)
function get_Price () { function get_Price () {
# get price from API # check if we already have this Currency Pair Value
json=$(wget -q -O- "$1") if [[ -z "${CurrencyPair[${Currency}${Target}]+unset}" ]]; then
value=($( echo "$json" | jq -r '.lprice')) # show what currency pair is being fetched
echo "${value}" if (( "$Factory" == 1 )); then
echoTweak "Getting the current price of $Currency in $Target"
fi
# get price from API
local URL="${API}${Currency}/${Target}"
# now get the json
local json=$(wget -q -O- "$URL")
# set the value
local value=($( echo "$json" | jq -r '.lprice'))
# add value to global bucket
CurrencyPair["${Currency}${Target}"]="$value"
fi
} }
# run some validation against the options given # run some validation against the options given

View File

@ -23,6 +23,7 @@ MAINLOAD=1
# Do some prep work # Do some prep work
command -v jq >/dev/null 2>&1 || { echo >&2 "We require jq for this script to run, but it's not installed. Aborting."; exit 1; } command -v jq >/dev/null 2>&1 || { echo >&2 "We require jq for this script to run, but it's not installed. Aborting."; exit 1; }
command -v curl >/dev/null 2>&1 || { echo >&2 "We require curl for this script to run, but it's not installed. Aborting."; exit 1; } command -v curl >/dev/null 2>&1 || { echo >&2 "We require curl for this script to run, but it's not installed. Aborting."; exit 1; }
command -v bc >/dev/null 2>&1 || { echo >&2 "We require bc for this script to run, but it's not installed. Aborting."; exit 1; }
# load notify # load notify
. "$DIR/functions.sh" . "$DIR/functions.sh"
@ -60,8 +61,11 @@ API="https://cex.io/api/last_price/"
FilePath='' FilePath=''
# Some Messages arrays # Some Messages arrays
declare -A CurrencyPair
declare -A aboveMessages declare -A aboveMessages
declare -A aboveKeys
declare -A belowMessages declare -A belowMessages
declare -A belowKeys
Messages=() Messages=()
# use UTC+00:00 time also called zulu # use UTC+00:00 time also called zulu