2 Replies - 1572 Views - Last Post: 02 December 2009 - 03:30 PM

#1 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Feed Reader

Posted 24 November 2009 - 02:31 PM

Description: Downloads feed content, and prints out the number of new articles, the date they were published, and the description of the article (after parsing out html, which doesn't look very good on a text only command line)
require 'rss'
require 'open-uri'

feed = "http://feeds.feedburner.com/dic_featured"
#our feed to open, I can't see why you would _want_
#to change it, but go ahead and make it whatever feed you want

content = ""

open(feed) do |f|
  content = f.read
  #get the content of the feed
end

rss = RSS::Parser.parse(content, false) #actual parsing here

#Output Information#
puts "tt#{rss.channel.title}"
puts "#{rss.items.size} new entriesnn"
#prints number of new entries and title of the feed

rss.items.each do |item|    #for each element of rss...
  desc = item.description 
  desc.gsub!(/<.*/[a-zA-Z]*>/i, '')
  #I suck at regex and this is the best I could come up with to parse out html
  #Suggestions welcome!
  desc.gsub!("'", "'")  #make ' display correctly as apostrophes
                            #(there are other, less common things as well)
  puts "#{item.date.strftime("%B %d, %Y")} : #{item.title}" #print the date in human readable time
  puts "t#{item.description}"
end



Is This A Good Question/Topic? 0
  • +

Replies To: Feed Reader

#2 EdwinNameless   User is offline

  • D.I.C Addict
  • member icon

Reputation: 128
  • View blog
  • Posts: 723
  • Joined: 15-October 09

Re: Feed Reader

Posted 02 December 2009 - 03:20 PM

Cool snippet. Just shows how powerful Ruby is...
Was This Post Helpful? 0
  • +
  • -

#3 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Feed Reader

Posted 02 December 2009 - 03:30 PM

Thanks. I was surprised by how easy this was to do actually. Never would have guessed it could be done in less than 30 lines!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1