#!/usr/bin/ruby -w

#=======================================================
# leerss.rb - un pequeño lector de RSS
#-------------------------------------------------------
# Modificado a partir de un trozo de ocho líneas de 
# código visto en el libro Ruby Cookbook (Curso de Ruby)
#=======================================================
# FJA - neocipres@gmail.com	Junio de 2008
#=======================================================

require 'rss/2.0'
require "open-uri"

url =  {"ruby" => 'http://www.ruby-lang.org/es/feeds/news.rss',		# Noticias sobre Ruby
	"dicampus" => 'http://cursodicampus2008.wordpress.com/feed/',	# Curso Dicampus 2008
        "mabishu" => 'http://www.mabishu.com/blog/feed',	   	# Mabishu
        "ciencia" => 'http://www.ELPAIS.com/rss/feed.html?feedId=13',  	# El País -Ciencia,tecnología ...
        "humor" => 'http://www.ELPAIS.com/rss/feed.html?feedId=1018'}  	# El País - Viñetas
html = ''
begin
 clave = ARGV[0]
 raise if ARGV[0] == nil 
 feed = RSS::Parser.parse(open(url[clave]).read, false)
 html << '<head>' << '<meta http-equiv="content-type" content="text/html; charset=utf-8">' << '</head>'
 html << '<hr>' << '<h2 align="center">' << #{feed.channel.title} ·" << "</h2>" << '<hr>' << '<br>'
 feed.items.each do |item|
  html << "<h3>" << item.title << "</h3>"
  html << '<div style="text-align: justify;"><p>' << item.description << "</p></div>"  
  html << " <p> <a href=#{item.link}> Leer más </a>  </p>"
  
 end
 f = File.new("/tmp/rss.html", "w")
 f.write(html)
 print "\n\t Generado el archivo: /tmp/rss.html \n\n"
rescue
 if ARGV[0] == nil
	puts "\n\t #{"="*60} "
	puts "\t > Uso: leerss <canal> \n" 
	puts "\n\t > canal: #{url.keys.join(", ")} "
 	puts "\t #{"="*60} \n\n"
 else
 	print "\n\t Error-> ", $!, "\n\n"
	
 end
ensure
 f.close unless f.nil?
end