Codice LunaRss 0_6

Vedi tutte le pagine e le modifiche recenti o scarica i sorgenti nella pagina


LunaRss v0.6 – modificat0 da Chiaroscuro

Codice LunaRss 0_7 – refactored by AntonioCarpentieri

Commenti

class FeedLoader
   def get_feed()      
      if @http_proxy.nil?
         h = Net::HTTP.new(@url_http)
      else
         proxy_uri = URI.parse(@http_proxy)
         h = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port).new(@url_http)
      end

      resp,data = h.get(@url_pagina,nil)

      data.gsub!(/\0/,'')

      return data
   end

end
 def initialize(url, proxy=nil)
    @feed_loader = FeedLoader.new(proxy)
    @url = url
 end 
 def inject_loader! feed_loader
    @feed_loader = feed_loader 
 end

ecco il codice finale:


require 'net/http'

FeedData = Struct.new :channel, 
                      :titles, 
                      :descriptions, 
                      :links

class FeedLoader

    def initialize(proxy=nil)
        @http_proxy = proxy 
    end 

    def get_feed url      
       url_no_http  = url.gsub(/http\:\/\//,'')
       url_http     = url_no_http.gsub(/\/.*/,'')
       url_pagina   = "/" + url.gsub(%r{http:\/\/.*?\/},'')

      if @http_proxy.nil?
         h = Net::HTTP.new(url_http)
      else
         proxy_uri = URI.parse(@http_proxy)
         h = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port).new(url_http)
      end

      resp,data = h.get(url_pagina,nil)

      data.gsub!(/\0/,'')

      return data
    end

end

class RssParser

 def initialize(url, proxy=nil)
    @feed_loader = FeedLoader.new(proxy)
    @url = url
 end 

 def inject_loader! feed_loader
    @feed_loader = feed_loader 
 end

 def esegui_parsing

   return FeedData.new( 
             parse( channel_title_pattern    ),            
             parse( item_title_pattern       ),
             parse( item_description_pattern ),
             parse( item_links_pattern       )
           )  
 end

private

   def get_raw_data 
     @feed_loader.get_feed @url
   end

   def rss_data
        @data ||= get_raw_data        
   end

   def parse pattern

      output_data = []

      rss_data.scan pattern do |x|
         output_data << x
      end

      return output_data      
   end

 def channel_title_pattern
   %r{<channel(?!s).*?>.*?(?:<title>(?:<!\[CDATA\[)*(.*?)(?:\]\]>)*</title>.*?)?</channel>}m
 end

 def item_title_pattern
   %r{<item(?!s).*?>.*?(?:<title>(?:<!\[CDATA\[)*(.*?)(?:\]\]>)*</title>.*?)?</item>}m 
 end

 def item_links_pattern
   %r{<item(?!s).*?>.*?(?:<link>(?:<!\[CDATA\[)*(.*?)(?:\]\]>)*</link>.*?)?</item>}m
 end

 def item_description_pattern
   %r{<item(?!s).*?>.*?(?:<description>(?:<!\[CDATA\[)*(.*?)(?:\]\]>)*</description>.*?)?</item>}m
 end

end

##############################

def align text
   text.gsub(/^\s*\./m, '') 
end

##############################

class RssMaker
  def header
    align <<-HTML
             .Content-type: text/html\r\n\r\n
             .<html>
             .<body>
             HTML
  end

  def footer
    align <<-HTML
             .</body>
             .</html>
             HTML
  end

  def channel name
    align <<-HTML
             .<h3>#{name}</h3>
             HTML
  end

  def entry titolo, descrizione, link
    align <<-HTML
             .<strong>
             .  <a href="#{link}"> #{titolo} </a>
             .</strong><br/>
             .
             .<font size="-1"> #{descrizione} </font>
             .<p/>
             HTML
  end

end

##############################

# Configurations

feeds=[
    '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'
]

NEED_PROXY = false #switch this if you have a proxy
proxy_url = "http://10.0.0.2:8080" #put here your proxy, if you have one

##############################

rss_make = RssMaker.new

puts rss_make.header

feeds.each do |feed|

  if NEED_PROXY 
    parser = RssParser.new feed, proxy_url 
  else
    parser = RssParser.new feed 
  end

  data = parser.esegui_parsing

  puts rss_make.channel( data.channel )

  0.upto data.titles.length do|i|

    puts rss_make.entry( 
                    data.titles[i], 
                    data.descriptions[i], 
                    data.links[i] 
                  )

  end

end

puts rss_make.footer
Updated on May 08, 2006 20:34 by Antonio Carpentieri (84.220.74.110)