-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebOfScienceClient.py
More file actions
138 lines (112 loc) · 4.36 KB
/
WebOfScienceClient.py
File metadata and controls
138 lines (112 loc) · 4.36 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
__author__ = 'szednik'
import xml.etree.ElementTree as ET
import requests
ns = {
"soapenv": "http://schemas.xmlsoap.org/soap/envelope/",
"woksearchlite": "http://woksearchlite.v3.wokmws.thomsonreuters.com",
"auth": "http://auth.cxf.wokmws.thomsonreuters.com"
}
class WebOfScienceClient(object):
def __init__(self):
self.session_id = None
def is_authenticated(self):
if self.session_id is not None:
return True
else:
return False
def authenticate(self):
tree = ET.parse("resources/authenticate.xml")
payload = ET.tostring(tree.getroot())
r = requests.post("http://search.webofknowledge.com/esti/wokmws/ws/WOKMWSAuthenticate", data=payload)
response = ET.fromstring(r.content)
_return = response.find(".//return")
if _return is not None:
self.session_id = _return.text
return True
else:
return False
def get_keywords_by_doi(self, doi):
pub = self.user_query_by_doi(doi)
if pub is not None and "keywords" in pub:
return pub["keywords"]
else:
return []
def user_query_by_doi(self, doi):
query = "DO="+doi
return self.user_query(query)
def user_query(self, query):
if not self.is_authenticated():
return
tree = ET.parse("resources/userQuery.xml")
user_query_node = tree.find(".//userQuery")
user_query_node.text = query
headers = self._get_session_header()
payload = ET.tostring(tree.getroot())
r = requests.post("http://search.webofknowledge.com/esti/wokmws/ws/WokSearchLite", headers=headers,
data=payload)
return self._process_response(r.content)
def close_session(self):
if not self.is_authenticated():
return
tree = ET.parse("resources/closeSession.xml")
payload = ET.tostring(tree.getroot())
headers = self._get_session_header()
r = requests.post("http://search.webofknowledge.com/esti/wokmws/ws/WOKMWSAuthenticate", headers=headers,
data=payload)
response = ET.fromstring(r.content)
fault = response.find(".//faultstring")
if fault is not None:
return fault.text
else:
self.session_id = None
return None
def _get_session_header(self):
if self.is_authenticated():
return {"Cookie": "SID=\"" + str(self.session_id) + "\""}
else:
return None
def __enter__(self):
self.authenticate()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close_session()
@staticmethod
def _process_response(content):
response = ET.fromstring(content)
record = response.find(".//return/records")
if record is None:
return None
r = {}
r.update({"uid": record.find("uid").text})
r.update({"title": record.find("title/value").text})
authors = []
for author in record.findall("authors/value"):
authors.append(author.text)
if authors:
r.update({"authors": authors})
for source in record.findall("source"):
r.update(WebOfScienceClient._process_node(source, "Published.BiblioYear"))
r.update(WebOfScienceClient._process_node(source, "Published.BiblioDate"))
r.update(WebOfScienceClient._process_node(source, "SourceTitle"))
for other in record.findall("other"):
r.update(WebOfScienceClient._process_node(other, "Identifier.Doi"))
r.update(WebOfScienceClient._process_node(other, "Identifier.Issn"))
keywords = []
for keyword in record.findall("keywords/value"):
keywords.append(keyword.text)
if keywords:
r.update({"keywords": keywords})
abstract = record.find(".//abstract")
if abstract is not None:
print("Found abstract!")
print(ET.tostring(abstract))
for label in record.findall(".//label"):
if str(label.text).lower() == "abstract":
print("Found abstract!")
return r
@staticmethod
def _process_node(node, label):
if node.find("label").text == label:
return {label: node.find("value").text}
else:
return {}