forked from cyberdelia/kales
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kales.py
47 lines (41 loc) · 1.67 KB
/
kales.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# -*- coding: utf-8 -*-
import json
import requests
import uuid
class Kales(object):
def __init__(self, api_key):
self.api_key = api_key
def _request(self, content, content_type, external_id, **kwargs):
headers = {
"X-AG-Access-Token": self.api_key,
"content-type": content_type,
"externalID": external_id,
"outputformat": "application/json",
"enableMetadataType": "SocialTags",
"calculateRelevanceScore": "true",
"allowDistribution": "false",
"allowSearch": "false"
}
headers.update(kwargs)
return requests.post("https://api.thomsonreuters.com/permid/calais",
data=content, headers=headers)
def analyze(self, content, content_type="text/raw", external_id=None, **kwargs):
if not content or not content.strip():
return None
if not external_id:
external_id = uuid.uuid4().hex
response = self._request(content, content_type, external_id, **kwargs)
response.raise_for_status()
content = json.loads(response.content)
for element in list(content.values()):
for key, value in list(element.items()):
if isinstance(value, str) and value.startswith("http://") and value in content:
element[key] = content[value]
for key, value in list(content.items()):
if "_typeGroup" in value:
group = value["_typeGroup"]
if group not in content:
content[group] = []
del value["_typeGroup"]
content[group].append(value)
return content