Created
June 1, 2009 10:17
-
-
Save liquidz/121338 to your computer and use it in GitHub Desktop.
nicobook.rb
This file contains 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 '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