Pythonのメモ

google app engineAWS から書籍名を取得。書籍の場合は ASIN = ISBN

import xml.etree.cElementTree as etree
from google.appengine.api import urlfetch

def getBookInfo(itemId):
  class Book():
    title  = ''
    author = ''
    detailPageUrl = ''

  awsKey = "xxxxxxxxxxxxxxxxxxxx"

  query = 'http://webservices.amazon.co.jp/onca/xml'
  query += '?Service=AWSECommerceService&'
  query += '&AWSAccessKeyId=' + awsKey
  query += '&Operation=ItemLookup'
  query += '&ContentType=text/xml'
  query += '&ResponseGroup=Medium,Images'
  query += '&Page=1'
  query += '&ItemId=' + itemId

  xml = urlfetch.fetch(query).content

  dom = etree.fromstring(xml)

  aws = '{http://webservices.amazon.com/AWSECommerceService/2005-10-05}'
  xpath_Item            = aws + 'Items/' + aws + 'Item/'
  xpath_ASIN            = aws + 'ASIN'
  xpath_DetailPageURL   = aws + 'DetailPageURL'
  xpath_ItemAttributes  = aws + 'ItemAttributes/'
  xpath_Title           = xpath_ItemAttributes + aws + 'Title'
  xpath_Author          = xpath_ItemAttributes + aws + 'Author'
  xpath_Publisher       = xpath_ItemAttributes + aws + 'Publisher'
  xpath_PublicationDate = xpath_ItemAttributes + aws + 'PublicationDate'

  items = dom.findall(xpath_Item)
  books = []
  for item in items:
    book = Book()
    book.title  = item.findtext(xpath_Title)
    book.author = item.findtext(xpath_Author)
    book.detailPageUrl = item.findtext(xpath_DetailPageURL)
    books.append(book)

  return books

books = getBookInfo('4798016411,4774130567,4873112761,4774130427,479733939X')
for book in books:
  print book.title
  print book.author
  print book.detailPageUrl