71 lines
2.0 KiB
Bash
Executable File
71 lines
2.0 KiB
Bash
Executable File
#!/bin/sh
|
|
### get-last-rss-post.sh
|
|
### Usage: ./get-last-rss-post.sh [-t] URL
|
|
###
|
|
### Downloads the RSS feed of the URL and checks the latest post.
|
|
### It provides a simple way to copy/paste the Title and Link of the post.
|
|
### Arguments:
|
|
### -t | --today Checks if the Publication Date of the post is Today.
|
|
### If not, it exits.
|
|
|
|
help() {
|
|
sed -rn 's/^### ?//;T;p' "$0"
|
|
}
|
|
if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]] || [[ "$1" == "" ]]; then
|
|
help
|
|
exit 1
|
|
fi
|
|
|
|
if [[ "$1" == "-t" ]] || [[ "$1" == "--today" ]]; then
|
|
TODAY=$(date +'%d %b %Y')
|
|
echo "Checking if the post pubblication days is ${TODAY}…"
|
|
set -- "$2" # Reset arguments - link becomes $1
|
|
CHECK_TODAY=1
|
|
fi
|
|
|
|
today () {
|
|
# Checks if pubDate is $TODAY
|
|
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
|
|
}
|
|
|
|
cleanup () {
|
|
rm -f feed.xml
|
|
exit $1
|
|
}
|
|
|
|
echo "Downloading latest XML feed…"
|
|
wget -q -O feed.xml $1
|
|
if ! [[ $? == "0" ]]; then echo "Error."; cleanup 1; else echo "Done."; fi
|
|
|
|
# Check if downloaded file has <rss> tag
|
|
grep -q '<rss' feed.xml
|
|
if ! [[ $? == "0" ]]; then echo "This is not a RSS feed. Exiting."; cleanup 1; fi
|
|
|
|
|
|
# Find 2nd clean title (1st post) and replaces / with \/
|
|
TITLE=$(awk '/<title>/{++n; if (n==2) {print; exit}}' feed.xml | sed 's/<title>//g; s/<\/title>//g; s/^ *//g')
|
|
|
|
# Find the link of the article
|
|
LINK=$(grep -A1 "$TITLE" feed.xml | tail -n 1 | sed 's/<link>//g; s/<\/link>//g; s/ //g')
|
|
|
|
# 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
|
|
|
|
echo "\nCopy and Paste this:\n\n\
|
|
+++++\n\
|
|
Latest article from my blog: \"$TITLE\"\n\
|
|
$LINK
|
|
+++++"
|
|
|
|
cleanup 0 |