vendredi 4 septembre 2015

Books with low SalesRank scores returned with parameter Sort=salesrank with Amazon Product Advertising Api

I'm trying to get the top 100 top-selling books using the Amazon Product Advertising API.

I used the ItemSearch method with an increasing ItemPage attribute (from 1 - 10) and Sort=salesrank. This returned 100 books (as expected) however the ranks were all over the place--while most were in the top ~30 or so, some books had rankings in the 700s.

My code (in Python) below:

from datetime import datetime, date, time
import hmac
import hashlib
import base64
import urllib.request
from xml.etree import ElementTree as ET
all_books = []
import json


num = 1 //the itemPage


key = 'KEY'
secret_key = 'SECRETKEY'
as_id = 'ID'

while num < 11:

    current_date = datetime.utcnow().strftime("%Y-%m-%dT%H%%3A%M%%3A%SZ")
url = """http://ift.tt/1zb16xr""" + key + """&AssociateTag=""" + as_id + """&ItemPage=""" + \
      str(num) + """&Operation=ItemSearch&ResponseGroup=Medium&SearchIndex=Books""" + """&BrowseNode=1000""" +\
      """&Sort=salesrank"""+ """&Timestamp=""" + str(current_date) + """&Version=2013-08-01"""

url_to_sign = "AWSAccessKeyId=""" + key + """&AssociateTag=""" + as_id + """&BrowseNode=1000&ItemPage=""" + \
      str(num) + """&Operation=ItemSearch&ResponseGroup=Medium&SearchIndex=Books""" +\
      """&Service=AWSECommerceService&Sort=salesrank"""+ """&Timestamp=""" + str(current_date) + """&Version=2013-08-01"""

encoded_url = url_to_sign

stringToSign = "GET" + "\n" + "webservices.amazon.com" + "\n" + "/onca/xml" + "\n" + str(encoded_url)

dig = hmac.new(b'SECRETKEY', msg=stringToSign.encode('utf-8'), digestmod=hashlib.sha256).digest()

sig = base64.b64encode(dig).decode('utf-8')
sig = sig.replace("+", "%2B").replace("=", "%3D")

url = url + "&Signature=" + sig


data = ET.fromstring(urllib.request.urlopen(url).read())
//parse responses and do stuff with the data

num += 1

(Excuse the messed up indenting please! If it's an eyesore, I can spend some time formatting.)

I've also tried the BrowseNodeGroup method (which seems to be the recommended approach for fetching top sellers), however the ItemPage attribute doesn't seem to do anything (though I've seen responses on other threads claiming otherwise).

If you have any idea how I can get a nice list of books with 1 - 100 salesRank (or tell me why this is impossible), I'd appreciate it.




Aucun commentaire:

Enregistrer un commentaire