#!/usr/bin/env ruby # This code comes from http://ruby-it.org/pages/Codice+LunaRss+0_2 # Check the page for copyright notice and explanations #!/usr/bin/env ruby require 'net/http' class Rss_parser def initialize(url) @url_completo = url @url_no_http = @url_completo.gsub(/http\:\/\//,'') @url_http = @url_no_http.gsub(/\/.*/,'') @url_pagina = "/" + @url_completo.gsub(%r{http:\/\/.*?\/},'') end def esegui_parsing h = Net::HTTP.new(@url_http) resp,data = h.get(@url_pagina,nil) data.gsub!(/\0/,'') canale = data.scan channel_title_pattern titolo = [] i = 0 data.scan item_title_pattern do |x| titolo[i] = x i += 1 end link = [] i = 0 data.scan item_links_pattern do |x| link[i] = x i += 1 end descrizione = [] i = 0 data.scan item_description_pattern do |x| descrizione[i] = x i += 1 end return { 'titoli' => titolo, 'links' => link, 'descrizioni' => descrizione, 'canali' => canale } end private def channel_title_pattern %r{.*?(?:(?:<!\[CDATA\[)*(.*?)(?:\]\]>)*.*?)?}m end def item_title_pattern %r{.*?(?:(?:<!\[CDATA\[)*(.*?)(?:\]\]>)*.*?)?}m end def item_links_pattern %r{.*?(?:(?:)*.*?)?}m end def item_description_pattern %r{.*?(?:(?:)*.*?)?}m end end ############################## class RssMaker def header <<-HTML Content-type: text/html\r\n\r\n HTML end def footer <<-HTML HTML end def channel name <<-HTML

#{name}

HTML end def entry titolo, descrizione, link <<-HTML #{titolo}
#{descrizione}

HTML end end ############################## elenco_rss=[ 'http://www.repubblica.it/rss/scienza_e_tecnologia/rss2.0.xml', 'http://programmazione.it/rss.xml', 'http://www.hwupgrade.it/rss_news.xml', 'http://www.hwupgrade.it/rss_articoli.xml', 'http://www.beppegrillo.it/index.xml' ] rss_make = RssMaker.new puts rss_make.header elenco_rss.each do |rss_file| rss_da_parsare = Rss_parser.new rss_file dati = {} dati = rss_da_parsare.esegui_parsing canale = dati['canali'] titolo = dati['titoli'] link = dati['links'] descrizione = dati['descrizioni'] puts rss_make.channel( canale ) 0.upto titolo.length do|i| puts rss_make.entry( titolo[i], descrizione[i], link[i] ) end end puts rss_make.footer