Saturday, May 1, 2010

feed aggreegator in ruby on rails

to fetch the feed to the db
def cfeed
if params[:recache] and params[:secret]=="at123"
cache_feeds
expire_fragment(:controller => 'public', :action => 'index') # next load of index will re-fragment cache
render :text => "Done recaching feeds"
end
end


uses the feed tool gem

# This will replace cached feeds in the DB that have the same URI. Be careful not to tie up the DB connection.
def cache_feeds
puts "Caching feeds... (can be slow)"

feeds = Feed.all.map do |uri|

feed = FeedTools::Feed.open( uri.address )
{ :uri => uri.address, :title => feed.title,
:items => feed.items.map { |item|
{:title => item.title, :published => item.published, :link => item.link,:description=>item.description,:author=>item.author} } }

end
feeds.each { |feed|
new = CachedFeed.find_or_initialize_by_uri( feed[:uri] )
new.parsed_feed = feed
puts feed
new.save!
}
end

and for showing the feeds from db to page..

@f=Feed.find(:all)
@aggregate = read_cache unless read_fragment({})

def read_cache
@fr=@f
@fr.map { |uri|
begin
feed = CachedFeed.find_by_uri( uri.address ).parsed_feed
feed[:items].map { |item| {:feed_title =>feed[:title], :feed_item => item} }
rescue
[] # because there might not be anything cached for some feed(s)
end

} .flatten .sort_by { |item| item[:feed_item][:published] } .reverse
end

and for the view part use the following code


<% cache do %>
<%
lastday = -1
@aggregate.each do |item| %>


<%
mydate = item[:feed_item][:published].getlocal
if mydate.yday != lastday
%>
<%= mydate.strftime("%b %d,%Y" ) %>
<%
lastday = mydate.yday
end
%>


"><%= item[:feed_item][:title] %>


<%=item[:feed_item][:description]%>


<% end %>
<% end %>


this will make your feed agreegator hopefully better..

No comments:

Post a Comment