Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active September 15, 2016 08:47
Show Gist options
  • Save kurozumi/e812d630cc800e2633e4aa6ba3900c6d to your computer and use it in GitHub Desktop.
Save kurozumi/e812d630cc800e2633e4aa6ba3900c6d to your computer and use it in GitHub Desktop.
【Python】Amazon Simple Product APIをラップしてLookupによるデータの取得をさらにシンプルにしてみた。
# coding: utf-8
from amazon.api import AmazonException, LookupException, AsinNotFound
class AmazonAPILookupWrapper(object):
def __init__(self, account):
self.account = account
def __enter__(self):
from amazon.api import AmazonAPI
self.API = AmazonAPI(
self.account["ACCESS_KEY"],
self.account["SECRET_KEY"],
self.account["ASSOC_TAG"])
region = self.account["REGION"] if "REGION" in self.account else "JP"
return self
def __exit__(self, exception_type, ecception_value, traceback):
pass
def Lookup(self, **kwargs):
if "ItemId" not in kwargs:
raise AmazonException("ItemId Not Found")
if isinstance(kwargs["ItemId"], list):
kwargs["ItemId"] = ",".join(kwargs["ItemId"]
response = self.API.lookup(**kwargs)
if hasattr(response, "__iter__"):
for item in response:
self.item = item
yield self
else:
self.item = response
yield self
def setAsin(self, asin):
self.Asin = asin
# インチをセンチに変換
def inches2cm(self, data):
return round(float(data) / 100 * 2.54, 2)
# ポンドをグラムに変換
def pounds2g(self, data):
return round(float(data) / 100 * 0.4536 * 1000, 2)
@property
def region(self):
return self.API.region
@property
def title(self):
return self.item.title
# 大カテゴリを取得
@property
def category(self):
if len(self.item.browse_nodes) > 0:
return self.item.browse_nodes[0].ancestors[-1].name.text
else:
return None
@property
def image_url(self):
if self.item.medium_image_url:
return str(self.item.medium_image_url)
@property
def attributes(self):
return self.item.get_attributes([
"PackageDimensions.Weight",
"PackageDimensions.Width",
"PackageDimensions.Height",
"PackageDimensions.Length"
])
@property
def weight(self):
if "PackageDimensions.Weight" in self.attributes:
return self.pounds2g(self.attributes["PackageDimensions.Weight"])
@property
def raw_weight(self):
if "PackageDimensions.Weight" in self.attributes:
return self.attributes["PackageDimensions.Weight"]
@property
def width(self):
if "PackageDimensions.Width" in self.attributes:
return self.inches2cm(self.attributes["PackageDimensions.Width"])
@property
def raw_width(self):
if "PackageDimensions.Width" in self.attributes:
return self.attributes["PackageDimensions.Width"]
@property
def height(self):
if "PackageDimensions.Height" in self.attributes:
return self.inches2cm(self.attributes["PackageDimensions.Height"])
@property
def raw_height(self):
if "PackageDimensions.Height" in self.attributes:
return self.attributes["PackageDimensions.Height"]
@property
def length(self):
if "PackageDimensions.Length" in self.attributes:
return self.inches2cm(self.attributes["PackageDimensions.Length"])
@property
def raw_length(self):
if "PackageDimensions.Length" in self.attributes:
return self.attributes["PackageDimensions.Length"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment