Add more servers + split file

This commit is contained in:
Boris Paing 2020-03-19 00:56:23 +01:00
parent 5db26e0ecd
commit a9ad86a5d1
1 changed files with 140 additions and 83 deletions

View File

@ -19,6 +19,8 @@ ssbpub=$(cat ~/.ssb/secret | grep public\" | cut -d ' ' -f 4 | cut -d '.' -f 1 |
amountPerLikeInUD="0.05" amountPerLikeInUD="0.05"
bold=$(tput bold) bold=$(tput bold)
normal=$(tput sgr0) normal=$(tput sgr0)
ssbMaxSize=8192
ssbMsgHeadersSize=5810 # Here is a wild guess!
############################################################################################ ############################################################################################
#### CHECK LIKE AND SEND LOVE #### CHECK LIKE AND SEND LOVE
@ -56,7 +58,7 @@ printf "...but did you know it is also a valid Ğ1 wallet?\n%s\n\n" $g1pub
sleep 2 sleep 2
printf "Let's check the current balance of your wallet!\n\n" printf "Let's check the current balance of your wallet!\n\n"
sleep 1 sleep 1
printf "We are interrogating the Ğ1 blockchain to check if anyone has already sent you Ğ1...\n\n" printf "I am interrogating the Ğ1 blockchain to check if anyone has already sent you Ğ1...\n\n"
sleep 2 sleep 2
printf "A moment please...\n\n" printf "A moment please...\n\n"
sleep 1 sleep 1
@ -64,22 +66,33 @@ sleep 1
silkajRelativeAmountPattern='Total\sRelative\s+=\s+(.*)\s+UD' silkajRelativeAmountPattern='Total\sRelative\s+=\s+(.*)\s+UD'
duniter_server="duniter-g1.p2p.legal:443" duniter_servers[0]="duniter-g1.p2p.legal:443"
echo "Testing server $duniter_server..." duniter_servers[1]="g1.duniter.org:443"
echo "" duniter_servers[2]="g1.presles.fr:443"
silkajOutput=$(silkaj -p "$duniter_server" balance $g1pub 2>/dev/null) duniter_servers[3]="balboa.altsysnet.com:10900"
if [ $? -ne 0 ] duniter_servers[4]="remuniter.cgeek.fr:16120"
then duniter_servers[5]="duniter.moul.re:443"
duniter_server="g1.duniter.org:443" duniter_servers[6]="77.152.31.154:20901"
echo "Testing server $duniter_server..." duniter_servers[7]="duniter.g1.1000i100.fr:443"
echo ""
silkajOutput=$(silkaj -p "$duniter_server" balance $g1pub 2>/dev/null)
if [ $? -eq 1 ] silkajExitCode=1
then i=0
while [ $silkajExitCode -ne 0 ] && [ $i -lt ${#duniter_servers[@]} ]
do
echo "Testing server ${duniter_servers[$i]}..."
echo ""
silkajOutput=$(silkaj -p "${duniter_servers[$i]}" balance $g1pub 2>/dev/null)
silkajExitCode=$?
((i++))
done
if [ $silkajExitCode -eq 1 ]
then
echo "The server did not respond well. Please try again." echo "The server did not respond well. Please try again."
exit 1 exit 1
fi
fi fi
if [[ $silkajOutput =~ $silkajRelativeAmountPattern ]] if [[ $silkajOutput =~ $silkajRelativeAmountPattern ]]
@ -119,7 +132,6 @@ days_from_last_tx=$(( (current_ts - last_ts) / (24*60*60*1000) ))
printf "First, let's see how much ❤ you gave lately...\n\n" printf "First, let's see how much ❤ you gave lately...\n\n"
sleep 1 sleep 1
declare -a liked_authors
declare -A likesNbPerAuthor declare -A likesNbPerAuthor
declare -A likedPosts declare -A likedPosts
@ -183,8 +195,6 @@ do
else else
liked_authors[${#liked_authors[@]}]=$target_author
((totalLikesGiven++)) ((totalLikesGiven++))
if [[ ${likesNbPerAuthor[$target_author]} -eq 0 ]] if [[ ${likesNbPerAuthor[$target_author]} -eq 0 ]]
@ -219,8 +229,6 @@ do
fi fi
done < <(printf '%s\n' "$messages") done < <(printf '%s\n' "$messages")
liked_authors_nb=${#liked_authors[@]}
if [[ $totalLikesGiven -eq 0 ]] if [[ $totalLikesGiven -eq 0 ]]
then then
printf "You did not give any like during the past %s days or they have already been processed.\n\n" "$days_from_last_tx" printf "You did not give any like during the past %s days or they have already been processed.\n\n" "$days_from_last_tx"
@ -234,11 +242,18 @@ printf "How many UDĞ1 do you want to send per each like you gave? (default is $
read a read a
echo "" echo ""
amountPerLikeInUD=$a tx_are_possible=0
amountGiven=$(echo "$totalLikesGiven * $amountPerLikeInUD" | bc -l) while [[ $tx_are_possible -eq 0 ]]
do
if (( $(echo "$amountGiven <= $balance" | bc -l) )); then amountPerLikeInUD=$a
amountGiven=$(echo "$totalLikesGiven * $amountPerLikeInUD" | bc -l)
tx_are_possible=$(( $(echo "$amountGiven <= $balance" | bc -l) ))
if [[ $tx_are_possible -ne 0 ]] ; then
newBalance=$(echo "$balance - $amountGiven" | bc -l) newBalance=$(echo "$balance - $amountGiven" | bc -l)
printf "${bold}%s UDĞ1${normal} will be given.\n\n" "$amountGiven" printf "${bold}%s UDĞ1${normal} will be given.\n\n" "$amountGiven"
sleep 1 sleep 1
@ -246,7 +261,7 @@ if (( $(echo "$amountGiven <= $balance" | bc -l) )); then
sleep 1 sleep 1
printf "Press ENTER to continue: " && read printf "Press ENTER to continue: " && read
printf "\n" printf "\n"
else else
printf "You don't have enough UDĞ1 to send all transactions.\n\n" printf "You don't have enough UDĞ1 to send all transactions.\n\n"
sleep 1 sleep 1
printf "${bold}%s UDĞ1${normal} are needed.\n\n" "$amountGiven" printf "${bold}%s UDĞ1${normal} are needed.\n\n" "$amountGiven"
@ -255,19 +270,27 @@ else
maxAmountPerLike=$(awk -vp=$balance -vq=$totalLikesGiven 'BEGIN{printf "%.2f" ,p / q}') maxAmountPerLike=$(awk -vp=$balance -vq=$totalLikesGiven 'BEGIN{printf "%.2f" ,p / q}')
sleep 1 sleep 1
printf "Maximum amount per like possible: ${bold}%s UDĞ1${normal}\n\n" "$maxAmountPerLike" printf "Maximum amount per like possible: ${bold}%s UDĞ1${normal}\n\n" "$maxAmountPerLike"
exit 1 sleep 1
fi printf "How many UDĞ1 do you want to send per each like you gave? "
read a
echo ""
fi
done
# Let's construct thank you message and send transactions! # Let's construct thank you message and send transactions!
output="" n=0
output+=$(printf "Huge thanks to the ScuttleButt community for all the fascinating posts you allowed me to read in the past %s days." "$days_from_last_tx") outputs[$n]=""
output+="\n\nAs a means to thank you further, I have just sent you #Ğ1 libre money.\n\n" outputs[$n]+=$(printf "Huge thanks to the ScuttleButt community for all the fascinating posts you allowed me to read in the past %s days." "$days_from_last_tx")
output+="These messages, though they might feel spammy (sorry) are also a way for the Ğ1 community to spread awareness about [libre currencies](https://libre-currency.org/) so we can build #resilience at every level.\n\n" outputs[$n]+="\n\nAs a means to thank you further, I have just sent you #Ğ1 libre money.\n\n"
output+="You can learn how to spend your freshly earned money at [https://git.p2p.legal/Axiom-Team/ssb-g1like](https://git.p2p.legal/Axiom-Team/ssb-g1like)\n\n" outputs[$n]+="These messages, though they might feel spammy (sorry) are also a way for the Ğ1 community to spread awareness about [libre currencies](https://libre-currency.org/) so we can build #resilience at every level.\n\n"
output+="Below is the list of SSB users whose content I liked recently, and the amount each one was given:\n\n" outputs[$n]+="You can learn how to spend your freshly earned money at [https://git.p2p.legal/Axiom-Team/ssb-g1like](https://git.p2p.legal/Axiom-Team/ssb-g1like)\n\n"
output+='| thanks to | for their posts | given |\n' outputs[$n]+="Below is the list of SSB users whose content I liked recently, and the amount each one was given:\n\n"
output+='| --- | --- | ---- |\n'
tableHeaders='| thanks to | for their posts | given |\n'
tableHeaders+='| --- | --- | ---- |\n'
outputs[$n]+=$tableHeaders
for author_id in ${!likesNbPerAuthor[@]} for author_id in ${!likesNbPerAuthor[@]}
do do
@ -283,35 +306,53 @@ do
echo $author_id >> $likedAuthorsFile echo $author_id >> $likedAuthorsFile
fi fi
author_amount=$(bc <<< "${likesNbPerAuthor[$author_id]} * $amountPerLikeInUD") author_amount=$(bc <<< "${likesNbPerAuthor[$author_id]} * $amountPerLikeInUD")
output+=$(printf "| [@%s](%s) " "${author_name:1:-1}" "$author_id") newLine=''
output+="| " newLine+=$(printf "| [@%s](%s) " "${author_name:1:-1}" "$author_id")
newLine+="| "
posts=${likedPosts[$author_id]} posts=${likedPosts[$author_id]}
thisAuthorLikedPosts=( ${posts//\\n/ } ) thisAuthorLikedPosts=( ${posts//\\n/ } )
j=0 firstPost=1
for post_id in ${thisAuthorLikedPosts[@]} for post_id in ${thisAuthorLikedPosts[@]}
do do
if [[ $j -gt 0 ]] if [[ $firstPost ]]
then then
output+=", " firstPost=0
else
newLine+=", "
fi fi
output+=$(printf " %s❤" "${likesNbPerPost[$post_id]}") newLine+=$(printf " %s❤" "${likesNbPerPost[$post_id]}")
#output+=$(printf "[%s](%s)" "${post_id:0:9}" "$post_id") newLine+=$(printf "[%s](%s)" "${post_id:0:9}" "$post_id")
output+=$(printf "[\`%s\`](%s)" "${excerpts[$post_id]}" "$post_id") newLine+=$(printf "[\`%s\`](%s)" "${excerpts[$post_id]}" "$post_id")
((j++))
done done
output+=$(printf "| %s UDĞ1 " $author_amount) newLine+=$(printf "| %s UDĞ1 " $author_amount)
output+="|\n" newLine+="|\n"
#printf '%s\n' "silkaj -af --file ~/.ssb/secret.dunikey tx --output $g1_author --amountUD $author_amount --comment "Thx for your cool posts on ScuttleButt" messageSize=$(printf "%s%s" "${outputs[$n]}" "$newLine" | wc -c)
silkaj -p "$duniter_server" -af --file ~/.ssb/secret.dunikey tx --output $g1_author --amountUD $author_amount --comment "Thx for your cool posts on ScuttleButt" -y 2>/dev/null totalSize=$((messageSize + ssbMsgHeadersSize))
if [[ $totalSize -gt $ssbMaxSize ]]
then
outputs[$n]+="For message size reasons, the rest of the list had to be displayed below, in the following answer:"
((n++))
outputs[$n]+=$tableHeaders
fi
outputs[$n]+=$newLine
#printf '%s\n' "silkaj -af --file ~/.ssb/secret.dunikey tx --outputs[$n] $g1_author --amountUD $author_amount --comment "Thx for your cool posts on ScuttleButt"
#silkaj -p "$duniter_server" -af --file ~/.ssb/secret.dunikey tx --outputs[$n] $g1_author --amountUD $author_amount --comment "Thx for your cool posts on ScuttleButt" -y 2>/dev/null
printf "\n${bold}%s UDĞ1${normal} sent to %s!\n\n" "$author_amount" "${author_name:1:-1}" printf "\n${bold}%s UDĞ1${normal} sent to %s!\n\n" "$author_amount" "${author_name:1:-1}"
@ -322,44 +363,60 @@ do
exit 1 exit 1
fi fi
if ! mv "$state_file"~ "$state_file" #if ! mv "$state_file"~ "$state_file"
then #then
msg_id=$(printf %s "$msg" | jq -r .key) # msg_id=$(printf %s "$msg" | jq -r .key)
printf 'Unable to write to state file. Update state file manually to prevent %s from being processed twice.\n' "$msg_id" >&2 # printf 'Unable to write to state file. Update state file manually to prevent %s from being processed twice.\n' "$msg_id" >&2
exit 1 # exit 1
fi #fi
sleep 20 # DO NOT OVER CHARGE DUNITER #sleep 20 # DO NOT OVER CHARGE DUNITER
done done
printf "\n%s UDĞ1 sent to %s butts!\n\n" "$amountGiven" "$liked_authors_nb" printf "\n%s UDĞ1 sent to %s butts!\n\n" "$amountGiven" "${#likesNbPerAuthor[@]}"
# Let's publicly thank everyone! # Let's publicly thank everyone!
#echo -e "$output" #echo -e "${outputs[$n]}"
# the following produces error: # the following produces error:
# "sbotc: unexpected end of parent stream" # "sbotc: unexpected end of parent stream"
# must be a non-escaped quote problem... # must be a non-escaped quote problem...
#thank_you_msg=$(printf "%q" "$output") #thank_you_msg=$(printf "%q" "${outputs[$n]}")
#sbotc publish '{"type":"post","text":"'"$thank_you_msg"'"}' 2>&1>/dev/null #sbotc publish '{"type":"post","text":"'"$thank_you_msg"'"}' 2>&1>/dev/null
msg_file=~/thank-your-butts-$last_ts.md
echo -e $output > $msg_file
printf "What now ?\n\n" printf "What now ?\n\n"
sleep 1 sleep 1
date=$(date -u +%Y-week-%W)
if [[ ${#outputs[@]} -eq 1 ]]
then
msg_filename=thank-your-butts-$date
echo -e ${outputs[$n]} > ~/$msg_filename
else
for i in ${!outputs[@]}
do
part=$((i + 1))
msg_filename=thank-your-butts-$date
echo -e ${outputs[$i]} > ~/$msg_filename-part-$part.md
done
fi
printf "A surprise is awaiting in your home dir (~).\n\n" printf "A surprise is awaiting in your home dir (~).\n\n"
sleep 2 sleep 2
printf "It's a file.\n\n" if [[ ${#outputs[@]} -eq 1 ]]
then
sleep 2 printf "It's a file.\n\n"
sleep 2
printf "It's called thank-your-butts-$last_ts.md.\n\n" printf "It's called $msg_filename.md\n\n"
else
printf "It's %s files.\n\n" "${#outputs[@]}"
sleep 2
printf "The first one is called $msg_filename-part-1.md.\n\n"
fi
sleep 2 sleep 2