# 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#--}
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
}
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
#
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
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
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