Amazon Web Service に ISBN で問い合わせしてみる。
登録IDの取得
http://www.amazon.co.jp/gp/feature.html?docId=451209
オンラインドキュメントトップページ
http://www.amazon.com/gp/browse.html/?node=3487571
オンラインドキュメントトップページから迷子になった後にやっとたどり着いたページ
http://docs.amazonwebservices.com/AWSEcommerceService/2007-02-22/
問い合わせ先:ecs.amazonaws.jp
目的のXML :
Medium Response Group
http://docs.amazonwebservices.com/AWSEcommerceService/2007-02-22/ApiReference/MediumResponseGroup.html
獲得したい情報:
URL
表紙URL
著者名
ISBN
出版社名
出版日付
タイトル
価格
# isbn_lookup.rb
=begin
The Base URL
# http://docs.amazonwebservices.com/AWSEcommerceService/2007-02-22/PgRestRequestsArticle.html
# For Amazon.co.jp (JP) => http://ecs.amazonaws.jp/onca/xml?Service=AWSECommerceService
ItemSearch Operation
# http://docs.amazonwebservices.com/AWSEcommerceService/2007-02-22/ApiReference/ItemSearchOperation.html
Medium Response Group
# http://docs.amazonwebservices.com/AWSEcommerceService/2007-02-22/ApiReference/MediumResponseGroup.html>
The Request that Generated the Response (REST)
http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService
# http://ecs.amazonaws.jp/onca/xml?Service=AWSECommerceService
&AWSAccessKeyId=[Your Access Key ID Here]
&AssociateTag=[Your Associate ID Here]
&Operation=ItemLookup
&ItemId=B00008OE6I
&ResponseGroup=Medium
=end
require 'uri'
require 'net/http'
require "rexml/document"
module Amazon
class ISBN_Lookup
def initialize(aws_accessKeyId, associate_ID)
@aws_key = aws_accessKeyId
@aws_id = associate_ID
@jp_ecs = 'http://ecs.amazonaws.jp'
end
def seturi(isbn)
uri = URI.parse(@jp_ecs)
uri.path='/onca/xml'
q = [
"Service=AWSECommerceService",
"AWSAccessKeyId=#{@aws_key}",
"Operation=ItemLookup",
"ItemId=#{isbn}",
"AssociateTag=#{@aws_id}",
"ResponseGroup=Medium"
]
uri.query = q.join("&")
return uri
end
end
class Access
def initialize(uri)
@host = uri.host
@request = uri.request_uri
end
def error_message
print "SocketError\n"
end
def reponse
begin
Net::HTTP.start(@host) { |http|
response = http.get(@request)
return response.body
}
rescue SocketError, error_message
end
end
end
class Book < Struct.new(:detailpageurl, :mediumimage_url, :author, :isbn, :publisher, :publicationdate, :title, :amount)
def initialize(res)
@doc = REXML::Document.new(res) unless res.nil?
@itemAttributes = @doc.root.elements['Items/Item/ItemAttributes'] unless res.nil?
end
def setup
return nil if @doc.nil?
return nil if @itemAttributes.nil?
@itemAttributes.to_a.each{|x|
xx = x.name.downcase
self[:amount] = x.text('Amount') if xx.to_s == "listprice"
unless members.index(xx.to_s).nil?
sym = xx.to_sym #symbol
self[sym] = x.text
end
}
self.detailpageurl = urldegital
self.mediumimage_url = urlimg
return self
end
def get(xpath)
@doc.root.elements[xpath].text unless @doc.root.elements[xpath].nil?
end
def urldegital
get('Items/Item/DetailPageURL')
end
def urlimg
get('Items/Item/MediumImage/URL')
end
end
end
#+++++++++++++++++++++++++
#example;
# ISBN :4063406482
aws_key = xxxxxxx
aws_id = xxxxxxx
isbn = '4063406482'
uri = Amazon::ISBN_Lookup.new(aws_key, aws_id).seturi(isbn)
res = Amazon::Access.new(uri).reponse
book = Amazon::Book.new(res)
p book.setup
#exit
puts book.mediumimage_url
#=> http://ec1.images-amazon.com/images/I/21po5oj5XOL.jpg
puts book.author
#=> 二ノ宮 知子
puts book.isbn
#=> 4063406482
puts book.publisher
#=> 講談社
puts book.publicationdate
#=> 2007-06-13
puts book.title
#=> のだめカンタービレ #18 (18)
puts book.amount
#=> 410
これをもとにエントリーに書籍リンクをはるとこまでが目的。。。
--imported_from
http://www.midore.net/daybook/2007/06/1180989126.html
0 件のコメント:
コメントを投稿