This Python 3.x script can be used for uploading of custom taxonomies. The script assumes that a dictionary instance has already been created and that one know the dictionary id. The dictionary can be a Excel file (in which case it must have an '.xslx' extension) or a JSON file (any extension but '.xslx' can then be used).
The format of the spreadsheet and the JSON file is described in Dictionary Management Tutorial
_username = "<YOUR CXENSE USER ID>"
_secret = "<YOUR CXENSE API KEY>"
_dictionaryId = "<YOUR DICTIONARY ID>"
_dictFilePath = "<YOUR DICTIONARY FILE PATH>"
import json, base64, datetime, hmac, hashlib, http.client
def cxApi(path, obj):
date = datetime.datetime.utcnow().isoformat() + "Z"
signature = hmac.new(_secret.encode('utf-8'), date.encode('utf-8'), digestmod=hashlib.sha256).hexdigest()
headers = {"X-cXense-Authentication": "username=%s date=%s hmac-sha256-hex=%s" % (_username, date, signature)}
connection = http.client.HTTPSConnection("api.cxense.com", 443)
connection.request("POST", path, json.dumps(obj), headers)
response = connection.getresponse()
status = response.status
responseObj = json.loads(response.read().decode('utf-8'))
connection.close()
return status, responseObj
def errorHandling(status, response, message):
if status != 200:
raise Exception("%s (http status = %s, error details: '%s')" % (message, status, response['error']))
def uploadDictionary(dictionaryId, dictFilePath):
if dictFilePath.lower().endswith('xlsx'):
requestObj = {"dictionaryId":dictionaryId, "workbook":base64.b64encode(open(dictFilePath, 'rb').read()).decode('ascii')}
else:
requestObj = {"dictionaryId":dictionaryId, "entries":json.loads(open(dictFilePath, 'rb').read().decode('utf-8-sig'))}
status, response = cxApi('/processing/dictionary/update', requestObj)
errorHandling(status, response, 'Unable to upload new version of dictionary')
if 'messages' in response and response['messages']:
print ('\n'.join(response['messages']))
uploadDictionary(_dictionaryId, _dictFilePath)