-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolver.py
More file actions
179 lines (147 loc) · 5.8 KB
/
resolver.py
File metadata and controls
179 lines (147 loc) · 5.8 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from es_connector import DLIESConnector
import urllib2
import json
import parser
import sys
papers_list=['monograph','publication','journal-article','proceedings-article','book-chapter']
def connecttorestjson(url):
exception = True
count = 0
rest_response = None
while exception and count < 3:
try:
response = urllib2.urlopen(url)
rest_response = json.loads(response.read())
exception = False
except:
count += 1
return exception,rest_response
def connecttorestxml(url):
exception = True
count = 0
rest_response = None
while exception and count < 3:
try:
response = urllib2.urlopen(url)
rest_response = response.read()
exception = False
except:
count += 1
return exception,rest_response
class Resolvers:
def __init__(self):
self.dli_connector = DLIESConnector('192.168.100.70', 'dli')
self.openaire_index = "http://solr.openaire.eu:8983 /solr/DMF-index-openaire_shard1_replica1/select?q=%s&wt=json"
self.crossref_baseurl = "http://api.crossref.org/works/"
self.datacite_baseurl = "https://api.datacite.org/works/"
self.openaire_publication_baseurl="http://api.openaire.eu/search/publications?doi="
self.openaire_dataset_baseurl="http://api.openaire.eu/search/datasets?doi="
self.arxiv_baseurl="http://export.arxiv.org/api/query?id_list="
self.pubmed_baseurl="https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&retmode=xml&id="
def resolverdli(self,doi):
res = self.dli_connector.pid_type(doi) # verifico se esiste risolto in dli
title = []
abstract = []
typology = ''
#try:
if len(res) > 0:
typology = res[0]['typology']
if typology == 'publication' or typology == 'dataset':
for dic in res:
try:
if title == []:
title = dic['title']
if dic['abstract'] != '':
abstract = [dic['abstract']]
break
except:
pass
return title, abstract, typology
def resolvecrossref(self,doi):
exception, response = connecttorestjson(self.crossref_baseurl + doi)
abs = []
if exception:
return [],[],''
response_type = response['message']['type']
if response_type in papers_list:
try:
abst = response['message']['abstract']
abst = abst.replace("<jats:p>",'').replace("</jats:p>",'')
abs = [abst]
except:
pass
return response['message']['title'],abs,response_type
def resolvedatacite(self,doi):
exception, response = connecttorestjson(self.datacite_baseurl + doi)
if exception:
return [],[],''
if response is None :
return [],[],''
if 'error' in response.keys():
return [],[],''
title = response['data']['attributes']['title']
title = [] if title is None else [title]
description = response['data']['attributes']['description']
description = [] if description is None else [description]
typology = response['data']['attributes']['resource-type-id']
typology = '' if typology is None else typology
return title , description, typology
def resolveopenaireindex(self,doi):
exception, response = connecttorestjson(self.openaire_index%("pid:" + doi))
if exception:
return [], [], ''
else:
if response['response']['numFound'] == 0:
return [], [], ''
else:
docs = response['response']['docs'][0]['__result']
print "sto andando in parser \n"
return parser.parseOpenaire(docs[0])
def resolveopenaire(self,doi, typology):
exception, response = connecttorestjson(self.openaire_dataset_baseurl + doi + '&format=json') if typology.strip().lower() == 'dataset' else connecttorestjson(self.openaire_publication_baseurl + doi+ '&format=json')
title = []
description = []
if exception:
# print "exception"
return [],[],''
if response['response']['results'] is None:
return [], [], ''
res = response['response']['results']['result']
if not type(res) is list:
res = [res]
for elem in res:
t = elem['metadata']['oaf:entity']['oaf:result']['title']
if not type(t) is list:
t=[t]
if title == []:
for e in t:
try:
title.append(e['$'])
except:
pass
dex = elem['metadata']['oaf:entity']['oaf:result']['description']
if dex != None:
if not type(dex) is list:
dex = [dex]
for d in dex:
try:
description.append(d['$'])
except:
pass
if description != []:
break
# print "ritorna..." , title, description, typology
return title, description, typology
def resolvearxiv(self, id):
exception, response = connecttorestxml(self.arxiv_baseurl + id)
if exception:
return [],[],''
return parser.parseArxiv(response)
def resolvepubmed(self, id):
exception, response = connecttorestxml(self.pubmed_baseurl + id)
if exception:
return [],[],''
return parser.parsepubmed(response)
#
# r = Resolvers()
# print r.resolveopenaireindex("10.5281/zenodo.11082")