Amazon Product Advertising API

  • written by member/すながわひろゆき
  • amazon.co.jpとかamazon.comの商品DBにアクセスしまくるAPI
    • 作りたいものがあってPerlで使うよ!
    • せっかくだからメモを残すよ!
  • Amazon Product Advertising API
    • 旧Amazon ECS
      • 旧旧Amazon AWS
  • みんながやりたい方題しすぎて制約が増えまくってる(^^;
    • アクセス量とか規制されているみたい。

準備

  • Net::Amazonインストール
    • yum install perl-XML-Simple
      • ちなみにperl-coreインストール済み。
    • CPANでNet::AmazonとCache::Fileをインストールする。
      • AWSの規約でキャッシュを実装する条件がついているので、同じクエリを連続して送ることは禁止されている。

Perlで引っ張る

#!/usr/bin/perl
use Net::Amazon;
use Cache::File;

&amazon_testcode;
exit 0;

sub amazon_testcode {
  
  # ファイルキャッシュモジュール
  my $cache = Cache::File->new(
    # キャッシュ用ディレクトリ
    cache_root => '/home/test/public_html/amazon_testcode/cache',
    # キャッシュ時間 (ex: '3 min', '1 hour'...)
    default_expires => '24 hour',
  );
  # Net::Amazonモジュール
  my $ua = Net::Amazon->new(
    token      => '(AWSのアクセスキーID)',
    secret_key => '(AWSのシークレットアクセスキー)',
    locale => 'jp',
    # ファイルキャッシュオブジェクト
    cache => $cache,
  );
  
  # 検索
  my $response = $ua->search(isbn => '9784832240575', AssociateTag => 'jamfunk-22');
  
  # 返値があったか?
  if($response->is_success()) {
    
    # 件数
    print "### total_results: " . $response->total_results() . "\n\n";
    # 複数返値ループ
    for($response->properties) {
      # The item's ASIN number.
      print "ASIN:", $_->ASIN(), "\n";
      # Book title, CD album name or item name. This option is actually an alias
      # for the method title, and is actually dependent upon the type of item
      # returned.
      print "ProductName:", $_->ProductName(), "\n";
      # Text string describing if the item is available. Examples: "Usually
      # ships within 24 hours" or "Out of Print--Limited Availability".
      print "Availability:", $_->Availability(), "\n";
      # Shows the catalog the item was found in: Book, Music, Classical,
      # Electronics etc.
      print "Catalog:", $_->Catalog(), "\n";
      # [book] Returns a list of the book's authors. There's also a author()
      # method which just returns the first author.
      print "authors:", join("," , $_->authors()), "\n";
      # [book] Returns the book's title as a string.
      print "title:", join("," , $_->title()), "\n";
      # Item's release date, format is "NN Monthname, Year".
      print "ReleaseDate:", $_->ReleaseDate(), "\n";
      #publication_date()
      print "publication_date:", $_->publication_date(), "\n";
      # Music label, publishing company or manufacturer
      print "Manufacturer:", $_->Manufacturer(), "\n";
      # (book) Returns the book's publishing company as a string. 
      print "publisher:", $_->publisher(), "\n";
      # URL to a small (thumbnail) image of the item
      print "ImageUrlSmall:", $_->ImageUrlSmall(), "\n";
      # URL to a medium-size image of the item
      print "ImageUrlMedium:", $_->ImageUrlMedium(), "\n";
      # URL to a large image of the item
      print "ImageUrlLarge:", $_->ImageUrlLarge(), "\n";
      # List price of the item
      print "ListPrice:", $_->ListPrice(), "\n";
      # Amazon price of the item
      print "OurPrice:", $_->OurPrice(), "\n";
      # Used price of the item
      print "UsedPrice:", $_->UsedPrice(), "\n";
      # Unformatted list price as an integer, without currency symbol.
      print "RawListPrice:", $_->RawListPrice(), "\n";
      # The currency code for the "ListPrice()", e.g. USD.
      print "CurrencyCode:", $_->CurrencyCode(), "\n";
      # Sales rank of the item (contains digits and commas, like 1,000,001)
      print "SalesRank:", $_->SalesRank(), "\n";
      # Type of media (Paperback, etc.).
      print "Media:", $_->Media(), "\n";
      # Number of media the item carries (1,2 CDs etc.).
      print "NumMedia:", $_->NumMedia(), "\n";
      # Lengthy textual description of the product.
      print "ProductDescription:", $_->ProductDescription(), "\n";
      # Lowest price in "Collectible" category.
      print "CollectiblePrice:", $_->CollectiblePrice(), "\n";
      # Number of offerings in "Collectible" category.
      print "CollectibleCount:", $_->CollectibleCount(), "\n";
      # Total number of offerings in all categories.
      print "NumberOfOfferings:", $_->NumberOfOfferings(), "\n";
      # Number of offerings in "Used" category.
      print "UsedCount:", $_->UsedCount(), "\n";
      # Number of offerings of the product.
      print "TotalOffers:", $_->TotalOffers(), "\n";
      # Lowest price in "Third Party New" category.
      print "ThirdPartyNewPrice:", $_->ThirdPartyNewPrice(), "\n";
      # Number of offerings in "Third Party New" category.
      print "ThirdPartyNewCount:", $_->ThirdPartyNewCount(), "\n";
      # Return the width of the small image in pixels.
      print "SmallImageWidth:", $_->SmallImageWidth(), "\n";
      # Return the height of the small image in pixels.
      print "SmallImageHeight:", $_->SmallImageHeight(), "\n";
      # Return the width of the medium image in pixels.
      print "MediumImageWidth:", $_->MediumImageWidth(), "\n";
      # Return the height of the medium image in pixels.
      print "MediumImageHeight:", $_->MediumImageHeight(), "\n";
      # Return the width of the large image in pixels.
      print "LargeImageWidth:", $_->LargeImageWidth(), "\n";
      # Return the height of the large image in pixels.
      print "LargeImageHeight:", $_->LargeImageHeight(), "\n";
      # Boolean value that indicates if the product is eligible for super saver shipping.
      print "SuperSaverShipping:", $_->SuperSaverShipping(), "\n";
      # The release year extracted from ReleaseDate().
      print "year:", $_->year(), "\n";
      # Returns a list of browse nodes (text string categories) for this item.
      print "browse_nodes:", join("," , $_->browse_nodes()), "\n";
      # Returns a list of ASINs of similar items for this item.
      print "similar_asins:", join("," , $_->similar_asins()), "\n";
      # [book] Returns the book's ISBN number. 
      print "isbn:", $_->isbn(), "\n";
      # [book] Returns the book's EAN number.
      print "ean:", $_->ean(), "\n";
      # [book] Returns the book's edition.
      print "edition:", $_->edition(), "\n";
      # [book] Returns the number of pages.
      print "numpages:", $_->numpages(), "\n";
      print "\n";
    }
  # 返値がない, エラー
  } else {
    print "Error: ", $response->message(), "\n";
  }
}
  • 結果
    [test@Sawara3 booklive]$ ./test_amazon.pl
    ### total_results: 1
    
    ASIN:4832240579
    ProductName:ひだまりスケッチ (6) (まんがタイムKRコミックス)
    Availability:在庫あり。
    Catalog:Book
    authors:蒼樹 うめ
    title:ひだまりスケッチ (6) (まんがタイムKRコミックス)
    ReleaseDate:2011-08-27
    publication_date:2011-08-27
    Manufacturer:芳文社
    publisher:芳文社
    ImageUrlSmall:http://ecx.images-amazon.com/images/I/51WWQ9FdI4L._SL75_.jpg
    ImageUrlMedium:http://ecx.images-amazon.com/images/I/51WWQ9FdI4L._SL160_.jpg
    ImageUrlLarge:http://ecx.images-amazon.com/images/I/51WWQ9FdI4L.jpg
    ListPrice:¥ 860
    OurPrice:¥ 860
    UsedPrice:¥ 600
    RawListPrice:860
    CurrencyCode:JPY
    SalesRank:
    Media:コミック
    NumMedia:
    ProductDescription:
    CollectiblePrice:
    CollectibleCount:0
    NumberOfOfferings:4
    UsedCount:3
    TotalOffers:1
    ThirdPartyNewPrice:¥ 860
    ThirdPartyNewCount:1
    SmallImageWidth:53
    SmallImageHeight:75
    MediumImageWidth:113
    MediumImageHeight:160
    LargeImageWidth:352
    LargeImageHeight:500
    SuperSaverShipping:1
    year:2011
    browse_nodes:コミック,漫画・アニメ・BL,ジャンル別,本,芳文社,By Publishers,本,コ
    ミック,Format (binding),Browse Refinements,Refinements,本,まんがタイムKRコミック
    ス,4コマレーベル,Comic & BL Label (feature_three_browse-bin),Browse Refinements,
    Refinements,本,ひだまりスケッチ,Series 5,Series (lbr_two_browse-bin),Browse Refi
    nements,Refinements,本,芳文社,Publishers 3,Publishers (lbr_publishers_browse-bin
    ),Browse Refinements,Refinements,本,まんがタイムKRコミックス,4コマレーベル,Custo
    m Stores,Self Service,本
    similar_asins:4832240587,4832278975,4785936908,4832240595,4832277626
    isbn:4832240579
    ean:9784832240575
    edition:
    numpages:120
    
    [test@Sawara3 booklive]$

気づいたこと

  • search(isbn => '9784063144505')問題?(勝手に命名)
    • あほな業者(?)Amazon(?)が中古コミックセットをISBN-13: 9784063144505で出品している。
    • もちろん正しい "くじびきアンバランス 1 (アフタヌーンKC)" もISBN-13: 9784063144505である。
    • search(isbn => '9784063144505')とするとコミックセットの1件しか返さない。
    • search(ean => '...')は無い。
    • 正規の商品が検索できない。
    • 対策
    • search(keyword=> '9784063144505')で検索する。
    • 複数結果が返ってくるため、for($responce->properties)でループさせている。
    • 副作用は未知。これから1000冊検索して確認する予定。
  • properties->url()は公式perldocは説明が書いていない。
    • いちおうDetailPageUrl(Amazonの商品ページURL)を引っ張ってくる。長い。
  • AssociateTag => 'xxxx'
    • 2012.02.05
    • 2012.02.21からデータを引っ張る際にAssociateTagパラメータを指定しないと情報を引っ張れない仕様になったらしい。(たぶんAWSのメールマガジンで配信されてたはず)
    • Amazon Associate ProgramでトラッキングIDを取得してリクエスト時に指定すること。

参考サイト