#! /usr/bin/ruby
# Download files to be aggregated into a local cache
require "get_param"
cachedir = get_param("rubyriver.yml","cachedir")
# The feedlist for RubyRiver is rubyriver.opml
feedlist = get_param("rubyriver.yml","feedlist")
# Record failures in an error log for later analysis
logdir = get_param("rubyriver.yml","logdir")
logname = Time.now.strftime(logdir + "/%Y%m%d-%H%M.txt")
# Read rubyriver.opml into memory for XML parsing
require "open-uri"
require "rexml/document"
include REXML
opmlfile = File.open(feedlist)
opmldoc = Document.new(opmlfile.read)
# Local copies of the feeds will be given sequential file names
id = 1
# Gather the feeds
opmldoc.elements.each("opml/body/outline/outline") do |item|
begin
# Read remote copy
remotefile = open(item.attributes["xmlUrl"] )
localfile = File.open(cachedir + "/" + id.to_s + ".xml", "w")
# Save local copy
localfile.puts( remotefile.read )
remotefile.close
localfile.close
rescue Exception
# Log errors
logfile = File.open(logname,"a+")
logfile.puts(item.attributes["title"])
logfile.puts(feedurl)
logfile.close
end
id += 1
end
opmlfile.close