forked from semantic-systems/nfdi-search-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
299 lines (235 loc) · 11.2 KB
/
main.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import logging
import logging.config
import os
import uuid
# from objects import Person, Zenodo, Article, Dataset, Presentation, Poster, Software, Video, Image, Lesson, Institute, Funder, Publisher, Gesis, Cordis, Orcid, Gepris
from objects import Article, Organization, Person, Dataset, Project
from flask import Flask, render_template, request, make_response
import threading
from sources import dblp, zenodo, openalex, resodate, oersi, wikidata, cordis, gesis, orcid, gepris, ieee, eudat, openaire, eulg
import details_page
logging.config.fileConfig(os.getenv('LOGGING_FILE_CONFIG', './logging.conf'))
logger = logging.getLogger('nfdi_search_engine')
app = Flask(__name__)
@app.route('/')
def index():
response = make_response(render_template('index.html'))
# Set search-session cookie to the session cookie value of the first visit
if request.cookies.get('search-session') is None:
if request.cookies.get('session') is None:
response.set_cookie('search-session', str(uuid.uuid4()))
else:
response.set_cookie('search-session', request.cookies['session'])
return response
@app.route('/results', methods=['POST', 'GET'])
def search_results():
# The search-session cookie setting can still be None if a user enters the
# /sources endpoint directly without going to / first!!!
logger.debug(
f'Search session {request.cookies.get("search-session")} '
f'searched for "{request.args.get("txtSearchTerm")}"'
)
if request.method == 'GET':
search_term = request.args.get('txtSearchTerm')
results = {
'publications': [],
'researchers': [],
'resources': [],
'organizations': [],
'events': [],
'fundings': [],
'others': [],
'timedout_sources': []
}
threads = []
# add all the sources here in this list; for simplicity we should use the exact module name
# ensure the main method which execute the search is named "search" in the module
sources = [resodate, oersi, openalex, orcid, dblp, zenodo, gesis, ieee, cordis, gepris, eudat, wikidata, openaire, eulg]
for source in sources:
t = threading.Thread(target=source.search, args=(search_term, results,))
t.start()
threads.append(t)
for t in threads:
t.join()
# print(t.is_alive())
logger.info(f'Got {len(results["publications"])} publications')
logger.info(f'Got {len(results["researchers"])} researchers')
logger.info(f'Got {len(results["resources"])} resources')
logger.info(f'Got {len(results["organizations"])} organizations')
logger.info(f'Got {len(results["events"])} events')
logger.info(f'Got {len(results["fundings"])} fundings')
logger.info(f'Got {len(results["others"])} others')
results["timedout_sources"] = list(set(results["timedout_sources"]))
logger.info('Following sources got timed out:' + ','.join(results["timedout_sources"]))
return render_template('results.html', results=results, search_term=search_term)
@app.route('/chatbox')
def chatbox():
response = make_response(render_template('chatbox.html'))
# Set search-session cookie to the session cookie value of the first visit
if request.cookies.get('search-session') is None:
if request.cookies.get('session') is None:
response.set_cookie('search-session', str(uuid.uuid4()))
else:
response.set_cookie('search-session', request.cookies['session'])
return response
@app.route('/publication-details')
def publication_details():
response = make_response(render_template('publication-details.html'))
# Set search-session cookie to the session cookie value of the first visit
if request.cookies.get('search-session') is None:
if request.cookies.get('session') is None:
response.set_cookie('search-session', str(uuid.uuid4()))
else:
response.set_cookie('search-session', request.cookies['session'])
return response
@app.route('/resource-details')
def resource_details():
response = make_response(render_template('resource-details.html'))
# Set search-session cookie to the session cookie value of the first visit
if request.cookies.get('search-session') is None:
if request.cookies.get('session') is None:
response.set_cookie('search-session', str(uuid.uuid4()))
else:
response.set_cookie('search-session', request.cookies['session'])
return response
@app.route('/researcher-details')
def researcher_details():
response = make_response(render_template('researcher-details.html'))
# Set search-session cookie to the session cookie value of the first visit
if request.cookies.get('search-session') is None:
if request.cookies.get('session') is None:
response.set_cookie('search-session', str(uuid.uuid4()))
else:
response.set_cookie('search-session', request.cookies['session'])
return response
@app.route('/organization-details')
def organization_details():
response = make_response(render_template('organization-details.html'))
# Set search-session cookie to the session cookie value of the first visit
if request.cookies.get('search-session') is None:
if request.cookies.get('session') is None:
response.set_cookie('search-session', str(uuid.uuid4()))
else:
response.set_cookie('search-session', request.cookies['session'])
return response
@app.route('/events-details')
def events_details():
response = make_response(render_template('events-details.html'))
# Set search-session cookie to the session cookie value of the first visit
if request.cookies.get('search-session') is None:
if request.cookies.get('session') is None:
response.set_cookie('search-session', str(uuid.uuid4()))
else:
response.set_cookie('search-session', request.cookies['session'])
return response
@app.route('/fundings-details')
def fundings_details():
response = make_response(render_template('fundings-details.html'))
# Set search-session cookie to the session cookie value of the first visit
if request.cookies.get('search-session') is None:
if request.cookies.get('session') is None:
response.set_cookie('search-session', str(uuid.uuid4()))
else:
response.set_cookie('search-session', request.cookies['session'])
return response
@app.route('/details', methods=['POST', 'GET'])
def details():
if request.method == 'GET':
# data_type = request.args.get('type')
details = {}
links = {}
name = ''
search_term = request.args.get('searchTerm')
if search_term.startswith('https://openalex.org/'):
details, links, name = details_page.search_openalex(search_term)
elif search_term.startswith('https://dblp'):
details, links, name = details_page.search_dblp(search_term)
elif search_term.startswith('http://www.wikidata.org'):
details, links, name = details_page.search_wikidata(search_term)
elif search_term.startswith('https://orcid.org/'):
details, links, name = details_page.search_orcid(search_term)
return render_template('details.html', search_term=search_term, details=details, links=links, name=name)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5002, debug=True)
# region OLD CODE
# @app.route('/index-old')
# def index_new():
# response = make_response(render_template('index-old.html'))
# # Set search-session cookie to the session cookie value of the first visit
# if request.cookies.get('search-session') is None:
# if request.cookies.get('session') is None:
# response.set_cookie('search-session', str(uuid.uuid4()))
# else:
# response.set_cookie('search-session', request.cookies['session'])
# return response
# @app.route('/result', methods=['POST', 'GET'])
# def sources():
# # The search-session cookie setting can still be None if a user enters the
# # /sources endpoint directly without going to / first!!!
# logger.debug(
# f'Search session {request.cookies.get("search-session")} '
# f'searched for "{request.args.get("txtSearchTerm")}"'
# )
# if request.method == 'GET':
# search_term = request.args.get('txtSearchTerm')
# results = []
# threads = []
# # add all the sources here in this list; for simplicity we should use the exact module name
# # ensure the main method which execute the search is named "search" in the module
# # sources = [dblp, zenodo, openalex, resodate, wikidata, cordis, gesis]
# sources = [dblp, zenodo, openalex, resodate, wikidata, cordis, gesis, orcid, gepris, ieee] #, eulg]
# for source in sources:
# t = threading.Thread(target=source.search, args=(search_term, results,))
# t.start()
# threads.append(t)
# for t in threads:
# t.join()
# # print(t.is_alive())
# data = {
# 'Researchers': [],
# 'Articles': [],
# 'Dataset': [],
# 'Software': [],
# 'Presentation': [],
# 'Poster': [],
# 'Lesson': [],
# 'Video': [],
# 'Institute': [],
# 'Publisher': [],
# 'Funder': [],
# 'Image': [],
# 'Zenodo': [],
# 'Gesis': [],
# 'Cordis': [],
# 'Orcid': [],
# 'Gepris': []
# }
# logger.info(f'Got {len(results)} results')
# object_mappings = {Person : 'Researchers' ,
# Article : 'Articles' ,
# Dataset : 'Dataset' ,
# Software : 'Software' ,
# Presentation : 'Presentation' ,
# Poster : 'Poster' ,
# Lesson : 'Lesson' ,
# Video : 'Video' ,
# Institute : 'Institute' ,
# Publisher : 'Publisher' ,
# Funder : 'Funder' ,
# Image : 'Image' ,
# Zenodo : 'Zenodo' ,
# Gesis : 'Gesis' ,
# Cordis : 'Cordis' ,
# Orcid : 'Orcid' ,
# Gepris : 'Gepris'
# }
# for result in results:
# result_type = type(result)
# if result_type in object_mappings.keys():
# data[object_mappings[result_type]].append(result)
# else:
# logger.warning(f"Type {result_type} of result not yet handled")
# # Remove items without results
# data = dict((k, result) for k, result in data.items() if result)
# return render_template('result.html', data=data, search_term=search_term)
# endregion