Skip to content

Instantly share code, notes, and snippets.

@mulbc
Created April 30, 2010 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mulbc/385890 to your computer and use it in GitHub Desktop.
Save mulbc/385890 to your computer and use it in GitHub Desktop.
#Source from http://snippets.dzone.com/posts/show/4713
require 'rss/1.0'
require 'rss/2.0'
require 'open-uri'
require 'open-uri'
class Rssreader
def initialize(url)
source = url # url or local file
content = "" # raw content of rss feed will be loaded here
open(source) do |s| content = s.read end
@rss = RSS::Parser.parse(content, false)
end
# returns the first 3 titles from the rss feed
def get_summary()
buffer = '['
for i in 0..2
buffer += @rss.items[i.to_i].title + ' | '
end
buffer.slice(0,buffer.length-3) + ']'
end
def enclosure?
@rss.items.to_s.scan('<enclosure').length > 0
end
def get_enclosure_url
enclosure = @rss.items[0].enclosure
enclosure.url
end
def rwget(url, filename)
file = File.new(filename, 'w')
file.puts open(url, 'User-Agent' => 'Ruby-wget').read
end
def download_enclosure()
if self.enclosure? then
enclosure_url = self.get_enclosure_url()
local_filename = File.basename(enclosure_url)
#puts local_filename
if not File.exist?(local_filename) then
puts 'downloading enclosure ...'
self.rwget(enclosure_url, local_filename)
puts 'download completed'
else
puts 'enclosure downloaded already'
end
end
end
end
if __FILE__ == $0
url = "http://mysite.com/gwd/feed/lugradio.rss"
rss = Rssreader.new(url)
puts rss.get_summary()
rss.download_enclosure()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment