Skip to content

Commit

Permalink
Avoid duplicate posts
Browse files Browse the repository at this point in the history
Maintain a history of recent posts, and don't reuse a post if it's been used in the past 500 posts (or ~8 months, at the current rate of posting).
  • Loading branch information
waldoj committed Apr 29, 2024
1 parent aefa9d4 commit 52a779c
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions post.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,46 @@ function exit_error {
exit "${2-1}"
}

# Select an entry from the list
function select_entry {
# Select a random entry from the list
local entry=$(sort -R finds_you.txt | head -1)
# Remove any trailing carriage return from the entry
echo "$entry" | tr -d '\r'
}

# See if an entry has been used in the past 500 posts
function is_recent {
local entry_hash=$(echo -n "$1" | md5sum | cut -d ' ' -f 1)
if tail -n 50 post_history.txt | grep -q "$entry_hash"; then
return 0 # 0 = true, recent
else
return 1 # 1 = false, not recent
fi
}

# Move into the directory where this script is found
cd "$(dirname "$0")" || exit

# Select a random entry from the list
ENTRY=$(sort -R finds_you.txt |head -1)

# Remove any trailing carriage return from the entry
ENTRY=$(echo "$ENTRY" | tr -d '\r')
# Loop until a non-recent entry is found
while true; do
ENTRY=$(select_entry)
if is_recent "$ENTRY"; then
echo "Entry is recent, selecting another..."
continue
else
break
fi
done

POST="I hope this email ${ENTRY}"

# Send the message to Mastodon
curl "$MASTODON_SERVER"/api/v1/statuses -H "Authorization: Bearer ${MASTODON_TOKEN}" -F "status=${POST}"

RESULT=$?
if [ "$RESULT" -ne 0 ]; then
exit_error "Posting message to Mastodon failed"
fi

# Log this to the post history
echo -n "$ENTRY" | md5sum | cut -d ' ' -f 1 >> post_history.txt

0 comments on commit 52a779c

Please sign in to comment.