#!/usr/bin/env python #-*- coding: utf-8 -*- import re import cgi import urllib import hashlib, hmac import base64 import sys import datetime from google.appengine.api import users from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app from google.appengine.ext.webapp.util import login_required from google.appengine.ext import db from google.appengine.ext.db import Key def id_filter(str): ids = re.findall("[0-9]{13}", str) if not ids: ids = re.findall("[0-9A-Z]{10}", str) ids = sorted(set(ids), key=ids.index) if (len(ids)>100): ids = ids[0:100] return ids def amazon_req(ids, country): idNum=len(ids) loop = (idNum-1)/10+1 xmlstr='' key1='AWSAccessKeyId=AKIAJSHFPCDM5YU44Z2A' key2='H2bSwA4H/eM6mPsS5SSbqZuC1dekk7q8u5NaU8pA' associate='AssociateTag=mahiroo-22' sign_head='GET\necs.amazonaws.jp\n/onca/xml\n' reqhead='http://ecs.amazonaws.jp/onca/xml?' service='Service=AWSECommerceService' operation='Operation=ItemLookup' version='Version=2010-06-01' response='ResponseGroup=Medium' searchindex='SearchIndex=All' if len(ids[0])==13: idtype='IdType=EAN' elif len(ids[0])==10: idtype='IdType=ASIN' for l in range(loop): id=','.join(map(str,ids[l*10:l*10+10])) id_quo=urllib.quote(id) itemid='ItemId=%s' % id_quo ymd=datetime.datetime.utcnow().strftime("Timestamp=%Y-%m-%d") hms=datetime.datetime.utcnow().strftime("T%H:%M:%SZ") hms=urllib.quote(hms) timestamp=ymd+hms if idtype=='IdType=EAN': request=[timestamp, service, operation, version, response, idtype, itemid, searchindex, associate] elif idtype=='IdType=ASIN': request=[timestamp, service, operation, version, response, idtype, itemid, associate] reqSorted =sorted(request) reqJoined=key1+'&'+'&'.join(reqSorted) reqJoined2=sign_head+reqJoined hmac_digest = hmac.new(key2, reqJoined2, hashlib.sha256).digest() base64_encoded = base64.b64encode(hmac_digest) result = urllib.quote(base64_encoded) reqstr = reqhead + reqJoined + '&Signature=' + result xmlfile=urllib.urlopen(reqstr) xmlJoin=xmlfile.read() xmlstr += xmlJoin xmlstr = re.sub('\n','',xmlstr) xmlstr = re.sub('<\?xml version="1.0" \?>', "", xmlstr) xmlstr = re.sub('', "", xmlstr) return '' + xmlstr +'' class Jp(webapp.RequestHandler): def get(self, codes): ids = id_filter(codes) xml = amazon_req(ids, 'jp') ##self.response.headers['Content-Type']='text/xml' self.response.out.write(xml) return 0 def main(): application = webapp.WSGIApplication( [('/amazonjp/(.*)', Jp), ], debug=False) run_wsgi_app(application) if __name__ == "__main__": main()