Display Latest FreeBSD News At Prompt

I'm not sure if this is the right section, but I am trying to get my shell to display the latest FreeBSD news at login.

however, characters are missing from the news, probably due to the way I used sed. (Which I am no good at)

Example:

MHTsX.png


And the code snippet:

Code:
if [ "$PS1" ]; then
	# The characters "£, §" are used as metacharacters. They should not be encountered in a feed...
	echo -e "$(echo $(curl --silent http://www.freebsd.org/news/rss.xml | sed -e ':a;N;$!ba;s/\n/ /g') | \
		sed -e 's/&/\&/g
		s/<\|</</g
		s/>\|>/>/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";
fi
 
I won't use sed to parse RSS. If you can handle using python:

Code:
#!/usr/bin/env python 
import feedparser
import socket 
socket.setdefaulttimeout(5)
feed = feedparser.parse( 'http://www.freebsd.org/news/rss.xml' )
sort_entries = sorted(feed.entries, key=lambda entry: entry["published_parsed"])

for e in sort_entries:
    print '\t::', '\033[91m', e.title.encode('utf8'), '\033[0m', '::\n'
    if 'description' in e: 
        print '  ' + e.description + '\n'
    print '\t[', e.link, ']\n'
 
Wow thanks, that is a lot better. I put that snippet into /usr/local/bin/news

and just placed a call to it in my bashrc.

However, I am not familiar with python, but is there a way to limit the amount of latest news it shows? Maybe only the 3 latest news articles?

Tho, using the following and your snippet works a treat. It is much appreciated.

Code:
if [ "$PS1" ]; then
	/usr/local/bin/news
fi
 
Here is mine:
Code:
echo -e "$( curl --silent http://www.freebsd.org/news/rss.xml \
              | grep -A 99999 '<atom:link' \
              | sed 1d \
              | grep -E "(title|link|description)" \
              | sed -E 's/.*<link>/\\e[4m\\e[34m/g' \
              | sed -e 's/<\/link>/\\e[0m\\n/g' \
              | sed -E 's/.*<title>/\\e[01;31m/g' \
              | sed -e 's/<\/title>/\\e[00m/g' \
              | sed -E 's/.*<description>/\\e[00;37m/g' \
              | sed -e 's/<\/description>/\\e[00m/g' \
          )"

... and it looks like that:
vZ2NvbA
 
This would list the latest three in ascending order

Code:
#!/usr/bin/env python 
import feedparser
import socket 
socket.setdefaulttimeout(5)
feed = feedparser.parse( 'http://www.freebsd.org/news/rss.xml' )
sort_entries = sorted(feed.entries[0], key=lambda entry: entry["published_parsed"])

for e in sort_entries:
    print '\t::', '\033[91m', e.title.encode('utf8'), '\033[0m', '::\n'
    if 'description' in e: 
        print '  ' + e.description + '\n'
    print '\t[', e.link, ']\n'
 
Other possibility, using ruby instead python:
Code:
require 'rss/1.0'
require 'rss/2.0'
require 'open-uri'

source = "http://www.freebsd.org/news/rss.xml"
content=""
open(source) do |s| content = s.read end
rss = RSS::Parser.parse(content,false)

print "RSS title: ", rss.channel.title, "\n"
print "RSS link: ", rss.channel.link, "\n"
print "RSS description: ", rss.channel.description, "\n"
print "RSS publication date: ", rss.channel.date, "\n"
print "Number of RSS items: ", rss.items.size, "\n" 
puts
puts "Item values"
puts
print "title: ", rss.items[0].title, "\n"
print "link: ", rss.items[0].link, "\n"
print "description: ", rss.items[0].description, "\n"
print "date: ", rss.items[0].date, "\n"
puts
print "title: ", rss.items[1].title, "\n"
print "link: ", rss.items[1].link, "\n"
print "description: ", rss.items[1].description, "\n"
print "date: ", rss.items[1].date, "\n"
puts
print "title: ", rss.items[2].title, "\n"
print "link: ", rss.items[2].link, "\n"
print "description: ", rss.items[2].description, "\n"
print "date: ", rss.items[2].date, "\n"
puts
print "title: ", rss.items[3].title, "\n"
print "link: ", rss.items[3].link, "\n"
print "description: ", rss.items[3].description, "\n"
print "date: ", rss.items[3].date, "\n"
puts
print "title: ", rss.items[4].title, "\n"
print "link: ", rss.items[4].link, "\n"
print "description: ", rss.items[4].description, "\n"
print "date: ", rss.items[4].date, "\n"
puts
print "title: ", rss.items[5].title, "\n"
print "link: ", rss.items[5].link, "\n"
print "description: ", rss.items[5].description, "\n"
print "date: ", rss.items[5].date, "\n"
puts
print "title: ", rss.items[6].title, "\n"
print "link: ", rss.items[6].link, "\n"
print "description: ", rss.items[6].description, "\n"
print "date: ", rss.items[6].date, "\n"
puts
print "title: ", rss.items[7].title, "\n"
print "link: ", rss.items[7].link, "\n"
print "description: ", rss.items[7].description, "\n"
print "date: ", rss.items[7].date, "\n"
puts
print "title: ", rss.items[8].title, "\n"
print "link: ", rss.items[8].link, "\n"
print "description: ", rss.items[8].description, "\n"
print "date: ", rss.items[8].date, "\n"
puts
print "title: ", rss.items[9].title, "\n"
print "link: ", rss.items[9].link, "\n"
print "description: ", rss.items[9].description, "\n"
print "date: ", rss.items[9].date, "\n"
 
cpu82 said:
Other possibility, using ruby instead python:
Code:
require 'rss/1.0'
require 'rss/2.0'
require 'open-uri'

source = "http://www.freebsd.org/news/rss.xml"
content=""
open(source) do |s| content = s.read end
rss = RSS::Parser.parse(content,false)

print "RSS title: ", rss.channel.title, "\n"
print "RSS link: ", rss.channel.link, "\n"
print "RSS description: ", rss.channel.description, "\n"
print "RSS publication date: ", rss.channel.date, "\n"
print "Number of RSS items: ", rss.items.size, "\n" 
puts
puts "Item values"
puts
print "title: ", rss.items[0].title, "\n"
print "link: ", rss.items[0].link, "\n"
print "description: ", rss.items[0].description, "\n"
print "date: ", rss.items[0].date, "\n"
puts
print "title: ", rss.items[1].title, "\n"
print "link: ", rss.items[1].link, "\n"
print "description: ", rss.items[1].description, "\n"
print "date: ", rss.items[1].date, "\n"
puts
print "title: ", rss.items[2].title, "\n"
print "link: ", rss.items[2].link, "\n"
print "description: ", rss.items[2].description, "\n"
print "date: ", rss.items[2].date, "\n"
puts
print "title: ", rss.items[3].title, "\n"
print "link: ", rss.items[3].link, "\n"
print "description: ", rss.items[3].description, "\n"
print "date: ", rss.items[3].date, "\n"
puts
print "title: ", rss.items[4].title, "\n"
print "link: ", rss.items[4].link, "\n"
print "description: ", rss.items[4].description, "\n"
print "date: ", rss.items[4].date, "\n"
puts
print "title: ", rss.items[5].title, "\n"
print "link: ", rss.items[5].link, "\n"
print "description: ", rss.items[5].description, "\n"
print "date: ", rss.items[5].date, "\n"
puts
print "title: ", rss.items[6].title, "\n"
print "link: ", rss.items[6].link, "\n"
print "description: ", rss.items[6].description, "\n"
print "date: ", rss.items[6].date, "\n"
puts
print "title: ", rss.items[7].title, "\n"
print "link: ", rss.items[7].link, "\n"
print "description: ", rss.items[7].description, "\n"
print "date: ", rss.items[7].date, "\n"
puts
print "title: ", rss.items[8].title, "\n"
print "link: ", rss.items[8].link, "\n"
print "description: ", rss.items[8].description, "\n"
print "date: ", rss.items[8].date, "\n"
puts
print "title: ", rss.items[9].title, "\n"
print "link: ", rss.items[9].link, "\n"
print "description: ", rss.items[9].description, "\n"
print "date: ", rss.items[9].date, "\n"

Might want to apply some iteration before you get trolled (to late).
 
Adding colors is not too bad, is more attractive:
Code:
require 'rss/1.0'
require 'rss/2.0'
require 'open-uri'
require 'term/ansicolor'
include Term::ANSIColor

source = "http://www.freebsd.org/news/rss.xml"
content=""
open(source) do |s| content = s.read end
rss = RSS::Parser.parse(content,false)

print negative, bold, "RSS title: ", rss.channel.title, reset, "\n"
print negative, bold, "RSS link: ", rss.channel.link, reset, "\n"
print negative, bold, "RSS description: ", rss.channel.description, reset, "\n"
print negative, bold, "RSS publication date: ", rss.channel.date, reset, "\n"
print negative, bold, "Number of RSS items: ", rss.items.size, reset, "\n" 
puts
print yellow, bold, underscore, "Item values", reset, "\n" 
puts
print red, bold, underscore, "title: ", rss.items[0].title, reset, "\n" 
print blue, bold, "link: ", rss.items[0].link, reset, "\n"
print green, bold, "description: ", rss.items[0].description, reset, "\n"
print bold, "date: ", rss.items[0].date, reset, "\n"
 
derekschrock said:
This would list the latest three in ascending order

Code:
#!/usr/bin/env python 
import feedparser
import socket 
socket.setdefaulttimeout(5)
feed = feedparser.parse( 'http://www.freebsd.org/news/rss.xml' )
sort_entries = sorted(feed.entries[0], key=lambda entry: entry["published_parsed"])

for e in sort_entries:
    print '\t::', '\033[91m', e.title.encode('utf8'), '\033[0m', '::\n'
    if 'description' in e: 
        print '  ' + e.description + '\n'
    print '\t[', e.link, ']\n'
Many thanks indeed, I love this and I put this code in my facebook group.
 
Here... Notice the heredoc removes the need for multiple print statements and newlines( it can take printf or puts as well) ... more so with ruby you can do enumerated iterations from the inside out which allows you to avoid the index style looping like almost every C derived language:

Code:
#!/usr/bin/env ruby
# UNIXgod's refactoring of cpu82 FreeBSD rss parser
# depends on term/ansicolor gem... 
# might be easier to localize that at toplevel of script.
require 'rss/1.0'
require 'rss/2.0'
require 'open-uri'
require 'term/ansicolor'
include Term::ANSIColor

source = "http://www.freebsd.org/news/rss.xml"
content= String.new
open(source) { |s| content = s.read }
rss = RSS::Parser.parse(content,false)

#THEMES:
r=reset; b=bold; u=underscore
n=negative; nb=n+b
ybu=yellow+b+u; rbu=red+b+u; blb=blue+b; gb=green+b

puts <<-EOF
#{nb} RSS title:  #{rss.channel.title} #{r}
#{nb} RSS link:  #{rss.channel.link} #{r} 
#{nb} RSS description:  #{rss.channel.description} #{r} 
#{nb} RSS publication date: #{rss.channel.date} #{r} 
#{nb} Number of RSS items: #{rss.items.size} #{r}

#{ybu} Item values #{r} 
EOF

rss.items.each do |i|
  printf <<-EOF
  #{rbu} title: #{i.title} #{r}
  #{blb} link:  #{i.link} #{r}
  #{gb} description:  #{i.description} #{r}
  EOF
end


cpu82's original:
cpu82 said:
Adding colors is not too bad, is more attractive:
Code:
require 'rss/1.0'
require 'rss/2.0'
require 'open-uri'
require 'term/ansicolor'
include Term::ANSIColor

source = "http://www.freebsd.org/news/rss.xml"
content=""
open(source) do |s| content = s.read end
rss = RSS::Parser.parse(content,false)

print negative, bold, "RSS title: ", rss.channel.title, reset, "\n"
print negative, bold, "RSS link: ", rss.channel.link, reset, "\n"
print negative, bold, "RSS description: ", rss.channel.description, reset, "\n"
print negative, bold, "RSS publication date: ", rss.channel.date, reset, "\n"
print negative, bold, "Number of RSS items: ", rss.items.size, reset, "\n" 
puts
print yellow, bold, underscore, "Item values", reset, "\n" 
puts
print red, bold, underscore, "title: ", rss.items[0].title, reset, "\n" 
print blue, bold, "link: ", rss.items[0].link, reset, "\n"
print green, bold, "description: ", rss.items[0].description, reset, "\n"
print bold, "date: ", rss.items[0].date, reset, "\n"

Also further exploration of that gem it looks like if you really want to use it you can remove the need to call reset with syntax like: puts "Hello".on_red or printf "World!".cyan and so forth without the need to call reset:

[CMD="2.0.0dev :005 >"]Term::ANSIColor.methods - Module.methods[/CMD]
=> [:coloring?, :coloring=, :attributes, :support?, :clear, :reset, :bold, :dark, :italic, :underline, :underscore, :blink, :rapid_blink, :negative, :concealed, :strikethrough, :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white, :eek:n_black, :eek:n_red, :eek:n_green, :eek:n_yellow, :eek:n_blue, :eek:n_magenta, :eek:n_cyan, :eek:n_white, :intense_black, :intense_red, :intense_green, :intense_yellow, :intense_blue, :intense_magenta, :intense_cyan, :intense_white, :eek:n_intense_black, :eek:n_intense_red, :eek:n_intense_green, :eek:n_intense_yellow, :eek:n_intense_blue, :eek:n_intense_magenta, :eek:n_intense_cyan, :eek:n_intense_white, :uncolored]
 
derekschrock said:
This would list the latest three in ascending order

Code:
#!/usr/bin/env python 
import feedparser
import socket 
socket.setdefaulttimeout(5)
feed = feedparser.parse( 'http://www.freebsd.org/news/rss.xml' )
sort_entries = sorted(feed.entries[0], key=lambda entry: entry["published_parsed"])

for e in sort_entries:
    print '\t::', '\033[91m', e.title.encode('utf8'), '\033[0m', '::\n'
    if 'description' in e: 
        print '  ' + e.description + '\n'
    print '\t[', e.link, ']\n'
Hi,

I have just discovered today that I got errors that said

ImportError: No module named feedparser

when running this python script. I am using python 2.7 as my default python and FreeBSD 9-STABLE is my os. I have just upgraded py27-feedparser-5.1.3 this morning too.

Apologized me for disturbing but since I am leaning python at this moment so I need to know that what really the cause of error is.

Thanks in advanced for any hints or helps.

With best regards,
MNIHKLOM
 
MNIHKLOM said:
Yes I did since your first post and have upgraded this morning but still got error.

Thank you very much for your prompt response.


With best regards,
MNIHKLOM

What site-package did you install it to? Seems like you might have 2.6 and 2.7 installed?
 
derekschrock said:
What site-package did you install it to? Seems like you might have 2.6 and 2.7 installed?

Hi,

To cpu82 and derekschrock, this is all about python I have installed

[MNIHKLOM] ~% pkg_info -Ix python
boost-python-libs-1.48.0 Framework for interfacing Python and C++
python-2.7,2 The "meta-port" for the default version of Python interpret
python-doc-html-2.7.3 Documentation for the Python programming language
python24-2.4.5_8 An interpreted object-oriented programming language
python27-2.7.3_6 An interpreted object-oriented programming language
python32-3.2.3_1 An interpreted object-oriented programming language
python33-3.3.0_2 An interpreted object-oriented programming language
[MNIHKLOM] ~%

and my /etc/make.conf content is

[MNIHKLOM] ~% cat /etc/make.conf
#
WITH_CLANG=yes
WITH_CLANG_IS_CC=yes
CC=clang
CXX=clang++
CPP=clang-cpp
#koko
WRKDIRPREFIX=/kaitag/MANEE
DISTDIR=/kaitag/distfiles
PACKAGES=/kaitag/packages
##
DOC_LANG= en_US.ISO8859-1
PYTHON_DEFAULT_VERSION=python2.7
###
EMACS_PORT_NAME=emacs24
###
WITH_BERKELEYDB=db43
WITH_BDB_VER=43
# added by use.perl 2012-03-11 11:37:05
PERL_VERSION=5.14.2
[MNIHKLOM] ~%

some blender's dependencies required python33 to be installed but I forgot why I installed python32. I keep python24 since I am using zope211.

Once again many thanks for your times indeed.

With best regards,
MNIHKLOM
 
Back
Top