Created
November 8, 2011 13:44
-
-
Save mfojtik/1347764 to your computer and use it in GitHub Desktop.
Ruby course RSS scapper
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
require 'open-uri' | |
require 'nokogiri' | |
module Course | |
class RSSSource | |
attr_accessor :url | |
attr_accessor :xml | |
def initialize(url) | |
self.url = url | |
self | |
end | |
def fetch! | |
f = open(url) | |
self.xml = Nokogiri::XML(f.read) | |
end | |
def titles | |
[] | |
end | |
end | |
class PlanetFedora < RSSSource | |
def initialize | |
super('http://planet.fedoraproject.org/rss20.xml') | |
@old_titles = [] | |
self | |
end | |
def titles | |
self.xml.xpath('/rss/channel/item/title').each do |t| | |
next if @old_titles.include? t.text | |
yield t.text | |
@old_titles << t.text | |
end | |
end | |
end | |
end | |
planet = Course::PlanetFedora.new | |
loop do | |
planet.fetch! | |
planet.titles do |title| | |
puts title | |
end | |
sleep(5) | |
puts "Fetching new titles..." | |
end | |
=begin | |
old_titles = [] | |
loop do | |
f = open("http://planet.fedoraproject.org/rss20.xml") | |
feed = Nokogiri::XML(f.read) | |
old_title_size = old_titles.size | |
feed.xpath("/rss/channel/item/title").each do |title| | |
next if old_titles.include? title.text | |
puts title.text | |
old_titles << title.text | |
end | |
puts "No change..." if old_title_size == old_titles.size | |
sleep(5) | |
end | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment