Get-Last-RSS-Post/get-last-rss-post.sh

157 lines
4.7 KiB
Bash
Raw Permalink Normal View History

2024-04-28 15:23:20 +02:00
#!/bin/sh
help() {
2024-05-12 16:50:41 +02:00
echo "Usage: $0 [-d] [-r] [-t VISIBILITY] [-h] URL"
2024-04-29 12:05:38 +02:00
echo ""
echo " Downloads the RSS feed of the URL and checks the latest post."
echo " It provides a simple way to copy/paste the Title and Link of the post."
echo " If toot is installed, it can post the status with custom visibility."
2024-05-18 20:02:40 +02:00
echo " There is also set of funny(?) random variants of the message."
2024-04-29 12:05:38 +02:00
echo " Arguments:"
echo " -d Checks if the Publication Date of the post is Today."
echo " If not, it exits."
2024-05-18 20:02:40 +02:00
echo " -r Uses a random expression."
2024-04-29 12:05:38 +02:00
echo " -t Post the link with toot. A prompt will ask confirmation."
echo " Accepted value: direct, unlisted, public, private"
echo " -h Displays this message."
echo ""
2024-04-28 15:23:20 +02:00
}
2024-04-28 17:58:16 +02:00
2024-05-12 16:46:11 +02:00
while getopts ":drt:h" opt; do
2024-04-29 12:05:38 +02:00
case $opt in
d)
TODAY=$(date +'%d %b %Y')
CHECK_TODAY=1
;;
2024-05-12 16:46:11 +02:00
r)
RANDOMIZE=1
;;
2024-04-29 12:05:38 +02:00
t)
TOOT=1
case "${OPTARG}" in
direct|unlisted|public|private)
VISIBILITY=${OPTARG}
;;
*)
2024-04-29 15:51:57 +02:00
echo "Invalid visibility option: --${OPTARG}"
2024-04-29 12:05:38 +02:00
exit 1
;;
esac
;;
h)
# Help
help
exit 1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
shift $(($OPTIND - 1))
if [[ "$*" == "" ]]; then
2024-04-28 15:23:20 +02:00
help
exit 1
fi
2024-05-18 20:02:40 +02:00
echo_msg () {
echo "\n+++++"
echo "$EXPRESSION \"$TITLE\"\n\n$LINK"
echo "+++++\n"
}
2024-04-28 17:58:16 +02:00
today() {
2024-04-28 15:23:20 +02:00
# Checks if pubDate is $TODAY
2024-04-29 12:05:38 +02:00
echo "Checking if the post pubblication days is ${TODAY}"
2024-04-28 15:23:20 +02:00
echo $PUB_DATE | grep -q "$TODAY"
if [[ "$?" == "0" ]]; then # pubDate is $TODAY
echo "Published Date is today - good!"
else
echo "ERROR. Published Date is different than today. Exiting."
cleanup 1
fi
}
2024-05-12 16:46:11 +02:00
random() {
EXPRESSIONS=("Latest article on my blog:"
"Latest rant on my blog:"
"Check out my latest post!"
"New. Blog. Post. NOW!"
"Just wrote a new blog post:"
"Just hit some random keys on the keyboard and pressed \"Publish\":"
"Just hit some random keys on my keyboard:"
"New thoughts:"
"New wordvomit:"
2024-05-18 16:05:20 +02:00
"I could have used some cool app to publish this post. Instead I used a script. 🤖"
2024-05-12 16:46:11 +02:00
"100 PRINT \"NEW BLOG POST\"\n200 PRINT"
"Yet another article on my blog."
2024-05-18 16:05:20 +02:00
"🤖 This is a random sentence chosen by a Bash script.\nSorry if it doesn't make much sense.\nAnd this is my new blog post:"
2024-05-12 16:46:11 +02:00
"Roses are red. Violets are blue. Here is a new blog post:"
"Sit down, grab a cup of tea, and click here for a new article on my blog:"
"🧠 -> 🧑‍💻 ->"
2024-05-18 16:05:20 +02:00
"$ cat new_blog_post.md\n"
"🤖 Beep. Boop. I'm a script to share my new blog post:")
2024-05-12 16:46:11 +02:00
EXPRESSION=${EXPRESSIONS[ $RANDOM % ${#EXPRESSIONS[@]} ]}
}
2024-04-28 17:58:16 +02:00
yes_or_no() {
while true; do
2024-05-23 12:18:29 +02:00
read -p "$* [Y]es/[N]o/[R]andom: " ynr
2024-05-18 20:02:40 +02:00
case $ynr in
2024-05-12 16:46:11 +02:00
[Yy]*) echo "$EXPRESSION \"$TITLE\"\n\n$LINK" | \
2024-04-29 12:05:38 +02:00
/opt/homebrew/bin/toot post -v $VISIBILITY ; return 0 ;;
[Nn]*) echo "Exiting." ; cleanup 0 ;;
2024-05-18 20:10:29 +02:00
[Rr]*) echo "\nPicking up a new expression:"; random; echo_msg ;;
2024-04-28 17:58:16 +02:00
esac
done
}
2024-04-28 15:23:20 +02:00
cleanup () {
rm -f feed.xml
2024-05-04 16:30:34 +02:00
rm -f feed_old.xml
2024-04-28 17:58:16 +02:00
rm -f status.txt
2024-04-28 15:23:20 +02:00
exit $1
}
echo "Downloading latest XML feed…"
2024-04-29 12:05:38 +02:00
wget -q -O feed.xml $*
2024-04-28 15:23:20 +02:00
if ! [[ $? == "0" ]]; then echo "Error."; cleanup 1; else echo "Done."; fi
# Check if downloaded file has <rss> tag
2024-04-28 15:41:43 +02:00
grep -q '<rss' feed.xml
if ! [[ $? == "0" ]]; then echo "This is not a RSS feed. Exiting."; cleanup 1; fi
2024-04-28 15:23:20 +02:00
2024-05-04 16:30:34 +02:00
# Clean up RSS from tabs
mv feed.xml feed_old.xml; cat feed_old.xml | tr -d ' ' > feed.xml
2024-04-28 15:23:20 +02:00
# Find 2nd clean title (1st post) and replaces / with \/
2024-05-04 16:30:34 +02:00
TITLE=$(awk '/<title>/{++n; if (n==2) {print; exit}}' feed.xml | sed -e 's/<title>\(.*\)<\/title>/\1/' | xargs)
#echo "TITLE: $TITLE"
2024-04-28 15:23:20 +02:00
# Find the link of the article
2024-05-04 16:30:34 +02:00
LINK=$(grep -A1 "<title>$TITLE" feed.xml | tail -n 1 | sed 's/<link>\(.*\)<\/link>/\1/' | xargs)
#echo "LINK: $LINK"
2024-04-28 15:23:20 +02:00
# Replace / with \/ in the link
LINK_FIXED=$(echo $LINK | sed 's./.\\/.g')
# Print whole post until pubDate tag, saves the PUB_DATE
PUB_DATE=$(sed -n "/<link>$LINK_FIXED/,/pubDate/p" feed.xml | tail -n 1 | sed 's/<pubDate>//g; s/<\/pubDate>//g')
if [[ "$CHECK_TODAY" == "1" ]]; then today; fi
2024-05-12 16:46:11 +02:00
EXPRESSION="Latest article on my blog:"
if [[ "$RANDOMIZE" == "1" ]]; then random; fi
2024-05-18 20:02:40 +02:00
echo "\nCopy and Paste this:"
echo_msg
2024-04-28 17:58:16 +02:00
2024-04-29 17:09:18 +02:00
if [[ "$TOOT" == "1" ]]; then
which toot > /dev/null # Check if toot is installed
if ! [[ $? == "0" ]]; then echo "ERROR. Toot is not installed. Exiting."; cleanup 1; fi
2024-05-18 16:16:35 +02:00
ACCOUNT=$(toot auth | grep ACTIVE | awk '{print $2}')
yes_or_no "Post it on Mastodon (as $ACCOUNT)?"
2024-04-29 17:09:18 +02:00
fi
2024-04-28 15:23:20 +02:00
cleanup 0