g1-stats/scripts/walletsBalance.sh

71 lines
2.5 KiB
Bash
Executable File

#!/bin/bash
# Récupération du chemin absolu du répertoire du script actuel
GPATH="$( cd "$(dirname "$0")" ; pwd -P )/.."
[[ ! -f $GPATH/.env ]] && cp $GPATH/.env.example $GPATH/.env
source $GPATH/.env
node=$(cut -d '/' -f3 <<<"$DUNITER")
#Check if ed is installed
which ed || { echo "$0: Error: please install ed" >&2 ; exit 1 ; }
#Check if Jaklis or silkaj is installed
if [[ "$JAKLIS" ]] ; then
exec() {
jsonResult=$($JAKLIS idBalance -p $i) || return $?
balance=$(echo "$jsonResult" | jq -r '.balance')
username=$(echo "$jsonResult" | jq -r '.username')
}
elif which silkaj ; then
exec() {
balance=$(silkaj -p "$node" balance "$i") || return $?
balance=$(sed -n 's,.* \([\.0-9]\+\) Ğ1.*,\1,p' <<<$balance)
username=$(silkaj -p "$node" id "$i") || return $?
username=$(sed -n 's,.*: \(.*\)$,\1,p' <<<$username)
}
else
echo "$0: Error: you should use Jaklis or install silkaj." >&2
exit 1
fi
work() {
for i in $wallets; do
((j++))
# For tests only
#(( j > 42 )) && break
unset k
while ! exec ; do echo "Erreur $?: retry=$((k)) wallet=$i"; ((k++ > 9999)) && break ; sleep $((RANDOM%8)) ; done
formatedBalance=$(printf "%-10s | %-45s | %-1s" $balance $i $username)
[[ $1 == "members" ]] && username="<b>$username</b>"
htmlBloc="$htmlBloc<td>$balance</td><td><a id=\"k$i\" href=\"payto://g1/$i\">$i</a></td><td><b>$username</b></td>"$'\n'
echo "$formatedBalance" | tee -a /tmp/balance-g1.txt | sed "s,^,$j | ,"
totalBalance="$totalBalance$formatedBalance"$'\n'
done
}
wallets=$(cat $WEBPATH/data/wallets-g1-membres.txt 2>/dev/null)
[[ -z $wallets ]] && wallets=$(curl -s https://g1-stats.axiom-team.fr/data/wallets-g1-membres.txt)
work members && unset wallets
wallets=$(cat $WEBPATH/data/wallets-g1-simple.txt 2>/dev/null)
[[ -z $wallets ]] && wallets=$(curl -s https://g1-stats.axiom-team.fr/data/wallets-g1-simple.txt)
work && unset wallets
totalBalance=$(sort -nr <<<$totalBalance)
[[ ! -d $GPATH/db ]] && mkdir $GPATH/db
grep . <<<"$totalBalance" > $GPATH/db/wallets_balance.txt && rm /tmp/balance-g1.txt
# Construct HTML
cp $GPATH/tpl/css/wallets_balance.css $WEBPATH/css/
cp $GPATH/tpl/js/wallets_balance.js $WEBPATH/js/
cp $GPATH/tpl/wallets_balance.html $WEBPATH/data/wallets_balance.html
sort -Vr <<<"$htmlBloc" | awk '{ print "<tr id=\"p"NR"\"><td>"NR"</td>"$0"</tr>" ; }' > /tmp/solde_g1_html.txt
printf '%s\n' '/_LINE/r /tmp/solde_g1_html.txt' 1 '/_LINE/d' w | ed $WEBPATH/data/wallets_balance.html > /dev/null
sed -i "0,/_DATE/s//$(date '+%d-%m-%Y')/" $WEBPATH/data/wallets_balance.html
rm /tmp/solde_g1_html.txt