I've been playing with the recently-released HTTP API for accessing the Best Buy product catalog. While its a little strange to use at first, its actually pretty useful. One of the things I am interested in is online retail, specifically how to make Internet shopping easier. Lets imagine I am looking for information on a particular digital camera - the Nikon Coolpix S210.
A Python skeleton
First, lets get our little Python test harness together. Also, you are going to need your own Best Buy Remix API key. Here is a skeletal Python HTTP client:
import httplib API_KEY='Save the above to a file like bb.py.
' QUERY="/v1/products(name=Coolpix*&modelNumber=S210)" OPTS="?sort=name.desc&show=all&format=json" c = httplib.HTTPConnection('api.remix.bestbuy.com') c.request('GET', "%s%s&apiKey=%s" %(QUERY, OPTS, API_KEY)) r = c.getresponse() data = r.read() print data
Our first Best Buy query
Now lets try to write a sample query for the Nikon Coolpix S210. Although the Best Buy Remix API docs are a bit sparse, we can guess that items must have an attribute called 'name'. In fact they do! So lets try searching for the camera by name.
# Same code as above, but we change the value of QUERY: QUERY="/v1/products(name=Nikon Coolpix S210)"Looks pretty reasonable. Unfortunately, Best Buy is going to give us back a 400 error:
$ python bb.py <?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>400 - Bad Request</title> </head> <body> <h1>400 - Bad Request</h1> </body> </html>
It turns out that Best Buy don't name their products in the most intuitive way. Lets try a wildcard on just `Coolpix' instead:
# Same code as above, but we change the value of QUERY: QUERY="/v1/products(name=Coolpix*)"
This time, we are going to get tons of data back, in JSON format. Best Buy remix defaults to XML, but I prefer JSON so I added the format=json parameter to the query. Ok, so now we have an overwhelming amount of data on Coolpix cameras - but we really just want information for the S210.
Best Buy's quirky product schema
Well, there is a solution. Best Buy don't store the model number in the `name' attribute - instead they store it in a separate `modelNumber' attribute. If we query for name=Coolpix* AND modelNumber=S210, we should get the expected result, finally:
# Same code as above, but we change the value of QUERY: QUERY="/v1/products(name=Coolpix*&modelNumber=S210)"
Et voila! Now Best Buy gives us back all the information it has about the Nikon Coolpix S210. This is pretty detailed stuff, including all those details like compatible memory formats, digital zoom, along with the price and availability. Very cool! Just for kicks, lets show the whole script to send a query to Best Buy, parse the JSON response, and finally print the price:
import httplib import json API_KEY='And we run it:
' QUERY="/v1/products(name=Coolpix*&modelNumber=S210)" OPTS="?sort=name.desc&show=all&format=json" c = httplib.HTTPConnection('api.remix.bestbuy.com') c.request('GET', "%s%s&apiKey=%s" %(QUERY, OPTS, API_KEY)) r = c.getresponse() data = r.read() camera_info = json.loads(data) print "price: %s"%(camera_info['products']['regularPrice'])
$ python bb.py price: 89.99Whee!
Niall O'Higgins is an author and software developer. He wrote the O'Reilly book MongoDB and Python. He also develops Strider Open Source Continuous Deployment and offers full-stack consulting services at FrozenRidge.co.Tweet