Git Product home page Git Product logo

amazon-json-api's Introduction

Amazon JSON API

amazon-json-api is a JSONP-enabled webservice that serves amazon.com’s product catalogue in an easily consumable JSON format.

You can use amazon-json-api to pull data from Amazon’s product catalogue and display it on your site.

The application uses Amazon’s ItemSearch and the amazon-ecs gem behind the scenes to pull in data from Amazon.

Installing

You’ll need Sinatra, Rack::Cache, amazon-ecs and ActiveSupport:


gem install sinatra rack-cache activesupport amazon-ecs

Configuring

Put your Amazon API key and secret in config.yml:


amazon:
  debug: true
  key: xxx
  secret: xxx

Results are by default cached in memory for 24 hours. You can tweak cache settings in config.yml:


cache:
  ttl: 86400
  metastore: heap:/
  entitystore: heap:/
  #metastore: file:/tmp/cache/rack/meta
  #entitystore: file:/tmp/cache/rack/body
  verbose: false

Launching

Starting the web server is as easy as executing this command:


$ ./app.rb

Tip: use Phusion Passenger and nginx in a production environment.

How to

All request parameters are forwarded as-is to the Amazon REST-API, which means this request:
http://localhost:4567/?response_group=Medium&search_index=Books&sort=salesrank&power=title:python+and+programming

Translates to the following ItemSearch query:
http://webservices.amazon.com/onca/xml?AWSAccessKeyId=xxx&Keywords=&Operation=ItemSearch&Power=title%3Apython%20and%20programming&ResponseGroup=Medium&SearchIndex=Books&Sort=salesrank&Timestamp=2010-04-16T19%3A18%3A47Z&Signature=xxx

Note how the signature, timestamp, and access key are all added automatically.

Examples

Search books and return JSON

This URL executes a power search for books having the words python and programming in the title:

http://localhost:4567/?response_group=Medium&search_index=Books&sort=salesrank&power=title:python+and+programming

The books are sorted by salesrank. Data is returned as JSON.

See the ItemSearch documentation for details.

Search books and return JSONP

The benefit of using JSONP is that you can host the webservice on one domain and use it on as many other domains as you need.

To search for books we use the same URL as the previous one with the addition of a callback parameter, which tells the webservice to return JSONP instead of JSON:
http://localhost:4567/?response_group=Medium&search_index=Books&sort=salesrank&power=title:python+and+programming&callback=showBooks

To retrieve the data you would add this code to your HTML:


<script defer="defer" type="text/javascript" src="http://localhost:4567/?response_group=ItemAttributes,Images&search_index=Books&sort=salesrank&power=title:xxx&callback=showBooks"></script>

Or the jQuery equivalent:


$.ajax({
    type: 'get',
    url: '/?your query goes here',
    dataType: 'jsonp',
    success: function(books) {
      showBooks(books);
    }
})

To render the data you would write a JSONP callback method such as this:


 <script type="text/javascript" defer="defer">
  function showBooks(books) {
    $.each(books, function(index, book) {
        var title = book.itemattributes.title;
        var url = book.detailpageurl;
        var image = book.mediumimage.url;
        var height = book.mediumimage.height;
        var width = book.mediumimage.width;

        // Do something smart here
    });
  }
</script>

Search the electronics department

This URL searches the electronics department for items containing the keyword “bluray”:
http://localhost:4567/?response_group=Medium&search_index=Electronics&sort=salesrank&keywords=bluray&callback=showResults

See the full list of search indexes for more options.

That’s it. You can see the webservice in action on this code snippets page.

Author

Christian Hellsten (Aktagon Ltd.)

amazon-json-api's People

Contributors

christianhellsten avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.