Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'builder'
- require 'nokogiri'
- BASE_URL = 'http://www.nga.gov'
- FEED_URL = BASE_URL + '/programs/calendar/'
- def national_gallery_of_art(page)
- doc = Nokogiri::HTML page
- xml = Builder::XmlMarkup.new :indent => 2
- xml.instruct! :xml, :version => 1.0, :encoding => 'utf-8'
- xml.feed(:xmlns => 'http://www.w3.org/2005/Atom') do |feed|
- feed.title 'National Gallery of Art'
- feed.link :href => FEED_URL
- feed.author do |author|
- author.name 'National Gallery of Art'
- end
- # feed.updated
- # feed.id ?
- #each anchor corresponds to a day
- anchors = doc.xpath("//a[starts-with(@id, 'date')]").to_a.map {|a| a['id']}
- anchors.each_index do |i|
- path = <<-XPATH
- //div[@class='event-grouping'
- and preceding-sibling::a[@id='#{anchors[i]}']][1]
- XPATH
- day = doc.xpath(path).text
- # select all events between two consecutive anchors
- path = "//div[@class='coe-list' and preceding-sibling::a[@id='#{anchors[i]}'] "
- if i < anchors.length - 1
- path << " and following-sibling::a[@id='#{anchors[i+1]}']"
- end
- path << "]"
- events = doc.xpath path
- time = events.first.xpath("span[@class='time'][1]").text.strip
- events.each do |event|
- new_time = event.xpath("span[@class='time']").first
- if new_time
- time = new_time.text.strip
- end
- div = event.xpath("div[@class='event'][1]")
- title = div.text
- link = BASE_URL + div.xpath('a[1]').attr('href')
- feed.entry do |entry|
- entry.title title, :type => 'text'
- entry.link link
- entry.summary "#{day}, #{time}", :type => 'text'
- end
- end
- end
- end # xml.feed
- xml.target!
- end
Advertisement
Add Comment
Please, Sign In to add comment