Last active
July 4, 2020 13:44
-
-
Save nunq/d5b7213c2b72c2504f50290116314c11 to your computer and use it in GitHub Desktop.
manage an rss feed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -eu -o pipefail | |
FEEDTITLE="" # feed title (as displayed in rss readers) | |
FEEDLINK="" # link to the .rss file, publicly accesible | |
RSSLINKTO="" # where the feed links to (ex: website) | |
FEEDIMAGEURL="" # use this as the feed image | |
FEEDDESCRIPTION="" # short feed description | |
RSSFILE="./feed.rss" | |
RSSDATE="$(date +%a,-%d-%b-%Y-%T-%z | sed -e 's/-/ /g')" | |
insertnewitem() { | |
ITEMTITLE="$1" | |
ITEMCONTENT="$2" | |
ENCLURL="$3" | |
ENCLLEN="$4" | |
ENCLMIME="$5" | |
sed -i '/<\/lastBuildDate>/a\\t<item>\n\t\t<title>'"$ITEMTITLE"'<\/title>\n\t\t<guid isPermaLink=\"false\">'"$(date +%s)"'<\/guid>\n\t\t<link>'"$RSSLINKTO"'<\/link>\n\t\t<description><![CDATA[<p>'"$ITEMCONTENT"'<\/p>]]><\/description>\n\t\t<enclosure url="'"$ENCLURL"'" length="'"$ENCLLEN"'" type="'"$ENCLMIME"'"/>\n\t\t<pubDate>'"$RSSDATE"'</pubDate>\n\t<\/item>' "$RSSFILE" | |
sed -i "s/<lastBuildDate>.*<\/lastBuildDate>/<lastBuildDate>$RSSDATE<\/lastBuildDate>/gi" "$RSSFILE" | |
# implement websub for realtime updates | |
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "hub.mode=publish" -d "hub.url=""$FEEDLINK" "https://pubsubhubbub.appspot.com/" | |
sleep 1 # avoid guid collisions | |
} | |
rsscleanup() { | |
# VERY hacky way to cleanup up rss items older than 3 days | |
mapfile -t guids <<< "$(grep -oP '(?<=guid isPermaLink="false">).*(?=</guid>)' "$RSSFILE")" | |
for guid in "${guids[@]}"; do | |
if [ "$guid" -lt $(( $(date +%s) - 259200 )) ]; then # if older than 3 days | |
linenum=$(grep -n "$guid" "$RSSFILE" | sed 's/:.*//gi') | |
upperlim=$(( $linenum - 2 )) | |
lowerlim=$(( $linenum + 5 )) | |
sed -i ''"$upperlim"','"$lowerlim"'d' "$RSSFILE" | |
fi | |
done | |
} | |
updatefeed() { | |
insertnewitem "title" "description" "https://www.gmkfreelogos.com/logos/A/img/america_online.gif" "2401" "image/gif" | |
rsscleanup | |
} | |
createfeed() { | |
echo "$RSSBOILERPLATE" > "$RSSFILE" || exit 1 | |
echo "created rss feed at "$RSSFILE"" | |
} | |
RSSBOILERPLATE='<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0"> | |
<channel> | |
<title>'"$FEEDTITLE"'</title> | |
<link rel="self" href="'"$FEEDLINK"'"/> | |
<link rel="hub" href="https://pubsubhubbub.appspot.com/"/> | |
<description>'"$FEEDDESCRIPTION"'</description> | |
<image><url>'"$FEEDIMAGEURL"'</url> | |
<link>'"$RSSLINKTO"'</link> | |
<title>'"$FEEDTITLE"'</title> | |
</image> | |
<atom:link href='"$RSSLINKTO"' rel="self"/> | |
<lastBuildDate>'"$RSSDATE"'</lastBuildDate> | |
<item> | |
<title>first entry</title> | |
<link>https://127.0.0.1</link> | |
<description>example content</description> | |
<pubDate>'"$RSSDATE"'</pubDate> | |
</item> | |
</channel> | |
</rss>' | |
case "$1" in | |
create) createfeed ;; | |
update) updatefeed ;; | |
*) echo "args: create||update" | |
exit 1 ;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment