Tuesday, June 17, 2014

Python call OnDemand indexing and search

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)

The following APIs will be used
createtextindex_url = 'https://api.idolondemand.com/1/api/sync/createtextindex/v1'
storeobject_url = 'https://api.idolondemand.com/1/api/sync/storeobject/v1'
viewdocument_url = 'https://api.idolondemand.com/1/api/sync/viewdocument/v1'
findrelated_url = 'https://api.idolondemand.com/1/api/sync/findrelatedconcepts/v1'
findsimilar_url = 'https://api.idolondemand.com/1/api/sync/findsimilar/v1'

The following option will be used.
params_createtextindex = {'index' : text, 'flavor' : 'explorer', 'apikey' : apikey }
params_storeobject = {'url' : doc_url, 'apikey' : apikey }
params_viewdocument = {'url' : doc_url, 'highlight_expression': 'physical activity', 'start_tag': '<b>', 'apikey' : apikey }

params_findrelated = {'url' : doc_url, 'apikey' : apikey }

No comments:

Post a Comment