kapott

Ghetto RSS parser in Bash

Oct 30th, 2017
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.07 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # Get some RSS feed, and parse it to html.
  4. # wget --quiet -O $rsslocalfile $rssfeedurl
  5. # ./parse.sh $rsslocalfile > $newslocalfile.html
  6.  
  7. xmlgetnext () {
  8.    local IFS='>'
  9.    read -d '<' TAG VALUE
  10. }
  11.  
  12. cat $1 | while xmlgetnext ; do
  13.    case $TAG in
  14.       'item')
  15.          title=''
  16.          link=''
  17.          pubDate=''
  18.          description=''
  19.          ;;
  20.       'title')
  21.          title="$VALUE"
  22.          ;;
  23.       'link')
  24.          link="$VALUE"
  25.          ;;
  26.       'pubDate')
  27.          # convert pubDate format for <time datetime="">
  28.          datetime=$( date --date "$VALUE" --iso-8601=minutes )
  29.          pubDate=$( date --date "$VALUE" '+%D %H:%M%P' )
  30.          ;;
  31.       'description')
  32.          # convert '&lt;' and '&gt;' to '<' and '>'
  33.          description=$( echo "$VALUE" | sed -e 's/&lt;/</g' -e 's/&gt;/>/g' )
  34.          ;;
  35.       '/item')
  36.          cat<<EOF
  37. <article>
  38. <h3><a href="$link">$title</a></h3>
  39. <p>$description
  40. <span class="post-date">posted on <time
  41. datetime="$datetime">$pubDate</time></span></p>
  42. </article>
  43. EOF
  44.          ;;
  45.       esac
  46. done
Advertisement
Add Comment
Please, Sign In to add comment