quiet/verbose, fix removing tnt/teof on mastodon
authorIan Kelling <iank@fsf.org>
Wed, 22 Nov 2023 07:02:16 +0000 (02:02 -0500)
committerIan Kelling <iank@fsf.org>
Wed, 22 Nov 2023 07:02:16 +0000 (02:02 -0500)
pdt.sh
t.py

diff --git a/pdt.sh b/pdt.sh
index c848ea768c8c1046d038c0c80ec00dcdc6cd1ae5..2e1e01045e0c0d03b2e57ad16d489e0f8bff00cf 100644 (file)
--- a/pdt.sh
+++ b/pdt.sh
@@ -37,7 +37,13 @@ _pdtsh_blocks=██████████████████████
 # posting a message like: 'Retweet:
 # https://nitter.net/fsf/status/1726382360826958325'
 tweet() {
-  local keys_file profile
+  local keys_file profile verbose_arg ret out line num_regex account_real_name
+  local md5_text
+  local -a out_lines
+  ret=0
+  if $verbose; then
+    verbose_arg=-v
+  fi
   keys_file=$_pdtsh_dir/twitter_keys.py
   if [[ $1 == --* ]]; then
     profile=${1#--}
@@ -45,18 +51,66 @@ tweet() {
     ln -s $keys_file-$profile $keys_file
     shift
   fi
+  if [[ $profile == dbd ]]; then
+    account_real_name=endDRM
+  elif [[ $profile ]]; then
+    account_real_name=$profile
+  else
+    account_real_name=USERNAME_HERE
+  fi
 
   # shellcheck disable=SC1090 # not relevant to this script
   source ~/src/tweepy/venv/bin/activate
-  {
-    printf "%s\n" "$*"
-    if [[ $media ]]; then
-      printf "%s\n" "$media"
-      if [[ $alt_text ]]; then
-        printf "%s\n" "$alt_text"
+  out=$(
+    {
+      printf "%s\n" "$*"
+      if [[ $media ]]; then
+        printf "%s\n" "$media"
+        if [[ $alt_text ]]; then
+          printf "%s\n" "$alt_text"
+        fi
+      fi
+    } |  $_pdtsh_tweet $verbose_arg 2>&1 ) || ret=1
+  if [[ $media ]]; then
+    md5_text="$(md5sum $media | awk '{print $1}') "
+  fi
+  post_info="$md5_text $*"
+  if (( ret == 1 )); then
+    if printf "%s\n" "$out" \
+        | grep -Fx 'You are not allowed to create a Tweet with duplicate content.' &>/dev/null; then
+      # We dont output duplicate error when not in verbose mode, because it is
+      # is pretty easy to trigger.
+      if grep -qFx -- "$post_info" ~/pdt/tweets.log; then
+        if $verbose; then
+          echo "pdt: error: post failed. twitter and our log says this tweet is duplicate." >&2
+        fi
+      else
+        printf "%s\n" "$post_info" >> ~/pdt/tweets.log
+        if $verbose; then
+          printf "%s\n" "$out" >&2
+        fi
       fi
+    else
+      printf "%s\n" "$out" >&2
     fi
-  } |  $_pdtsh_tweet || { deactivate; return 1; }
+    deactivate
+    return 1
+  fi
+  ## handle case of ret == 0
+  mapfile -t out_lines <<<"$out"
+  num_regex='$[0-9][0-9]*$'
+  for line in "${out_lines[@]}"; do
+    if [[ $line =~ $num_regex ]]; then
+      echo "https://nitter.net/$account_real_name/status/$line"
+    fi
+  done
+  if [[ $line ]]; then
+    if grep -qFx -- "$post_info" ~/pdt/tweets.log; then
+      echo "pdt: this tweet was a duplicate according to our log but unexpected was allowed."
+    else
+      printf "%s\n" "$post_info" >> ~/pdt/tweets.log
+    fi
+  fi
   deactivate
 }
 
@@ -134,7 +188,7 @@ toot() {
   if [[ $1 == --* ]]; then
     mast_profile=${1#--}
     shift
-    command toot activate --quiet $mast_profile || { deactivate; return 1; }
+    command toot activate $toot_quiet_arg $mast_profile || { deactivate; return 1; }
   fi
   command toot "$@" || { deactivate; return 1; }
   deactivate
@@ -185,7 +239,7 @@ toot() {
 #
 pdt() {
   local video media twitter_account gs_account mastodon_account video gs_arg network
-  local do_mastodon do_twitter do_gnusocial
+  local do_mastodon do_twitter do_gnusocial verbose
   local -a toot_args
   if [[ $pdttest ]]; then
     twitter_account=iank
@@ -269,7 +323,11 @@ pdt() {
     read -r -p "input PDT text: " input
     set -- "$input"
   fi
+  verbose=false
+  toot_quiet_arg=--quiet
   if [[ $- == *i* ]]; then
+    verbose=true
+    toot_quiet_arg=
     echo "About to PDT the following line. Press enter to confirm or ctrl-c to quit:"
     echo "$*"
     read -r
@@ -295,16 +353,15 @@ pdt() {
   text="${text//\/tnt\/}"
   text="${text//\/teof\/}"
 
-
   if $do_mastodon; then
-    if ! toot --$mastodon_account post --quiet "$*" "${toot_args[@]}"; then
+    if ! toot --$mastodon_account post $toot_quiet_arg "$text" "${toot_args[@]}"; then
       fails+=(toot)
     fi
   fi
   if $do_gnusocial; then
     # https://gnusocial.net/doc/twitterapi
     if ! curl -o /dev/null -sS -u "$gs_account:$(cat ~/.gnusocial_login-$gs_account)" \
-         $gs_arg -F "status=$*" https://status.fsf.org/api/statuses/update.xml; then
+         $gs_arg -F "status=$text" https://status.fsf.org/api/statuses/update.xml; then
       fails+=(gnu-social)
     fi
   fi
diff --git a/t.py b/t.py
index 164553f21dada4712a4418fdbcd758345e7f0c19..9507ebf09168250a1714df78b589e0d637d4a0c9 100755 (executable)
--- a/t.py
+++ b/t.py
@@ -99,11 +99,11 @@ if have_image:
 else:
     response = client.create_tweet(text=tweet_text)
     if verbose:
-        print(f"https://nitter.net/user/status/{response.data['id']}")
+        print(response.data['id'])
 if post_count > 1:
     for x in range(1, post_count):
         time.sleep(1)
         tweet_text=posts[x].strip() + f" {x+1}/{post_count}"
         response = client.create_tweet(text=tweet_text, in_reply_to_tweet_id=response.data['id'])
         if verbose:
-            print(f"{x+1}/{post_count} https://nitter.net/USER/status/{response.data['id']}")
+            print(response.data['id'])