Created
January 18, 2011 23:38
-
-
Save paulclip/785390 to your computer and use it in GitHub Desktop.
RSSFusion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# RSS Fusion: combines multiple RSS feeds and prints out the last N sorted by date | |
require 'rss/1.0' | |
require 'rss/2.0' | |
require 'open-uri' | |
# sample feeds, replace with your own :-) | |
feeds = [ {:name => "Main", :url => "http://feeds.feedburner.com/Technoclippings"}, | |
{:name => "Art & Science", :url => "http://artscience.cyberclip.com/rss.xml"}, | |
{:name => "Tech", :url => "http://tech.cyberclip.com/rss.xml"}, | |
{:name => "Travel", :url => "http://travel.cyberclip.com/rss.xml"}, | |
{:name => "Cog", :url => "http://cog.cyberclip.com/rss.xml"} | |
] | |
class RSSFusion | |
attr_reader :post | |
def initialize(feeds, n = 10) | |
@feeds = feeds | |
@n = n | |
@posts = {} | |
end | |
# fetch a single rss feed and store key attributes in @posts | |
def fetch_rss_feed(url) | |
feed = "" | |
open(url) do |s| feed = s.read end | |
rss = RSS::Parser.parse(feed, false) | |
rss.items.each do |p| | |
@posts[p.date] = { :title => p.title, :url => p.link, :date => p.date, :desc => p.description } | |
end | |
end | |
# fetches all feeds and iterates through N most recent posts yielding to user-supplied block | |
def print_feeds | |
@feeds.each { |f| fetch_rss_feed f[:url] } | |
@posts.keys.sort.last(@n).reverse.each do |k| | |
@post = @posts[k] | |
yield | |
end | |
end | |
end | |
if ARGV.size != 2 | |
puts "Usage: rssfusion <N> <template>\nWhere <N> is the number of posts to print and <template> is a ruby file with formating instructions" | |
exit -1 | |
end | |
n = ARGV[0].to_i | |
template = IO.read(ARGV[1]) | |
rss = RSSFusion.new(feeds, n) | |
# eval was the most expedient / flexible path for me, make sure you understand its security implications! | |
rss.print_feeds { eval(template) } | |
# example template file | |
# puts "#{rss.post[:title]}\t#{rss.post[:date].strftime('%m/%d/%y')}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment