in src/plugins/utils/kpe.py [0:0]
def azureKPE(KibbleBit, bodies):
""" KPE using Azure Text Analysis API """
if 'azure' in KibbleBit.config:
headers = {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': KibbleBit.config['azure']['apikey']
}
js = {
"documents": []
}
# For each body...
a = 0
KPEs = []
for body in bodies:
# Crop out quotes
lines = body.split("\n")
body = trimBody(body)
doc = {
"language": "en",
"id": str(a),
"text": body
}
js['documents'].append(doc)
KPEs.append({}) # placeholder for each doc, to be replaced
a += 1
try:
rv = requests.post(
"https://%s.api.cognitive.microsoft.com/text/analytics/v2.0/keyPhrases" % KibbleBit.config['azure']['location'],
headers = headers,
data = json.dumps(js)
)
jsout = rv.json()
except:
jsout = {} # borked sentiment analysis?
if 'documents' in jsout and len(jsout['documents']) > 0:
for doc in jsout['documents']:
KPEs[int(doc['id'])] = doc['keyPhrases'][:5] # Replace KPEs[X] with the actual phrases, 5 first ones.
else:
KibbleBit.pprint("Failed to analyze email body.")
print(jsout)
# Depending on price tier, Azure will return a 429 if you go too fast.
# If we see a statusCode return, let's just stop for now.
# Later scans can pick up the slack.
if 'statusCode' in jsout:
KibbleBit.pprint("Possible rate limiting in place, stopping for now.")
return False
return KPEs