#!/bin/bash
#
# getrss
#
# A command line rss reader.
#
# v1.1
#
# Known Bugs:
#
# An RSS URL that includes an equal sign will not get rendered correctly
# if stored as a bookmark, as I'm currently using that to parse the config file
#
# Even though I've tried to eliminate this through terse regular expressions,
# some exotic RSS feeds may not render correctly if they use obscure or
# elaborate markup methods.
#
# Soon to be added is a way to list your bookmarks. Until then, you can use
# this command: cat ~/.getrssrc | grep ARRTITLE | awk -F= '{print $2}'
#
# There is currently no way to delete bookmarks automatically. If you do it by
# hand, then you must renumber all the subsequent bookmarks, or you will have
# numbering issues when you add a new bookmark, because it counts the number of
# them it finds, rather than incrementing the last one in the list. This
# is trivial to fix, I just haven't done it yet.
#
# Please let me know of any bugs you find at the address below.
# Many thanks,
#
# Matt Simmons
# bandman@gmail.com
# 7/20/06
if [ "$1" == "" ] ; then
echo "Usage: $0 [OPTION] [URL] [LINK NUMBER | <-a>]
"
echo "Display an RSS feed live from the internet"
echo ""
echo "Options:"
echo " - Display a bookmarked feed"
echo " Note the need to prefix the bookmark with a -"
echo ""
echo "[URL] The uniform resource locator"
echo "An example is http://www.thinkgeek.com/thinkgeek.rss"
echo ""
echo "[LINK NUMBER] Browse to the given link"
echo " in Firefox, or your default browser"
echo "<-a> Tells getrss that you want to add [URL] "
echo " to your bookmarks. "
echo " Is the title you want to assign your bookmark"
echo " Notice that you must yuse if you use <-a>"
echo ""
exit
fi
if [ "$2" == "-a" ] ; then
if [ "$3" == "" ] ; then
echo "Error parsing command line."
echo "Please run without options for usage"
exit
fi
ADDBOOKMARK=$3
ADDLINK=$1
fi
if [ "`echo ${1:0:1}`" == "-" ] ; then
BOOKMARK=`echo ${1:1}`
fi
FEEDURL=$1
RETURNLINKS=5
BROWSER=/usr/local/bin/firefox
CONFIGFILE=~/.getrssrc
if [ "$2" != "-a" ] ; then
BROWSETO=$2
fi
# The following block allows listing of bookmarks.
# Contributed by Amy Ginter (wallmalker1@gmail.com)
if [ "$1" == "-l" ] ; then
echo "Existing Bookmarks:"
cat $CONFIGFILE | grep ARRTITLE | cut -d\= -f2 | sed 's/^/ -/g'
exit
fi
TMPFILE=/tmp/tmprss.`date +%s`
####################################################
function createConfig() {
touch "$CONFIGFILE"
echo "
# config file for getrss
# Comments must start on the beginning of the line with the hash symbol
# Do not put comments after config statements
# The number of RSS links you want returned.
RETURNLINKS=5
# Full path to the browser of your choice
BROWSER=/usr/local/bin/firefox
ARRLINK[1]=http://earthquake.usgs.gov/recenteqsww/catalogs/eqs7day-M2.5.xml
ARRTITLE[1]=usgs
ARRLINK[2]=http://www.fark.com/fark.rss
ARRTITLE[2]=fark
ARRLINK[3]=http://rss.slashdot.org/Slashdot/slashdot
ARRTITLE[3]=slashdot
ARRTITLE[4]=thinkgeek
ARRLINK[4]=http://www.thinkgeek.com/thinkgeek.rss
" > $CONFIGFILE
}
###############
function parseConfig() {
if [ -e $CONFIGFILE ] ; then
TMPCFG="/tmp/tmpcfg.`date +%s`"
cat $CONFIGFILE | grep -v ^$ | grep -v \^\# | sed 's/^[ ]*//g' | sed 's/[ ]*=[ ]*/=/g' > $TMPCFG
LINES=`cat $TMPCFG | wc -l`
THISLINE=1
while [ $THISLINE -le $LINES ] ; do
# Magic
eval `cat $TMPCFG | head -n $THISLINE | tail -n 1`
let THISLINE=THISLINE+1
done
else
createConfig
parseConfig
fi
}
###############
function getTitles () {
curTitle=1
numTitles=`cat $TMPFILE | grep "^.*" | wc -l`
if [ "$numTitles" -gt "$1" ] ; then
let numTitles=$1+3
fi
while [ $curTitle -lt $numTitles ] ; do
if [ $curTitle -gt 2 ] ; then
arrTitles[`expr $curTitle - 2`]="`cat $TMPFILE | sed 's//\n/g' | sed 's/<\/title>/<\/title>\n/g' | grep "^.*" | head -n $curTitle | tail -n 1 | sed 's///g' | sed 's/<\/title>//g' | lynx --stdin --dump -width=1000`"
fi
let curTitle=curTitle+1
done
}
###############
function getLinks () {
curLink=1
numLinks=`cat $TMPFILE | grep "^.*/\n/g' | sed 's/<\/link>/<\/link>\n/g' | grep "^.*" | head -n $curLink | tail -n 1 | sed 's///g' | sed 's/<\/link>//g'`"
arrLinks[`expr $curLink - 2`]="`cat $TMPFILE | grep "//g' | sed 's/<\/link>//g'`"
fi
let curLink=curLink+1
done
}
###############
function processBookmark () {
if [ "`cat $CONFIGFILE | grep "=$BOOKMARK$"`" == "" ] ; then
echo "Error: No bookmark found in $CONFIGFILE"
rm -f $TMPFILE 2> /dev/null
exit
fi
ARRAYNUM=`cat $CONFIGFILE | grep $BOOKMARK | cut -d \[ -f 2 | cut -d \] -f 1`
FEEDURL=`cat $CONFIGFILE | grep "ARRLINK\[$ARRAYNUM\]" | awk -F= '{print $2}'`
}
###############
function addBookmark () {
if [ "`cat $CONFIGFILE | grep "=$ADDBOOKMARK$"`" != "" ] ; then
echo "Error: $ADDBOOKMARK already found in $CONFIGFILE"
rm -f $TMPFILE 2> /dev/null
exit
fi
NEWID=`cat $CONFIGFILE | grep "ARRLINK\[" | wc -l`
let NEWID=NEWID+1
echo "ARRTITLE[$NEWID]=$ADDBOOKMARK" >> $CONFIGFILE
echo "ARRLINK[$NEWID]=$ADDLINK" >> $CONFIGFILE
}
##########################################################################################
parseConfig
if [ "$ADDBOOKMARK" != "" ] ; then
addBookmark
rm -f $TMPFILE 2> /dev/null
exit
fi
if [ "$BOOKMARK" != "" ] ; then
processBookmark
fi
wget -O $TMPFILE $FEEDURL 2>/dev/null
TITLE=`cat $TMPFILE | grep "\" | head -n 1`
DESC=`cat $TMPFILE | grep "\" | head -n 1 | sed 's///g' | sed 's/<\/description>//g'`
echo "$TITLE: $DESC" | sed 's///g '| lynx --stdin --dump
getTitles $RETURNLINKS
getLinks $RETURNLINKS
let RETURNLINKS=RETURNLINKS+1
if [ $BROWSETO ] ; then
echo "Going to: ${arrLinks[$BROWSETO]}"
$BROWSER "${arrLinks[$BROWSETO]}"
echo ""
rm -f $TMPFILE 2> /dev/null
exit
fi
curLink=1
while [ $curLink -lt $RETURNLINKS ] ; do
echo "$curLink. ${arrTitles[$curLink]}" | lynx --stdin --dump | grep -v "^$"
let curLink=curLink+1
done
echo ""
rm -f $TMPFILE