Prima versione del RitPlanet
RitPlanet (Ruby IT Planet) si ispira a PlanetPlanet e vuole essere un semplice aggregatore di feed che sfrutta la libreria rss/2.0 di ruby basato su 3 file:- Un file di testo con i vari url dei feed
- Un file rhtml con la template di base della pagina html finale
- Il file ritplanet.rb
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
http://ruby-it.org/feed.rss
tpl.rhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>RubyIT Planet</title>
<style type="text/css">
body {
margin: 10px 0 0 0; padding: 0;
font-family: verdana, "Lucida Grande", arial, helvetica, sans-serif;
color: #333;
text-align: center;
}
#container{
width: 800px;
margin: 0 auto;
text-align: left;
}
#container #content h2 a:link{color: #CC0000;}
#container #content h2 a:visited{color: #CC0000;}
#container #content h2 a:hover{background: #000; color: #FFF;}
</style>
</head>
<body>
<div id="container">
<h1>RitPlanet</h1>
<div id="content">
<% for item in @items %>
<h2><a href="<%= item.link %>"><%= item.title %></a></h2>
<p><%= item.description %><p>
<% end %>
</div>
</div>
</body>
ritplanet.rb
#!/usr/bin/env ruby
require 'rss/2.0'
require 'open-uri'
require 'erb'
module RIT
class PlanetItem
attr_reader :title, :link, :description, :date, :channel_title, :channel_link
def initialize(params)
@title = params[:title]
@link = params[:link]
@description = params[:description]
@date = params[:date]
@channel_title = params[:channel_title]
@channel_link = params[:channel_link]
end
end
class Planet
attr_reader :items
def initialize(file_name)
@urls = []
@items = []
open(file_name) do |file|
file.readlines.grep /^[^#]/ do |line|
@urls << line.strip
end
end
start
end
def start
for url in @urls
begin
open(url) do |http|
response = http.read
result = RSS::Parser.parse(response, false)
begin
for item in result.items
@items << PlanetItem.new(
:title => item.title, :link => item.link, :description => item.description,
:date => item.date, :channel_title => result.channel.title,
:channel_link => result.channel.link
)
end
rescue
puts "Problemi nella lettura del feed #{url}"
end
end
rescue
puts "Error: impossibile contattare l'url #{url}"
end
end
end
def render(tpl, file_name)
tpl = ERB.new(open(tpl).read)
result = tpl.result(binding)
open(file_name, 'w') do |file|
file.write(result)
end
end
end
end
rp = RIT::Planet.new('urls.txt')
rp.render('tpl.rhtml', 'blogs.html')