-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopencalais.py
58 lines (50 loc) · 1.71 KB
/
opencalais.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
48
49
50
51
52
53
54
55
56
57
58
import sys
import requests
import os
import json
from xml.dom.minidom import parseString
calais_url = 'https://api.thomsonreuters.com/permid/calais'
def main():
try:
if len(sys.argv) < 3:
print '2 params are required: 1.input file full path, 2.access token'
sys.exit(-1)
else:
input_file = sys.argv[1]
access_token = sys.argv[2]
if not os.path.exists(input_file):
print 'The file [%s] does not exist' % input_file
return
headers = {'X-AG-Access-Token' : access_token, 'Content-Type' : 'text/raw', 'outputformat' : 'application/json'}
sendFiles(input_file, headers)
except Exception ,e:
print 'Error in connect ' , e
def sendFiles(files, headers):
is_file = os.path.isfile(files)
if is_file == True:
sendFile(files, headers)
else:
for file_name in os.listdir(files):
if os.path.isfile(file_name):
sendFile(file_name, headers)
else:
sendFiles(file_name, headers)
def sendFile(file_name, headers):
files = {'file': open(file_name, 'rb')}
response = requests.post(calais_url, files=files, headers=headers, timeout=80)
content = response.text
#print 'Results received: %s' % content
if response.status_code == 200:
oc_response_json = json.loads(content);
#print findall(oc_response_json, 'socialTag');
findall(oc_response_json, 'socialTag');
def findall(v, k):
if type(v) == type({}):
for k1 in v:
if k1 == k:
#result.append(v['name'].encode("utf-8"));
print v['name'] + ',';
#print v[k1]
findall(v[k1], k)
if __name__ == "__main__":
main()