とりあえずAmazonラッパー。

伸ばしたり縮めたり回転させたりする画像を取ってくるためにとりあえずラッパーを作りました。
ASINを使っての単独アイテム取得と類似アイテム取得に対応してます。
とりあえず版なのでエラーを考慮してませんがせっかくなので大公開。
log関数は↓を使わせていただいてます。

WEB+DB PRESS Vol.41:サポートページ|gihyo.jp … 技術評論社

47ページ「log関数でデバッグ」のlog関数

使い方はMain.asをどうぞ。

あと気になったことなど。

  • いつのまにかECSがA2Sに変わってた。
  • E4Xでのnamespaceでハマりにハマった。(詳しくは[ref.])

[ref.] re: E4X namespace – PBD – subtech

Main.as

package {
import flash.display.Sprite;
import jp.slz.actionscript.amazon.A2S;
import jp.slz.actionscript.amazon.Item;

public class Main extends Sprite {
public function Main() {
var a2s:A2S = new A2S('YOUR_AWS_ACCESS_KEY_ID', 'YOUR_ASSOCIATE_TAG');

/*
a2s.lookupItem('0977616630', function(item:Item):void {
logItem(item);
});
*/
/*
a2s.lookupSimilarity('0596526946', function(items:Array):void {
for each (var item:Item in items) {
logItem(item);
}
});
*/
}

private function logItem(item:Item):void {
log(item.asin);
log(item.title);
log(item.smallImage.url);
log(item.smallImage.height);
log(item.smallImage.width);
log(item.mediumImage.url);
log(item.mediumImage.height);
log(item.mediumImage.width);
log(item.largeImage.url);
log(item.largeImage.height);
log(item.largeImage.width);
for each (var creator:Object in item.creators) {
log(creator.name);
log(creator.role);
}
log(item.listPrice.amount);
log(item.listPrice.currencyCode);
log(item.publisher);
log(item.publicationDate);
}
}
}


jp/slz/actionscript/amazon/A2S.as

package jp.slz.actionscript.amazon {
import flash.events.Event;
import flash.external.ExternalInterface;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLVariables;
import com.adobe.utils.StringUtil;

/**
* Make easy to accessing to A2S.
*/
public class A2S {
public static const API_VERSION:String = '2007-10-29';
public static const XMLNS:String = 'http://webservices.amazon.com/AWSECommerceService/2007-10-29';

private var awsAccessKeyId:String;
private var associateTag:String;

public function A2S(awsAccessKeyId:String, associateTag:String = '') {
this.awsAccessKeyId = awsAccessKeyId;
this.associateTag = associateTag;
}

/**
* Lookup item by asin.
*
* @param itemId Just asin.
*/
public function lookupItem(itemId:String, itemHandler:Function):void {
itemId = StringUtil.trim(itemId);

var request:URLRequest = createRequest();
(request.data as URLVariables).Operation = 'ItemLookup';
(request.data as URLVariables).ItemId = itemId;
(request.data as URLVariables).ResponseGroup = 'Images,ItemAttributes';

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, function(e:Event):void {
var ilr:XML = new XML(e.target.data);   // ItemLookupResponse

default xml namespace = new Namespace(XMLNS);

var item:Item = generateItem(ilr.Items.Item[0]);

itemHandler(item);
});
loader.load(request);
}

/**
* Lookup similer item by asin.
*
* @param itemId Just asin.
*/
public function lookupSimilarity(itemId:String, similarityHandler:Function):void {
itemId = StringUtil.trim(itemId);

var request:URLRequest = createRequest();
(request.data as URLVariables).Operation = 'SimilarityLookup';
(request.data as URLVariables).ItemId = itemId;
(request.data as URLVariables).ResponseGroup = 'Images,ItemAttributes';

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, function(e:Event):void {
var slr:XML = new XML(e.target.data);   // SimilarityLookupResponse

default xml namespace = new Namespace(XMLNS);

var items:Array = [];
for each (var itemXML:XML in slr.Items.Item) {
items.push(generateItem(itemXML));
}

similarityHandler(items);
});
loader.load(request);
}

/**
* Creates request with common variables.<br/>
* <strong>Need to add rest of variables. (*Operation* and propers.)</strong>
*
* @return Request for A2S
*/
private function createRequest():URLRequest {
var request:URLRequest = new URLRequest('http://ecs.amazonaws.jp/onca/xml');

var variables:URLVariables = new URLVariables();
variables.AssociateTag = associateTag;
variables.AWSAccessKeyId = awsAccessKeyId;
variables.Service = 'AWSECommerceService';
variables.Version = API_VERSION;

request.data = variables;

return request;
}

/**
* Generates item from xml.
*
* @param itemXML Item node
*
* @return Item
*/
private function generateItem(itemXML:XML):Item {
/* By unknown reason, this code doesn't work here.
default xml namespace = itemXML.namespace();
*/
default xml namespace = new Namespace(XMLNS);

var item:Item = new Item();
item.asin = itemXML.ASIN.toString();
item.title = itemXML.ItemAttributes.Title.toString();
item.smallImage = {url: itemXML.SmallImage.URL.toString(),
height: itemXML.SmallImage.Height.toString(),
width: itemXML.SmallImage.Width.toString()};
item.mediumImage = {url: itemXML.MediumImage.URL.toString(),
height: itemXML.MediumImage.Height.toString(),
width: itemXML.MediumImage.Width.toString()};
item.largeImage = {url: itemXML.LargeImage.URL.toString(),
height: itemXML.LargeImage.Height.toString(),
width: itemXML.LargeImage.Width.toString()};
item.creators = [];
for each (var creator:XML in itemXML.ItemAttributes.Creator) {
item.creators.push({name: creator.toString(), role: creator.@Role.toString()});
}
item.listPrice = {amount: itemXML.ItemAttributes.ListPrice.Amount.toString(),
currencyCode: itemXML.ItemAttributes.ListPrice.CurrencyCode.toString()};
item.publisher = itemXML.ItemAttributes.Publisher.toString();
item.publicationDate = itemXML.ItemAttributes.PublicationDate.toString();

return item;
}
}
}

jp/slz/actionscript/amazon/Item.as

package jp.slz.actionscript.amazon {
/**
* ValueObject for item.
*/
public class Item {
private var _asin:String;
private var _title:String;
private var _smallImage:Object;
private var _mediumImage:Object;
private var _largeImage:Object;
private var _creators:Array;
private var _listPrice:Object;
private var _publisher:String;
private var _publicationDate:String;

public function set asin(asin:String):void {
this._asin = asin;
}
public function get asin():String {
return this._asin;
}

public function set title(title:String):void {
this._title = title;
}
public function get title():String {
return this._title;
}

public function set smallImage(smallImage:Object):void {
this._smallImage = smallImage;
}
public function get smallImage():Object {
return this._smallImage;
}

public function set mediumImage(mediumImage:Object):void {
this._mediumImage = mediumImage;
}
public function get mediumImage():Object {
return this._mediumImage;
}

public function set largeImage(largeImage:Object):void {
this._largeImage = largeImage;
}
public function get largeImage():Object {
return this._largeImage;
}

public function set creators(creators:Array):void {
this._creators = creators;
}
public function get creators():Array {
return this._creators;
}

public function set listPrice(listPrice:Object):void {
this._listPrice = listPrice;
}
public function get listPrice():Object {
return this._listPrice;
}

public function set publisher(publisher:String):void {
this._publisher = publisher;
}
public function get publisher():String {
return this._publisher;
}

public function set publicationDate(publicationDate:String):void {
this._publicationDate = publicationDate;
}
public function get publicationDate():String {
return this._publicationDate;
}
}
}

コメント

  1. virgoboy より:

    いやぁ、ためになりました。使わせていただきます。

  2. yoshuki より:

    どういたしまして!お役に立てたようでよかったです。

タイトルとURLをコピーしました