You are not logged in.

#1 2012-08-11 01:07:41

grufo
Member
Registered: 2012-08-09
Posts: 100

A simple but well coloured feed reader written in bash

Hello everyone.
I wrote these two functions for my .bashrc:

#!/bin/bash
if [ ! -n "$FEED_BOOKMARKS" ]; then export FEED_BOOKMARKS=$HOME/.feed_bookmarks; fi
if [ ! -d "$FEED_BOOKMARKS" ]; then mkdir -p $FEED_BOOKMARKS; fi

feed() {
	if [ ! -d $FEED_BOOKMARKS ]; then mkdir $FEED_BOOKMARKS; fi

	if [ ! -n "$1" ]; then
		echo -e "\\n \\e[04mUsage\\e[00m\\n\\n   \\e[01;37m\$ feed \\e[01;31m<url>\\e[00m \\e[01;31m<new bookmark?>\\e[00m\\n\\n \\e[04mSee also\\e[00m\\n\\n   \\e[01;37m\$ deef\\e[00m\\n"
		return 1;
	fi

	local rss_source="$(curl --silent $1 | sed -e ':a;N;$!ba;s/\n/ /g')";

	if [ ! -n "$rss_source" ]; then
		echo "The feed is empty";
		return 1;
	fi

	# THE RSS PARSER
	# The characters "£, §" are used as metacharacters. They should not be encountered in a feed...
	echo -e "$(echo $rss_source | \
		sed -e 's/&gt;/>/g' \
		-e 's/&lt;/</g' \
		-e 's/<\/a>/£/g' \
		-e 's/href\=\"/§/g' \
		-e 's/<title>/\\n\\n\\n   :: \\e[01;31m/g' -e 's/<\/title>/\\e[00m ::\\n/g' \
		-e 's/<link>/ [ \\e[01;36m/g' -e 's/<\/link>/\\e[00m ]/g' \
		-e 's/<description>/\\n\\n\\e[00;37m/g' -e 's/<\/description>/\\e[00m\\n\\n/g' \
		-e 's/<p>\|<br\s*\/\?>/\n/g' \
		-e 's/<b>\|<strong>/\\e[01;30m/g' -e 's/<\/b>\|<\/strong>/\\e[00;37m/g' \
		-e 's/<u>/\\e[4;37m/g' -e 's/<\/u>/\\e[00;37m/g' \
		-e 's/<b>\|<code>/\\e[00m/g' -e 's/<\/b>\|<\/code>/\\e[00;37m/g' \
		-e 's/<a[^§]*§\([^\"]*\)\"[^>]*>\([^£]*\)[^£]*£/\\e[01;31m\2\\e[00;37m \\e[01;34m[\\e[00;37m \\e[04m\1\\e[00;37m\\e[01;34m ]\\e[00;37m/g' \
		-e 's/<li>/\n \\e[01;34m*\\e[00;37m /g' \
		-e 's/<!\[CDATA\[\|\]\]>\|>\s*<//g' \
		-e 's/<[^>]*>/ /g' \
		-e 's/[<>£§]//g')\n\n";
	# END OF THE RSS PARSER

	if [ -n "$2" ]; then
		echo "$1" > $FEED_BOOKMARKS/$2
		echo -e "\\n\\t\\e[01;37m==> \\e[01;31mBookmark saved as \\e[01;36m\\e[04m$2\\e[00m\\e[01;37m <==\\e[00m\\n"
	fi
}

deef() {
	if test -n "$1"; then
		if [ ! -r "$FEED_BOOKMARKS/$1" ]; then
			echo -e "\\n \\e[01;31mBookmark \\e[01;36m\\e[04m$1\\e[00m\\e[01;31m not found.\\e[00m\\n\\n \\e[04mType:\\e[00m\\n\\n   \\e[01;37m\$ deef\\e[00m (without arguments)\\n\\n to get the complete list of all currently saved bookmarks.\\n";
			return 1;
		fi
		local url="$(cat $FEED_BOOKMARKS/$1)";
		if [ ! -n "$url" ]; then
			echo "The bookmark is empty";
			return 1;
		fi
		echo -e "\\n\\t\\e[01;37m==> \\e[01;31m$url\\e[01;37m <==\\e[00m"
		feed "$url";
	else
		echo -e "\\n \\e[04mUsage\\e[00m\\n\\n   \\e[01;37m\$ deef \\e[01;31m<bookmark>\\e[00m\\n\\n \\e[04mCurrently saved bookmarks\\e[00m\\n";
		for i in $(find $FEED_BOOKMARKS -maxdepth 1 -type f);
			do echo -e "   \\e[01;36m\\e[04m$(basename $i)\\e[00m";
		done;
		echo -e "\\n \\e[04mSee also\\e[00m\\n\\n   \\e[01;37m\$ feed\\e[00m\\n";
	fi;
}

It's a very simple rss reader written in bash with two functions.

The first one:

$ feed <url> <new bookmark?>

prints the parsed content of <url> and possibly save the url as <new bookmark?>, if specified.
For example:

$ feed http://www.archlinux.org/feeds/news/

or

$ feed http://www.archlinux.org/feeds/news/ archnews

The second one:

$ deef <bookmark>

reads the url previously saved as <bookmark> and prints its content.
For example:

$deef archnews

I'd like you to test it and suggest whatever you want smile
The parser is very very simple.

**EDIT**
A better version of this script is this.

Last edited by grufo (2012-08-15 02:41:19)

Offline

#2 2012-08-11 01:18:32

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: A simple but well coloured feed reader written in bash

Cool idea!

But holy sed pipes batman ... I think this would be a perfect place to get a little practice with awk.  One awk invocation could replace all those seds and remove the need for the double nested echos.  If you aren't familiar with awk, then at least you could use sed's "-e" parameter to use one invocation of sed rather than piping through a dozen independent processes (each one requiring the loading and unloading of sed).


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2012-08-11 01:22:51

grufo
Member
Registered: 2012-08-09
Posts: 100

Re: A simple but well coloured feed reader written in bash

Post your code suggestions! :-)
I'm also not fully persuaded about the colors I choose...

Last edited by grufo (2012-08-11 01:26:16)

Offline

#4 2012-08-11 01:41:24

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: A simple but well coloured feed reader written in bash

It would take a bit to translate it into awk, but the sed "-e" option would look like the following excerpt:

echo -e "$(echo $rss_source |  sed -e \
		's/&gt;/>/g' -e \
		's/&lt;/</g'  -e \
		's/<\/a>/£/g' -e\
		's/href\=\"/§/g' -e\
...

"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#5 2012-08-11 01:49:06

grufo
Member
Registered: 2012-08-09
Posts: 100

Re: A simple but well coloured feed reader written in bash

Thnx. I've updated it smile

Last edited by grufo (2012-08-11 01:50:00)

Offline

#6 2012-08-11 02:09:14

grufo
Member
Registered: 2012-08-09
Posts: 100

Re: A simple but well coloured feed reader written in bash

I have no ideas on how to render the italic style. Suggestions?

Offline

#7 2012-08-11 06:55:03

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: A simple but well coloured feed reader written in bash

You don't need -e, you can just enclose all the expressions in one script:

echo -e "$(echo $rss_source |  sed 's/&gt;/>/g
		s/&lt;/</g
		s/<\/a>/£/g
		s/href\=\"/§/g
...
'

sed applies every line that matches the address pattern. No address pattern and it tries to apply the substitution to every line.


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#8 2012-08-11 15:15:49

grufo
Member
Registered: 2012-08-09
Posts: 100

Re: A simple but well coloured feed reader written in bash

It doesn't work in this way...

Offline

#9 2012-08-11 15:54:06

grufo
Member
Registered: 2012-08-09
Posts: 100

Re: A simple but well coloured feed reader written in bash

I added a red background for italic style and also enforced the parsing process with more possibilities...

#!/bin/bash
if [ ! -n "$FEED_BOOKMARKS" ]; then export FEED_BOOKMARKS=$HOME/.feed_bookmarks; fi
if [ ! -d "$FEED_BOOKMARKS" ]; then mkdir -p $FEED_BOOKMARKS; fi

feed() {
	if [ ! -d $FEED_BOOKMARKS ]; then mkdir $FEED_BOOKMARKS; fi

	if [ ! -n "$1" ]; then
		echo -e "\\n \\e[04mUsage\\e[00m\\n\\n   \\e[01;37m\$ feed \\e[01;31m<url>\\e[00m \\e[01;31m<new bookmark?>\\e[00m\\n\\n \\e[04mSee also\\e[00m\\n\\n   \\e[01;37m\$ deef\\e[00m\\n"
		return 1;
	fi

	local rss_source="$(curl --silent $1 | sed -e ':a;N;$!ba;s/\n/ /g')";

	if [ ! -n "$rss_source" ]; then
		echo "The feed is empty";
		return 1;
	fi

	# THE RSS PARSER
	# The characters "£, §" are used as metacharacters. They should not be encountered in a feed...
	echo -e "$(echo $rss_source | \
		sed -e 's/&amp;/\&/g' \
		-e 's/&lt;\|&#60;/</g' \
		-e 's/&gt;\|&#62;/>/g' \
		-e 's/<\/a>/£/g' \
		-e 's/href\=\"/§/g' \
		-e 's/<title>/\\n\\n\\n   :: \\e[01;31m/g' -e 's/<\/title>/\\e[00m ::\\n/g' \
		-e 's/<link>/ [ \\e[01;36m/g' -e 's/<\/link>/\\e[00m ]/g' \
		-e 's/<description>/\\n\\n\\e[00;37m/g' -e 's/<\/description>/\\e[00m\\n\\n/g' \
		-e 's/<p\( [^>]*\)\?>\|<br\s*\/\?>/\n/g' \
		-e 's/<b\( [^>]*\)\?>\|<strong\( [^>]*\)\?>/\\e[01;30m/g' -e 's/<\/b>\|<\/strong>/\\e[00;37m/g' \
		-e 's/<i\( [^>]*\)\?>\|<em\( [^>]*\)\?>/\\e[41;37m/g' -e 's/<\/i>\|<\/em>/\\e[00;37m/g' \
		-e 's/<u\( [^>]*\)\?>/\\e[4;37m/g' -e 's/<\/u>/\\e[00;37m/g' \
		-e 's/<code\( [^>]*\)\?>/\\e[00m/g' -e 's/<\/code>/\\e[00;37m/g' \
		-e 's/<a[^§]*§\([^\"]*\)\"[^>]*>\([^£]*\)[^£]*£/\\e[01;31m\2\\e[00;37m \\e[01;34m[\\e[00;37m \\e[04m\1\\e[00;37m\\e[01;34m ]\\e[00;37m/g' \
		-e 's/<li\( [^>]*\)\?>/\n \\e[01;34m*\\e[00;37m /g' \
		-e 's/<!\[CDATA\[\|\]\]>//g' \
		-e 's/\|>\s*<//g' \
		-e 's/ *<[^>]\+> */ /g' \
		-e 's/[<>£§]//g')\n\n";
	# END OF THE RSS PARSER

	if [ -n "$2" ]; then
		echo "$1" > $FEED_BOOKMARKS/$2
		echo -e "\\n\\t\\e[01;37m==> \\e[01;31mBookmark saved as \\e[01;36m\\e[04m$2\\e[00m\\e[01;37m <==\\e[00m\\n"
	fi
}

deef() {
	if test -n "$1"; then
		if [ ! -r "$FEED_BOOKMARKS/$1" ]; then
			echo -e "\\n \\e[01;31mBookmark \\e[01;36m\\e[04m$1\\e[00m\\e[01;31m not found.\\e[00m\\n\\n \\e[04mType:\\e[00m\\n\\n   \\e[01;37m\$ deef\\e[00m (without arguments)\\n\\n to get the complete list of all currently saved bookmarks.\\n";
			return 1;
		fi
		local url="$(cat $FEED_BOOKMARKS/$1)";
		if [ ! -n "$url" ]; then
			echo "The bookmark is empty";
			return 1;
		fi
		echo -e "\\n\\t\\e[01;37m==> \\e[01;31m$url\\e[01;37m <==\\e[00m"
		feed "$url";
	else
		echo -e "\\n \\e[04mUsage\\e[00m\\n\\n   \\e[01;37m\$ deef \\e[01;31m<bookmark>\\e[00m\\n\\n \\e[04mCurrently saved bookmarks\\e[00m\\n";
		for i in $(find $FEED_BOOKMARKS -maxdepth 1 -type f);
			do echo -e "   \\e[01;36m\\e[04m$(basename $i)\\e[00m";
		done;
		echo -e "\\n \\e[04mSee also\\e[00m\\n\\n   \\e[01;37m\$ feed\\e[00m\\n";
	fi;
}

Last edited by grufo (2012-08-11 17:06:33)

Offline

#10 2012-08-11 16:31:04

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: A simple but well coloured feed reader written in bash

grufo wrote:

It doesn't work in this way...

That'll teach me to answer programming stuff on my phone. I'll try again when on my laptop. Sorry.


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#11 2012-08-11 17:45:03

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: A simple but well coloured feed reader written in bash

Right, this worked for me:

 echo -e "$(echo $rss_source | \
                sed 's/&amp;/\&/g
                s/&lt;\|&#60;/</g
                s/&gt;\|&#62;/>/g
                s/<\/a>/£/g
                s/href\=\"/§/g
                s/<title>/\\n\\n\\n   :: \\e[01;31m/g
                s/<\/title>/\\e[00m ::\\n/g
                s/<link>/ [ \\e[01;36m/g
                s/<\/link>/\\e[00m ]/g
                s/<description>/\\n\\n\\e[00;37m/g
                s/<\/description>/\\e[00m\\n\\n/g
                s/<p\( [^>]*\)\?>\|<br\s*\/\?>/\n/g
                s/<b\( [^>]*\)\?>\|<strong\( [^>]*\)\?>/\\e[01;30m/g
                s/<\/b>\|<\/strong>/\\e[00;37m/g
                s/<i\( [^>]*\)\?>\|<em\( [^>]*\)\?>/\\e[41;37m/g
                s/<\/i>\|<\/em>/\\e[00;37m/g
                s/<u\( [^>]*\)\?>/\\e[4;37m/g
                s/<\/u>/\\e[00;37m/g
                s/<code\( [^>]*\)\?>/\\e[00m/g
                s/<\/code>/\\e[00;37m/g
                s/<a[^§]*§\([^\"]*\)\"[^>]*>\([^£]*\)[^£]*£/\\e[01;31m\2\\e[00;37m \\e[01;34m[\\e[00;37m \\e[04m\1\\e[00;37m\\e[01;34m ]\\e[00;37m/g
                s/<li\( [^>]*\)\?>/\n \\e[01;34m*\\e[00;37m /g
                s/<!\[CDATA\[\|\]\]>//g
                s/\|>\s*<//g
                s/ *<[^>]\+> */ /g
                s/[<>£§]//g')\n\n";

Note, I may have made a mistake so the formatting may not be perfect, but the output was generally good.

If you want to put two items on the same line, they should be able to just be separated by a ;

Last edited by skanky (2012-08-11 17:45:46)


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#12 2012-08-11 20:06:01

grufo
Member
Registered: 2012-08-09
Posts: 100

Re: A simple but well coloured feed reader written in bash

skanky wrote:

Right, this worked for me:

Great! smile
This is the script with your suggestions:

#!/bin/bash
if [ ! -n "$FEED_BOOKMARKS" ]; then export FEED_BOOKMARKS=$HOME/.feed_bookmarks; fi
if [ ! -d "$FEED_BOOKMARKS" ]; then mkdir -p $FEED_BOOKMARKS; fi

feed() {
	if [ ! -d $FEED_BOOKMARKS ]; then mkdir $FEED_BOOKMARKS; fi

	if [ ! -n "$1" ]; then
		echo -e "\\n \\e[04mUsage\\e[00m\\n\\n   \\e[01;37m\$ feed \\e[01;31m<url>\\e[00m \\e[01;31m<new bookmark?>\\e[00m\\n\\n \\e[04mSee also\\e[00m\\n\\n   \\e[01;37m\$ deef\\e[00m\\n"
		return 1;
	fi

	local rss_source="$(curl --silent $1 | sed -e ':a;N;$!ba;s/\n/ /g')";

	if [ ! -n "$rss_source" ]; then
		echo "The feed is empty";
		return 1;
	fi

	# THE RSS PARSER
	# The characters "£, §" are used as metacharacters. They should not be encountered in a feed...
	echo -e "$(echo $rss_source | \
		sed -e 's/&amp;/\&/g
		s/&lt;\|&#60;/</g
		s/&gt;\|&#62;/>/g
		s/<\/a>/£/g
		s/href\=\"/§/g
		s/<title>/\\n\\n\\n   :: \\e[01;31m/g; s/<\/title>/\\e[00m ::\\n/g
		s/<link>/ [ \\e[01;36m/g; s/<\/link>/\\e[00m ]/g
		s/<description>/\\n\\n\\e[00;37m/g; s/<\/description>/\\e[00m\\n\\n/g
		s/<p\( [^>]*\)\?>\|<br\s*\/\?>/\n/g
		s/<b\( [^>]*\)\?>\|<strong\( [^>]*\)\?>/\\e[01;30m/g; s/<\/b>\|<\/strong>/\\e[00;37m/g
		s/<i\( [^>]*\)\?>\|<em\( [^>]*\)\?>/\\e[41;37m/g; s/<\/i>\|<\/em>/\\e[00;37m/g
		s/<u\( [^>]*\)\?>/\\e[4;37m/g; s/<\/u>/\\e[00;37m/g
		s/<code\( [^>]*\)\?>/\\e[00m/g; s/<\/code>/\\e[00;37m/g
		s/<a[^§]*§\([^\"]*\)\"[^>]*>\([^£]*\)[^£]*£/\\e[01;31m\2\\e[00;37m \\e[01;34m[\\e[00;37m \\e[04m\1\\e[00;37m\\e[01;34m ]\\e[00;37m/g
		s/<li\( [^>]*\)\?>/\n \\e[01;34m*\\e[00;37m /g
		s/<!\[CDATA\[\|\]\]>//g
		s/\|>\s*<//g
		s/ *<[^>]\+> */ /g
		s/[<>£§]//g')\n\n";
	# END OF THE RSS PARSER

	if [ -n "$2" ]; then
		echo "$1" > $FEED_BOOKMARKS/$2
		echo -e "\\n\\t\\e[01;37m==> \\e[01;31mBookmark saved as \\e[01;36m\\e[04m$2\\e[00m\\e[01;37m <==\\e[00m\\n"
	fi
}

deef() {
	if test -n "$1"; then
		if [ ! -r "$FEED_BOOKMARKS/$1" ]; then
			echo -e "\\n \\e[01;31mBookmark \\e[01;36m\\e[04m$1\\e[00m\\e[01;31m not found.\\e[00m\\n\\n \\e[04mType:\\e[00m\\n\\n   \\e[01;37m\$ deef\\e[00m (without arguments)\\n\\n to get the complete list of all currently saved bookmarks.\\n";
			return 1;
		fi
		local url="$(cat $FEED_BOOKMARKS/$1)";
		if [ ! -n "$url" ]; then
			echo "The bookmark is empty";
			return 1;
		fi
		echo -e "\\n\\t\\e[01;37m==> \\e[01;31m$url\\e[01;37m <==\\e[00m"
		feed "$url";
	else
		echo -e "\\n \\e[04mUsage\\e[00m\\n\\n   \\e[01;37m\$ deef \\e[01;31m<bookmark>\\e[00m\\n\\n \\e[04mCurrently saved bookmarks\\e[00m\\n";
		for i in $(find $FEED_BOOKMARKS -maxdepth 1 -type f);
			do echo -e "   \\e[01;36m\\e[04m$(basename $i)\\e[00m";
		done;
		echo -e "\\n \\e[04mSee also\\e[00m\\n\\n   \\e[01;37m\$ feed\\e[00m\\n";
	fi;
}

Last edited by grufo (2012-08-11 20:08:15)

Offline

#13 2012-08-12 07:40:13

debdj
Member
Registered: 2012-01-19
Posts: 163

Re: A simple but well coloured feed reader written in bash

Very nice script mate!
I'm going to use it.

I've a suggestion. You could assign the color values to variables and use them throughout the script. That'd enhance the readability of the script, also it would be easier to modify later or users can assign their own colors of choice to the variables.

Last edited by debdj (2012-08-12 07:44:50)

Offline

#14 2012-08-12 09:24:45

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: A simple but well coloured feed reader written in bash

This is great, thanks!  I certainly like using newsbeuter, but the simplicity of this is amazing.

Offline

#15 2012-08-12 11:36:44

grufo
Member
Registered: 2012-08-09
Posts: 100

Re: A simple but well coloured feed reader written in bash

debdj wrote:

Very nice script mate!
I'm going to use it.

I've a suggestion. You could assign the color values to variables and use them throughout the script. That'd enhance the readability of the script, also it would be easier to modify later or users can assign their own colors of choice to the variables.

You are right. But unfortunately this was only an "experiment" and I have no idea on how to put the result of a variable inside a sed script. Anyone can help me? smile

WonderWoofy wrote:

This is great, thanks!  I certainly like using newsbeuter, but the simplicity of this is amazing.

^__^

Offline

#16 2012-08-12 20:30:18

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: A simple but well coloured feed reader written in bash

grufo wrote:

You are right. But unfortunately this was only an "experiment" and I have no idea on how to put the result of a variable inside a sed script. Anyone can help me? smile

Break the '' :-

sed 's/abc/'$VARIABLE'/'

You can split the sed script to a separate file, which would mean that you could have different types of file or streams that could be converted...should you wish. wink


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#17 2012-08-13 09:27:22

debdj
Member
Registered: 2012-01-19
Posts: 163

Re: A simple but well coloured feed reader written in bash

Here.
I just replaced the color codes with variables in the parser. Others are intact. Working for me.

#!/bin/bash -x

#black=`tput setaf 0`
red=`tput setaf 1`
#green=`tput setaf 2`
#yellow=`tput setaf 3`
blue=`tput setaf 4`
#magenta=`tput setaf 5`
cyan=`tput setaf 6`
white=`tput setaf 7`
norm=`tput sgr0`

bold=`tput bold`
ulbegin=`tput smul`
ulend=`tput rmul`

title="$bold$red"
text="$white"
link="$bold$cyan"
brace="$bold$blue"  #also for lists
embedul="$ulbegin$white"
#alert="$bold$white"
#strong="$bold$black"

if [ ! -n "$FEED_BOOKMARKS" ]; then export FEED_BOOKMARKS=$HOME/.feed_bookmarks; fi
if [ ! -d "$FEED_BOOKMARKS" ]; then mkdir -p $FEED_BOOKMARKS; fi

feed() {
	if [ ! -d $FEED_BOOKMARKS ]; then mkdir $FEED_BOOKMARKS; fi

	if [ ! -n "$1" ]; then
		echo -e "\\n \\e[04mUsage\\e[00m\\n\\n   \\e[01;37m\$ feed $title<url>\\e[00m $title<new bookmark?>\\e[00m\\n\\n \\e[04mSee also\\e[00m\\n\\n   \\e[01;37m\$ deef\\e[00m\\n"
		return 1;
	fi

	local rss_source="$(curl --silent $1 | sed -e ':a;N;$!ba;s/\n/ /g')";

	if [ ! -n "$rss_source" ]; then
		echo "The feed is empty";
		return 1;
	fi

	# THE RSS PARSER
	# The characters "£, §" are used as metacharacters. They should not be encountered in a feed...
	echo -e "$(echo $rss_source | \
		sed -e "s/&gt;/>/g" \
		-e "s/&lt;/</g" \
		-e "s/<\/a>/£/g" \
		-e "s/href\=\"/§/g" \
		-e "s/<title>/\\n\\n\\n   :: $title/g" -e "s/<\/title>/$norm ::\\n/g" \
		-e "s/<link>/ [ $link/g" -e "s/<\/link>/$norm ]/g" \
		-e "s/<description>/\\n\\n$text/g" -e "s/<\/description>/$norm\\n\\n/g" \
		-e "s/<p>\|<br\s*\/\?>/\n/g" \
		-e "s/<b>\|<strong>/\\e[01;30m/g" -e "s/<\/b>\|<\/strong>/$text/g" \
		-e "s/<u>/$embedul/g" -e "s/<\/u>/$norm$text/g" \
		-e "s/<b>\|<code>/$norm/g" -e "s/<\/b>\|<\/code>/$text/g" \
		-e "s/<a[^§]*§\([^\"]*\)\"[^>]*>\([^£]*\)[^£]*£/$title\2$text $brace[$norm$text $ulbegin\1$ulend$brace ]$norm$text/g" \
		-e "s/<li>/\n $brace*$norm$text /g" \
		-e "s/<!\[CDATA\[\|\]\]>\|>\s*<//g" \
		-e "s/<[^>]*>/ /g" \
		-e "s/[<>£§]//g")\n\n";
	# END OF THE RSS PARSER

	if [ -n "$2" ]; then
		echo "$1" > $FEED_BOOKMARKS/$2
		echo -e "\\n\\t\\e[01;37m==> $titleBookmark saved as \\e[01;36m\\e[04m$2\\e[00m\\e[01;37m <==\\e[00m\\n"
	fi
}

deef() {
	if test -n "$1"; then
		if [ ! -r "$FEED_BOOKMARKS/$1" ]; then
			echo -e "\\n $titleBookmark \\e[01;36m\\e[04m$1\\e[00m$title not found.\\e[00m\\n\\n \\e[04mType:\\e[00m\\n\\n   \\e[01;37m\$ deef\\e[00m (without arguments)\\n\\n to get the complete list of all currently saved bookmarks.\\n";
			return 1;
		fi
		local url="$(cat $FEED_BOOKMARKS/$1)";
		if [ ! -n "$url" ]; then
			echo "The bookmark is empty";
			return 1;
		fi
		echo -e "\\n\\t\\e[01;37m==> $title$url\\e[01;37m <==\\e[00m"
		feed "$url";
	else
		echo -e "\\n \\e[04mUsage\\e[00m\\n\\n   \\e[01;37m\$ deef $title<bookmark>\\e[00m\\n\\n \\e[04mCurrently saved bookmarks\\e[00m\\n";
		for i in $(find $FEED_BOOKMARKS -maxdepth 1 -type f);
			do echo -e "   \\e[01;36m\\e[04m$(basename $i)\\e[00m";
		done;
		echo -e "\\n \\e[04mSee also\\e[00m\\n\\n   \\e[01;37m\$ feed\\e[00m\\n";
	fi;
}

EDIT: tongue I started modifying the first script (I saved) and didn't notice the later ones.

Last edited by debdj (2012-08-13 09:44:53)

Offline

#18 2012-08-14 12:28:59

grufo
Member
Registered: 2012-08-09
Posts: 100

Re: A simple but well coloured feed reader written in bash

skanky wrote:

Break the '' :-

sed 's/abc/'$VARIABLE'/'

You can split the sed script to a separate file, which would mean that you could have different types of file or streams that could be converted...should you wish. wink

Thank you smile
So, if you prefer a version with formatting variables...

#!/bin/bash
if [ ! -n "$FEED_BOOKMARKS" ]; then export FEED_BOOKMARKS=$HOME/.feed_bookmarks; fi
if [ ! -d "$FEED_BOOKMARKS" ]; then mkdir -p $FEED_BOOKMARKS; fi

feed() {
	if [ ! -d $FEED_BOOKMARKS ]; then mkdir $FEED_BOOKMARKS; fi

	if [ ! -n "$1" ]; then
		echo -e "\\n \\e[04mUsage\\e[00m\\n\\n   \\e[01;37m\$ feed \\e[01;31m<url>\\e[00m \\e[01;31m<new bookmark?>\\e[00m\\n\\n \\e[04mSee also\\e[00m\\n\\n   \\e[01;37m\$ deef\\e[00m\\n"
		return 1;
	fi

	local rss_source="$(curl --silent $1 | sed -e ':a;N;$!ba;s/\n/ /g')";

	if [ ! -n "$rss_source" ]; then
		echo "The feed is empty";
		return 1;
	fi

	# THE RSS PARSER

	# Formatting
	local f_title="\\\\e[01;31m"
	local f_descr="\\\\e[00;37m"
	local f_ref="\\\\e[01;36m"
	local f_bold="\\\\e[01;36m"
	local f_italic="\\\\e[41;37m"
	local f_underline="\\\\e[4;37m"
	local f_code="\\\\e[00m"
	local f_linklabel="\\\\e[01;31m"
	local f_linkurl="\\\\e[04m"
	local f_formatchrs="\\\\e[01;34m"

	# The characters "£, §" are used as metacharacters. They should not be encountered in a feed...
	echo -e "$(echo $rss_source | \
		sed -e 's/&amp;/\&/g
		s/&lt;\|&#60;/</g
		s/&gt;\|&#62;/>/g
		s/<\/a>/£/g
		s/href\=\"/§/g
		s/<title>/\\n\\n\\n   :: '$f_title'/g; s/<\/title>/\\e[00m ::\\n/g
		s/<link>/ [ '$f_ref'/g; s/<\/link>/\\e[00m ]/g
		s/<description>/\\n\\n'$f_descr'/g; s/<\/description>/\\e[00m\\n\\n/g
		s/<p\( [^>]*\)\?>\|<br\s*\/\?>/\n/g
		s/<b\( [^>]*\)\?>\|<strong\( [^>]*\)\?>/'$f_bold'/g; s/<\/b>\|<\/strong>/'$f_descr'/g
		s/<i\( [^>]*\)\?>\|<em\( [^>]*\)\?>/'$f_italic'/g; s/<\/i>\|<\/em>/'$f_descr'/g
		s/<u\( [^>]*\)\?>/'$f_underline'/g; s/<\/u>/'$f_descr'/g
		s/<code\( [^>]*\)\?>/'$f_code'/g; s/<\/code>/'$f_descr'/g
		s/<a[^§]*§\([^\"]*\)\"[^>]*>\([^£]*\)[^£]*£/'$f_linklabel'\2'$f_descr' '$f_formatchrs'['$f_descr' '$f_linkurl'\1'$f_descr''$f_formatchrs' ]'$f_descr'/g
		s/<li\( [^>]*\)\?>/\n '$f_formatchrs'*'$f_descr' /g
		s/<!\[CDATA\[\|\]\]>//g
		s/\|>\s*<//g
		s/ *<[^>]\+> */ /g
		s/[<>£§]//g')\n\n";
	# END OF THE RSS PARSER

	if [ -n "$2" ]; then
		echo "$1" > $FEED_BOOKMARKS/$2
		echo -e "\\n\\t\\e[01;37m==> \\e[01;31mBookmark saved as \\e[01;36m\\e[04m$2\\e[00m\\e[01;37m <==\\e[00m\\n"
	fi
}

deef() {
	if test -n "$1"; then
		if [ ! -r "$FEED_BOOKMARKS/$1" ]; then
			echo -e "\\n \\e[01;31mBookmark \\e[01;36m\\e[04m$1\\e[00m\\e[01;31m not found.\\e[00m\\n\\n \\e[04mType:\\e[00m\\n\\n   \\e[01;37m\$ deef\\e[00m (without arguments)\\n\\n to get the complete list of all currently saved bookmarks.\\n";
			return 1;
		fi
		local url="$(cat $FEED_BOOKMARKS/$1)";
		if [ ! -n "$url" ]; then
			echo "The bookmark is empty";
			return 1;
		fi
		echo -e "\\n\\t\\e[01;37m==> \\e[01;31m$url\\e[01;37m <==\\e[00m"
		feed "$url";
	else
		echo -e "\\n \\e[04mUsage\\e[00m\\n\\n   \\e[01;37m\$ deef \\e[01;31m<bookmark>\\e[00m\\n\\n \\e[04mCurrently saved bookmarks\\e[00m\\n";
		for i in $(find $FEED_BOOKMARKS -maxdepth 1 -type f);
			do echo -e "   \\e[01;36m\\e[04m$(basename $i)\\e[00m";
		done;
		echo -e "\\n \\e[04mSee also\\e[00m\\n\\n   \\e[01;37m\$ feed\\e[00m\\n";
	fi;
}

Offline

#19 2012-11-10 14:58:17

emorkay
Member
From: India
Registered: 2012-06-06
Posts: 43

Re: A simple but well coloured feed reader written in bash

Hi,
How can I restrict the number of news items shown?

Thank you,

Offline

#20 2012-12-01 20:15:00

Tarqi
Member
From: Ixtlan
Registered: 2012-11-27
Posts: 179
Website

Re: A simple but well coloured feed reader written in bash

@grufo:

There is a small bug in the parser (as you also can see in the wiki-entry simulation): the link target is not correct.

Change

s/<a[^§] ...

to

s/<a[^§|t] ...

to make it work as expected, otherwise sed will match for "<atomic ...", which is the feed link itself.

Btw: I hacked together an extended function for the login message like suggested in the wiki. It will only print the top message and tries to avoid needless downloads and views. Any interest?


Knowing others is wisdom, knowing yourself is enlightenment. ~Lao Tse

Offline

#21 2012-12-02 11:39:19

grufo
Member
Registered: 2012-08-09
Posts: 100

Re: A simple but well coloured feed reader written in bash

Tarqi wrote:

@grufo:

There is a small bug in the parser (as you also can see in the wiki-entry simulation): the link target is not correct.

Thank you very mutch! smile
For now I have not enough time, but I think you can also fix the Wiki (I don't use this script, so I am unable to search for bugs)...

Tarqi wrote:

Btw: I hacked together an extended function for the login message like suggested in the wiki. It will only print the top message and tries to avoid needless downloads and views. Any interest?

Of course! Post it! wink

Offline

#22 2012-12-02 14:51:07

Tarqi
Member
From: Ixtlan
Registered: 2012-11-27
Posts: 179
Website

Re: A simple but well coloured feed reader written in bash

grufo wrote:

For now I have not enough time, but I think you can also fix the Wiki (I don't use this script, so I am unable to search for bugs)...

Fixed.

grufo wrote:

Of course! Post it! wink

It needs some polish, if done i will replace the wiki entry if you like it. However, it will take some time, because i am quite busy right now.


Knowing others is wisdom, knowing yourself is enlightenment. ~Lao Tse

Offline

#23 2013-04-27 09:11:03

djipey
Member
Registered: 2011-07-30
Posts: 158

Re: A simple but well coloured feed reader written in bash

Hello,

do you guys have the same version of the script, but without the parsing of the content ? Without the content mark in fact.

Sincerely

Offline

#24 2013-05-04 02:56:51

TheTruthIs
Member
Registered: 2013-05-04
Posts: 6

Re: A simple but well coloured feed reader written in bash

This is awesome!
However, I want to have this run every time I open a terminal, so I would like to shorten it to one or tow entrie if possible. Is there a way to do that?

Offline

#25 2013-06-22 06:41:09

eijnuhs
Member
Registered: 2013-06-22
Posts: 1

Re: A simple but well coloured feed reader written in bash

I did something that perhaps is what you are looking for. The code is a modified version from the Archwiki site to return the top N news with only its headers and links.
You can modified the code to suit your needs. My code is more of a hack so you can cut out the relevant pieces and stick it in those posted here.
Hope this can be helpful. smile

 
   # Set N to be the number of latest news to fetch
  NEWS=$(echo -e $(curl --silent https://www.archlinux.org/feeds/news/ | awk ' NR == 1 {N = 3 ; while (N) {print;getline; if($0 ~ /<\/item>/) N=N-1} ; sub(/<\/item>.*/,"</item>") ;print}'))
  # Remove some tags 
  NEWS=$(echo -e $NEWS | \
  awk '{
    # uncomment to remove first line which is usually not a news item
    sub(/<lastBuildDate[^>]*>([^>]*>)/,"");sub(/<language[^>]*>([^>]*>)/,"");sub(/<title[^>]*>([^>]*>)/,"");sub(/<link[^>]*>([^>]*>)/,""); 
    while (sub(/<guid[^>]*>([^>]*>)/,"")); 
    while (sub(/<dc:creator[^>]*>([^>]*>)/,""));
    while (sub(/<description[^>]*>([^>]*>)/,"")); print }' | \
  sed -e ':a;N;$!ba;s/\n/ /g')
  echo -e "$(echo -e $NEWS | \
    sed -e 's/&amp;/\&/g
    s/&lt;\|&#60;/</g
    s/&gt;\|&#62;/>/g
    s/<\/a>/£/g
    s/href\=\"/§/g
    s/<title>/\\n\\n :: \\e[01;31m/g; s/<\/title>/\\e[00m ::/g
    s/<link>/\\n [ \\e[01;36m/g; s/<\/link>/\\e[00m ]\\n/g
    s/<p\( [^>]*\)\?>\|<br\s*\/\?>/\n/g
    s/<b\( [^>]*\)\?>\|<strong\( [^>]*\)\?>/\\e[01;30m/g; s/<\/b>\|<\/strong>/\\e[00;37m/g
    s/<i\( [^>]*\)\?>\|<em\( [^>]*\)\?>/\\e[41;37m/g; s/<\/i>\|<\/em>/\\e[00;37m/g
    s/<u\( [^>]*\)\?>/\\e[4;37m/g; s/<\/u>/\\e[00;37m/g
    s/<code\( [^>]*\)\?>/\\e[00m/g; s/<\/code>/\\e[00;37m/g
    s/<a[^§|t]*§\([^\"]*\)\"[^>]*>\([^£]*\)[^£]*£/\\e[01;31m\2\\e[00;37m \\e[01;34m[\\e[00;37m \\e[04m\1\\e[00;37m\\e[01;34m ]\\e[00;37m/g
    s/<li\( [^>]*\)\?>/\n \\e[01;34m*\\e[00;37m /g
    s/<!\[CDATA\[\|\]\]>//g
    s/\|>\s*<//g
    s/ *<[^>]\+> */ /g
    s/[<>£§]//g')"    

The most uninteresting quote is also interesting

Offline

Board footer

Powered by FluxBB