Skip to content

Instantly share code, notes, and snippets.

@liquidz
Created June 1, 2009 10:17
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 liquidz/121338 to your computer and use it in GitHub Desktop.
Save liquidz/121338 to your computer and use it in GitHub Desktop.
nicobook.rb
require 'open-uri'
require 'kconv'
require 'rexml/document'
def make_url(aws_key, options)
res = "http://webservices.amazon.co.jp"
res += "/onca/xml?Service=AWSECommerceService"
res += "&AWSAccessKeyId=#{aws_key}"
res += "&SearchIndex=Books"
res += "&ResponseGroup=Small,Images"
options.each_key do |key|
res += "&#{key}=#{options[key]}"
end
res
end
def keyword_search(aws_key, keyword)
url = make_url(aws_key, {
:Operation => "ItemSearch",
:Keywords => keyword
})
open(url) do |fp|
yield(REXML::Document.new(fp.read).root)
end
end
def isbn_search(aws_key, isbn)
url = make_url(aws_key, {
:Operation => "ItemLookup",
:IdType => "ISBN",
:ItemId => isbn
})
open(url) do |fp|
File.open("tmpxml.xml", "w") do |wfp|
wfp.write(fp.read)
end
#yield(REXML::Document.new(fp.read).root)
end
end
lambda {
aws = ""
File.open("tmp") do |fp|
aws = fp.read.strip
end
=begin
keyword_search(aws, "Ruby") do |doc|
doc.elements.each("Items/Item/ItemAttributes/Title") do |elem|
puts Kconv.toeuc(elem.text)
end
end
=end
=begin
isbn_search(aws, "4163233105") do |doc|
doc.elements.each("Items/Item/ItemAttributes") do |attrs|
p attrs
#puts Kconv.toeuc(attrs.elements["title"].text)
#puts Kconv.toeuc(elem.text)
end
end
=end
File.open("tmpxml.xml") do |fp|
doc = REXML::Document.new(fp.read).root
doc.elements.each('Items/Item') do |item|
attrs = item.elements['ItemAttributes']
imgs = item.elements['MediumImage']
title = Kconv.toeuc(attrs.elements['Title'].text)
author = Kconv.toeuc(attrs.elements['Author'].text)
company = Kconv.toeuc(attrs.elements['Manufacturer'].text)
image = imgs.elements['URL'].text
puts image
end
end
}[]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment