diff --git a/process-likes-g1tx.sh b/process-likes-g1tx.sh index c2553fd..f7507bf 100755 --- a/process-likes-g1tx.sh +++ b/process-likes-g1tx.sh @@ -19,7 +19,9 @@ ssbpub=$(cat ~/.ssb/secret | grep public\" | cut -d ' ' -f 4 | cut -d '.' -f 1 | amountPerLikeInUD="0.05" bold=$(tput bold) normal=$(tput sgr0) - +ssbMaxSize=8192 +ssbMsgHeadersSize=5810 # Here is a wild guess! + ############################################################################################ #### CHECK LIKE AND SEND LOVE # Let's get Ğ1 account balance @@ -56,7 +58,7 @@ printf "...but did you know it is also a valid Ğ1 wallet?\n%s\n\n" $g1pub sleep 2 printf "Let's check the current balance of your wallet!\n\n" 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 printf "A moment please...\n\n" sleep 1 @@ -64,22 +66,33 @@ sleep 1 silkajRelativeAmountPattern='Total\sRelative\s+=\s+(.*)\s+UD' -duniter_server="duniter-g1.p2p.legal:443" -echo "Testing server $duniter_server..." -echo "" -silkajOutput=$(silkaj -p "$duniter_server" balance $g1pub 2>/dev/null) -if [ $? -ne 0 ] -then - duniter_server="g1.duniter.org:443" - echo "Testing server $duniter_server..." - echo "" - silkajOutput=$(silkaj -p "$duniter_server" balance $g1pub 2>/dev/null) +duniter_servers[0]="duniter-g1.p2p.legal:443" +duniter_servers[1]="g1.duniter.org:443" +duniter_servers[2]="g1.presles.fr:443" +duniter_servers[3]="balboa.altsysnet.com:10900" +duniter_servers[4]="remuniter.cgeek.fr:16120" +duniter_servers[5]="duniter.moul.re:443" +duniter_servers[6]="77.152.31.154:20901" +duniter_servers[7]="duniter.g1.1000i100.fr:443" - if [ $? -eq 1 ] - then - echo "The server did not respond well. Please try again." - exit 1 - fi +silkajExitCode=1 +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." + exit 1 fi 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" sleep 1 -declare -a liked_authors declare -A likesNbPerAuthor declare -A likedPosts @@ -183,8 +195,6 @@ do else - liked_authors[${#liked_authors[@]}]=$target_author - ((totalLikesGiven++)) if [[ ${likesNbPerAuthor[$target_author]} -eq 0 ]] @@ -219,8 +229,6 @@ do fi done < <(printf '%s\n' "$messages") -liked_authors_nb=${#liked_authors[@]} - if [[ $totalLikesGiven -eq 0 ]] 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" @@ -234,40 +242,55 @@ printf "How many UDĞ1 do you want to send per each like you gave? (default is $ read a 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 - newBalance=$(echo "$balance - $amountGiven" | bc -l) - printf "${bold}%s UDĞ1${normal} will be given.\n\n" "$amountGiven" - sleep 1 - printf "After sending transactions, your new balance will be ${bold}%s UDĞ1${normal}\n\n" "$newBalance" - sleep 1 - printf "Press ENTER to continue: " && read - printf "\n" -else - printf "You don't have enough UDĞ1 to send all transactions.\n\n" - sleep 1 - printf "${bold}%s UDĞ1${normal} are needed.\n\n" "$amountGiven" - sleep 1 - printf "Try again with a different amount per like.\n\n" - maxAmountPerLike=$(awk -vp=$balance -vq=$totalLikesGiven 'BEGIN{printf "%.2f" ,p / q}') - sleep 1 - printf "Maximum amount per like possible: ${bold}%s UDĞ1${normal}\n\n" "$maxAmountPerLike" - exit 1 -fi + 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) + printf "${bold}%s UDĞ1${normal} will be given.\n\n" "$amountGiven" + sleep 1 + printf "After sending transactions, your new balance will be ${bold}%s UDĞ1${normal}\n\n" "$newBalance" + sleep 1 + printf "Press ENTER to continue: " && read + printf "\n" + else + printf "You don't have enough UDĞ1 to send all transactions.\n\n" + sleep 1 + printf "${bold}%s UDĞ1${normal} are needed.\n\n" "$amountGiven" + sleep 1 + printf "Try again with a different amount per like.\n\n" + maxAmountPerLike=$(awk -vp=$balance -vq=$totalLikesGiven 'BEGIN{printf "%.2f" ,p / q}') + sleep 1 + printf "Maximum amount per like possible: ${bold}%s UDĞ1${normal}\n\n" "$maxAmountPerLike" + sleep 1 + 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! -output="" -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") -output+="\n\nAs a means to thank you further, I have just sent you #Ğ1 libre money.\n\n" -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" -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" -output+="Below is the list of SSB users whose content I liked recently, and the amount each one was given:\n\n" -output+='| thanks to | for their posts | given |\n' -output+='| --- | --- | ---- |\n' +n=0 +outputs[$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") +outputs[$n]+="\n\nAs a means to thank you further, I have just sent you #Ğ1 libre money.\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" +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" +outputs[$n]+="Below is the list of SSB users whose content I liked recently, and the amount each one was given:\n\n" + +tableHeaders='| thanks to | for their posts | given |\n' +tableHeaders+='| --- | --- | ---- |\n' + +outputs[$n]+=$tableHeaders for author_id in ${!likesNbPerAuthor[@]} do @@ -283,35 +306,53 @@ do echo $author_id >> $likedAuthorsFile fi + author_amount=$(bc <<< "${likesNbPerAuthor[$author_id]} * $amountPerLikeInUD") - output+=$(printf "| [@%s](%s) " "${author_name:1:-1}" "$author_id") - output+="| " + newLine='' + newLine+=$(printf "| [@%s](%s) " "${author_name:1:-1}" "$author_id") + newLine+="| " posts=${likedPosts[$author_id]} thisAuthorLikedPosts=( ${posts//\\n/ } ) - j=0 + firstPost=1 for post_id in ${thisAuthorLikedPosts[@]} do - if [[ $j -gt 0 ]] + if [[ $firstPost ]] then - output+=", " + firstPost=0 + else + newLine+=", " fi - output+=$(printf " %s❤" "${likesNbPerPost[$post_id]}") - #output+=$(printf "[%s](%s)" "${post_id:0:9}" "$post_id") - output+=$(printf "[\`%s\`](%s)" "${excerpts[$post_id]}" "$post_id") + newLine+=$(printf " %s❤" "${likesNbPerPost[$post_id]}") + newLine+=$(printf "[%s](%s)" "${post_id:0:9}" "$post_id") + newLine+=$(printf "[\`%s\`](%s)" "${excerpts[$post_id]}" "$post_id") - ((j++)) 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" - 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 + messageSize=$(printf "%s%s" "${outputs[$n]}" "$newLine" | wc -c) + 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}" @@ -322,44 +363,60 @@ do exit 1 fi - if ! mv "$state_file"~ "$state_file" - then - 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 - exit 1 - fi + #if ! mv "$state_file"~ "$state_file" + #then + # 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 + # exit 1 + #fi - sleep 20 # DO NOT OVER CHARGE DUNITER + #sleep 20 # DO NOT OVER CHARGE DUNITER 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! -#echo -e "$output" +#echo -e "${outputs[$n]}" # the following produces error: # "sbotc: unexpected end of parent stream" # 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 - -msg_file=~/thank-your-butts-$last_ts.md -echo -e $output > $msg_file - printf "What now ?\n\n" 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" sleep 2 -printf "It's a file.\n\n" - -sleep 2 - -printf "It's called thank-your-butts-$last_ts.md.\n\n" +if [[ ${#outputs[@]} -eq 1 ]] +then + printf "It's a file.\n\n" + sleep 2 + 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