amazon_rss.rb: View Code / Run#! /usr/bin/ruby
# Read RSS feed for computer books at Amazon
# Define as a method so it can be pasted into libraries later
def amazon_rss
# Open the RSS file
require 'open-uri'
uri = "http://xml.amazon.com/onca/xml3?mode=books&bcm=Books%3A%20Computers%20%26%20Internet" +
"&t=webservices-20&dev-t=amznRss&type=lite&page=1&ct=text/xml&sort=+salesrank" +
"&f=http://xml.amazon.com/xsl/xml-rss091.xsl&BrowseNodeSearch=5"
rssfeed = open(uri)
# Use the built-in REXML module
require "rexml/document"
include REXML
# Parse the RSS feed
rssdoc = Document.new rssfeed.read
# Create a list of each book and its URL
result = "<strong>Current items in Amazon's <a href=#{uri}>Computer Book RSS feed</a>:</strong><ul>"
rssdoc.elements.each("rss/channel/item") do |item|
title = item.elements["title"].text
link = item.elements["link"].text
result += "<li><a href='" + link + "'>" + title + "</a>"
end
return result + "</ul>"
end
|