Tuesday, June 17, 2014

python call HP IDOL OnDemand

It's quit simple to use Python calling HP IDOL OnDemand APIs.
In Python 3.2.3. just use urlib to send http/https requst.
import urllib.parse
import urllib.request

I define this method to send request and parse the json result.
def getJson(url, params):
    data = urllib.parse.urlencode(params)
    data = data.encode('utf-8')
    req = urllib.request.Request(url, data)
    response = urllib.request.urlopen(req, timeout=500000)
    jsonstr = response.read().decode("utf-8", 'ignore');

    return json.loads(jsonstr)

e.g.

The url for Find Similar API is:  'https://api.idolondemand.com/1/api/sync/findsimilar/v1'
the params  for Find Similar API is: {'text' : phrase_text, 'print' : 'all', 'apikey' : apikey }

No comments:

Post a Comment