The Friends of the Alum Rock library sells books via Amazon to raise money for library programs. We thought it would be fun to see where we send the books.
Amazon makes it surprisingly hard to get programmatic access to order information. They do have an API (Amazon Marketplace Web Service), but it's only available to business sellers.
I wrote a Python script using mechanize that logs in to our seller account, goes to the order page, and clicks each of the orders to get to the order detail page. From there, I used regular expressions to extract the relevant order info (shipping address, date, price, and title). The HTML is not very well marked up, so this is likely to break. I use the MapQuest geocoding API to convert the mailing address to latitude/longitude so it can be mapped easily. I save this data in GeoJSON format, and update it whenever an order comes in. The orders.json
file contains some sample data but is not updated automatically.
I used MapBox and Leaflet to display the GeoJSON orders data on a map, with a jQuery UI slider to filter orders by price.
I wanted custom markers created from GeoJSON data that can be filtered by the user. This was harder than I expected:
- adding markers with L.geoJson doesn't add them to a
L.mapbox.featureLayer
that can respond to events - creating markers using
L.mapbox.featureLayer
doesn't allow customizing the markers L.mapbox.featureLayer.setFilter
doesn't run for markers added manually to the feature layer
Here's how I got it working:
- create a feature layer with no data:
var featureLayer = L.mapbox.featureLayer(null);
- load the geoJSON data with jQuery
$.getJSON
- iterate through the data and create a
CircleMarker
for each feature, with color and size a function of age and sale price - add the marker to the feature layer
- save the
CircleMarker
objects in an array keyed by orderID - in the price filter slider's
change
function, clear all the markers from the feature layer - for each feature that matches the selected price criteria, lookup the saved layer and add it to the feature layer