summaryrefslogtreecommitdiffstats
path: root/rss.sh
blob: 60173544a03b33c2c7293d1a994d563c79fe0481 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env bash

###
TEMPSUFFIX="$(od -t x1 -An -N6 /dev/urandom | tr -d '\n ')"
TEMPFILE="/tmp/rss.xml.${TEMPSUFFIX}"

# Make a tempfile for rss feed construction
touch "${TEMPFILE}"

echo "Tempfile at: ${TEMPFILE}"
###

title='Glue Code'
link="https://gluecode.net/blog"
description='Brain Dump'
rsslink="https://gluecode.net/blog/rss.xml"
feedname="/var/www/blog/rss.xml"
postDir="/var/www/blog/posts"

###

header="""
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>${title}</title>
<link>${link}</link>
<description>${description}</description>
<atom:link href='${rsslink}' rel='self' type='application/rss+xml' />
"""

footer="</channel></rss>"

echo $header >> $TEMPFILE

postArray=( $(ls -r "$postDir"/*/*.html) )
numPosts=$(ls "$postDir"/*/*.html | wc -l)
echo "numposts is $numPosts"
for posts in "${postArray[@]}"; do
        post=$posts
        fullTitle=$(grep -o '>.*</h1>' $post | sed -e 's,>,,g' -e 's,</h1,,g')
        postname=${post##*/}
        linkadd="$link"/"$postname"
        guidadd=$linkadd

        LEN=$(wc -l ${post}|awk '{print $1}')
        extract=$(cat $post | head -n $((${LEN}-1)) | tail -n $((${LEN}-3)) )

        echo """<item>
        <title>$fullTitle</title>
        <link>$linkadd</link>
        <guid>$linkadd</guid>
        <description>$fulltitle</description>
        <content type="text/html" mode="escaped">$extract</content>
        </item>
        """ >> $TEMPFILE
done
echo $footer >> $TEMPFILE

mv -v $TEMPFILE $feedname