her.esy.fun/engine/mkrss.sh

108 lines
2.9 KiB
Bash
Raw Normal View History

2020-05-25 20:28:06 +00:00
#!/usr/bin/env zsh
2019-09-24 15:55:59 +00:00
2020-02-10 08:42:17 +00:00
cd "$(git rev-parse --show-toplevel)" || exit 1
2019-09-30 13:10:39 +00:00
# Directory
2021-05-06 22:21:41 +00:00
webdir="_site"
2019-09-30 07:54:53 +00:00
postsdir="$webdir/posts"
2019-09-24 15:55:59 +00:00
rssfile="$webdir/rss.xml"
2021-05-07 16:02:58 +00:00
indexdir=".cache/rss"
2019-09-24 15:55:59 +00:00
2019-09-30 13:10:39 +00:00
# maximal number of articles to put in the RSS file
maxarticles=10
# RSS Metas
rsstitle="her.esy.fun"
rssurl="https://her.esy.fun/rss.xml"
websiteurl="https://her.esy.fun"
rssdescription="her.esy.fun articles, mostly random personal thoughts"
rsslang="en"
rssauthor="yann@esposito.host (Yann Esposito)"
rssimgurl="https://her.esy.fun/img/FlatAvatar.png"
# HTML Accessors (similar to CSS accessors)
2021-05-07 16:02:58 +00:00
dateaccessor='pubDate'
2019-09-24 15:55:59 +00:00
formatdate() {
2019-09-30 13:10:39 +00:00
# format the date for RSS
2021-04-27 12:34:29 +00:00
local d="$1"
# echo "DEBUG DATE: $d" >&2
2019-09-24 15:55:59 +00:00
LC_TIME=en_US date --date $d +'%a, %d %b %Y %H:%M:%S %z'
}
2019-09-30 13:10:39 +00:00
2021-05-07 16:02:58 +00:00
isodate() {
# format the date for sorting
local d="$1"
2021-05-25 14:27:58 +00:00
# echo "DEBUG DATE: $d" >&2
2021-05-08 08:07:10 +00:00
LC_TIME=en_US date --date "$d" +'%Y-%m-%dT%H:%M:%S'
2019-09-30 07:54:53 +00:00
}
2019-09-24 15:55:59 +00:00
2021-05-08 08:07:10 +00:00
finddate(){ < $1 hxselect -c $dateaccessor }
2021-05-07 16:02:58 +00:00
2019-09-25 10:11:24 +00:00
autoload -U colors && colors
2019-09-30 13:10:39 +00:00
typeset -a dates
dates=( )
2021-05-07 16:02:58 +00:00
tmpdir=$(mktemp -d)
2021-05-08 08:07:10 +00:00
for fic in $indexdir/**/*.rss; do
rssdate=$(finddate $fic)
2021-05-25 14:27:58 +00:00
echo -n "${${fic:h}:t} [$rssdate]"
2021-05-07 16:02:58 +00:00
d=$(isodate $rssdate)
2019-09-30 13:10:39 +00:00
dates=( $d $dates )
2019-09-25 10:11:24 +00:00
echo " [${fg[green]}OK${reset_color}]"
2021-05-08 08:07:10 +00:00
cp $fic $tmpdir/$d-${${fic:h}:t}.rss
2019-09-25 10:11:24 +00:00
done
2019-09-30 13:10:39 +00:00
echo "Publishing"
2021-05-08 08:07:10 +00:00
n=1
2019-09-30 13:10:39 +00:00
for fic in $(ls $tmpdir/*.rss | sort -r | head -n $maxarticles ); do
2021-05-08 08:07:10 +00:00
echo "$((n++)) ${fic:t}"
2019-09-25 10:11:24 +00:00
cat $fic >> $tmpdir/rss
2019-09-24 15:55:59 +00:00
done
2019-09-30 13:10:39 +00:00
rssmaxdate=$(formatdate $(for d in $dates; do echo $d; done | sort -r | head -n 1))
rssbuilddate=$(formatdate $(date))
{
cat <<END
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
xmlns:georss="http://www.georss.org/georss"
xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
xmlns:media="http://search.yahoo.com/mrss/"><channel>
<title>${rsstitle}</title>
<atom:link href="${rssurl}" rel="self" type="application/rss+xml" />
<link>${websiteurl}</link>
<description><![CDATA[${rssdescription}]]></description>
<language>${rsslang}</language>
<pubDate>${rssmaxdate}</pubDate>
<lastBuildDate>$rssbuilddate</lastBuildDate>
<generator>mkrss.sh</generator>
<webMaster>${rssauthor}</webMaster>
<image>
<url>${rssimgurl}</url>
<title>${rsstitle}</title>
2019-09-30 13:10:39 +00:00
<link>${websiteurl}</link>
</image>
END
cat $tmpdir/rss
cat <<END
</channel>
</rss>
END
} > "$rssfile"
2021-05-02 08:32:40 +00:00
# HACK TO UPDATE OLD RSS FEEDS
legacyenrss="$webdir/Scratch/en/blog/feed/feed.xml"
legacyfrrss="$webdir/Scratch/fr/blog/feed/feed.xml"
2021-05-08 08:07:10 +00:00
mkdir -p "${legacyenrss:h}"
mkdir -p "${legacyfrrss:h}"
2021-05-02 08:32:40 +00:00
cp -f "$rssfile" "$legacyenrss"
cp -f "$rssfile" "$legacyfrrss"
2019-09-25 10:11:24 +00:00
rm -rf $tmpdir
2019-12-22 23:01:06 +00:00
echo "* RSS [done]"