This repository has been archived by the owner on Oct 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.py
56 lines (47 loc) · 1.63 KB
/
client.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
"""
Basic Python client for AMR.
http://ipscience-help.thomsonreuters.com/LAMRService/WebServicesOverviewGroup/overview.html
"""
from itertools import zip_longest
import os
import xml.etree.ElementTree as ET
import requests
try:
USER = os.environ['WOS_USER']
PASSWORD = os.environ['WOS_PASSWORD']
except KeyError:
raise Exception("Unable to read WOS_USER and WOS_PASSWORD environment variables.")
AMR_URL = "https://ws.isiknowledge.com/cps/xrpc"
BATCH_SIZE = 50
THROTTLE_CAP = 2500 # Max number of records you may request per minute
def grouper(iterable, n, fillvalue=None):
"""
Group iterable into n sized chunks.
See: http://stackoverflow.com/a/312644/758157
"""
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)
def read(raw_in):
ns = {'isi': 'http://www.isinet.com/xrpc41'}
raw = ET.fromstring(raw_in)
out = {}
recs = raw.findall('isi:fn/isi:map/isi:map', ns)
if len(recs) < 1:
print('\n\nERROR: The AMR API did not return any records. Response text:')
print(raw_in)
else:
for cite in recs:
cite_key = cite.attrib['name']
meta = {}
for val in cite.findall('isi:map/isi:val', ns):
meta[val.attrib['name']] = val.text
if val.attrib['name'] == 'message':
print('\n\nMessage from AMR: {}'.format(val.text))
out[cite_key] = meta
return out
def get(request_xml):
headers = {'Content-Type': 'application/xml'}
response = requests.post(AMR_URL, data=request_xml, headers=headers)
# parse into xml
xml = read(response.text)
return xml