kapott icon

Ghetto RSS parser in Bash

kapott | PRO | 10/30/17 02:46:22 PM UTC | 0 ⭐ | 300 👁️ | Never ⏰ | []
Bash |

1.07 KB

|

None

|

0 👍

/

0 👎

#!/bin/sh
 
# Get some RSS feed, and parse it to html.
# wget --quiet -O $rsslocalfile $rssfeedurl
# ./parse.sh $rsslocalfile > $newslocalfile.html
 
xmlgetnext () {
   local IFS='>'
   read -d '<' TAG VALUE
}
 
cat $1 | while xmlgetnext ; do
   case $TAG in
      'item')
         title=''
         link=''
         pubDate=''
         description=''
         ;;
      'title')
         title="$VALUE"
         ;;
      'link')
         link="$VALUE"
         ;;
      'pubDate')
         # convert pubDate format for <time datetime="">
         datetime=$( date --date "$VALUE" --iso-8601=minutes )
         pubDate=$( date --date "$VALUE" '+%D %H:%M%P' )
         ;;
      'description')
         # convert '&lt;' and '&gt;' to '<' and '>'
         description=$( echo "$VALUE" | sed -e 's/&lt;/</g' -e 's/&gt;/>/g' )
         ;;
      '/item')
         cat<<EOF
<article>
<h3><a href="$link">$title</a></h3>
<p>$description
<span class="post-date">posted on <time
datetime="$datetime">$pubDate</time></span></p>
</article>
EOF
         ;;
      esac
done

Comments