forked from ourresearch/openalex-guts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
420 lines (347 loc) · 13.4 KB
/
views.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
from flask import make_response
from flask import request
from flask import redirect
from flask import abort
from flask import render_template
from flask import jsonify
from flask import send_from_directory
from flask import send_file
from flask import safe_join
from flask import url_for
from sqlalchemy.sql import func
from sqlalchemy.orm import load_only
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy import or_
import re
import json
import os
import sys
import yaml
from app import app
from app import db
from app import logger
from app import MAX_MAG_ID
import models
from util import jsonify_fast_no_sort
from util import TimingMessages
from util import is_openalex_id
from util import normalize_openalex_id
def json_dumper(obj):
"""
if the obj has a to_dict() function we've implemented, uses it to get dict.
from http://stackoverflow.com/a/28174796
"""
try:
return obj.to_dict()
except AttributeError:
return obj.__dict__
def json_resp(thing):
json_str = json.dumps(thing, sort_keys=True, default=json_dumper, indent=4)
if request.path.endswith(".json") and (os.getenv("FLASK_DEBUG", False) == "True"):
logger.info("rendering output through debug_api.html template")
resp = make_response(render_template(
'debug_api.html',
data=json_str))
resp.mimetype = "text/html"
else:
resp = make_response(json_str, 200)
resp.mimetype = "application/json"
return resp
def abort_json(status_code, msg):
body_dict = {
"HTTP_status_code": status_code,
"message": msg,
"error": True
}
resp_string = json.dumps(body_dict, sort_keys=True, indent=4)
resp = make_response(resp_string, status_code)
resp.mimetype = "application/json"
abort(resp)
@app.after_request
def after_request_stuff(resp):
#support CORS
resp.headers["Access-Control-Allow-Origin"] = "*"
resp.headers["Access-Control-Allow-Methods"] = "POST, GET, OPTIONS, PUT, DELETE, PATCH"
resp.headers["Access-Control-Allow-Headers"] = "Origin, X-Requested-With, Content-Type, Accept, Authorization, Cache-Control"
resp.headers["Access-Control-Expose-Headers"] = "Authorization, Cache-Control"
resp.headers["Access-Control-Allow-Credentials"] = "true"
# make not cacheable because the GETs change after parameter change posts!
resp.cache_control.max_age = 0
resp.cache_control.no_cache = True
# without this jason's heroku local buffers forever
sys.stdout.flush()
return resp
@app.after_request
def after_request_override_urls_for_debugging(response):
wants_apiurls = ("apiurls" in request.args)
if wants_apiurls:
json_response_data = response.get_data().decode('utf-8')
json_response_data = re.sub("https://openalex.org/(?P<id>[A-Za-z\d]{3,})", "https://api.openalex.org/\g<id>?apiurls", json_response_data)
response.set_data(json_response_data.encode())
return response
def is_work_openalex_id(id):
if isinstance(id, int):
return False
clean_id = normalize_openalex_id(id)
return clean_id.startswith("W")
def is_author_openalex_id(id):
if isinstance(id, int):
return False
clean_id = normalize_openalex_id(id)
return clean_id.startswith("A")
def is_venue_openalex_id(id):
if isinstance(id, int):
return False
clean_id = normalize_openalex_id(id)
return clean_id.startswith("V")
def is_institution_openalex_id(id):
if isinstance(id, int):
return False
clean_id = normalize_openalex_id(id)
return clean_id.startswith("I")
def is_concept_openalex_id(id):
if isinstance(id, int):
return False
clean_id = normalize_openalex_id(id)
return clean_id.startswith("C")
@app.route('/swagger.yml')
def yaml_get():
data = json.loads(json.dumps(app.__schema__))
with open('yamldoc.yml', 'w') as yamlf:
yaml.dump(data, yamlf, allow_unicode=True, default_flow_style=False)
file = os.path.abspath(os.getcwd())
# return send_file(safe_join(file, 'yamldoc.yml'), as_attachment=True, attachment_filename='yamldoc.yml', mimetype='application/x-yaml')
return send_file(safe_join(file, 'yamldoc.yml'), mimetype='text/plain')
#### Record
@app.route('/records/RANDOM')
@app.route('/records/random')
def records_random_get():
from models import Record
obj = db.session.query(Record).order_by(func.random()).first()
if not obj:
abort(404)
return jsonify_fast_no_sort({"n": len(obj.siblings), "siblings": obj.siblings})
@app.route('/records/<id>')
def records_id_get(id):
from models import Record
obj = Record.query.get(id)
return jsonify_fast_no_sort({"n": len(obj.siblings), "siblings": obj.siblings})
#### Work
@app.route("/works/RANDOM")
@app.route("/works/random")
def works_random_get():
query = db.session.query(models.Work.paper_id).order_by(func.random())
if ("new" in request.args):
query = query.filter(models.Work.paper_id >= MAX_MAG_ID)
work_id = query.first()
work_id = work_id[0]
obj = models.work_from_id(work_id)
if not obj:
abort(404)
response = obj.to_dict()
return jsonify_fast_no_sort(response)
@app.route("/works/<path:id>")
def works_id_get(id):
from util import normalize_doi
from util import normalize_pmid
from models import openalex_id_from_pmid
obj = None
if is_openalex_id(id):
clean_id = normalize_openalex_id(id)
if clean_id != id:
return redirect(url_for("works_id_get", id=clean_id, **request.args))
clean_id = int(clean_id[1:])
obj = models.work_from_id(clean_id)
elif id.startswith("mag:"):
clean_id = id.replace("mag:", "")
clean_id = f"W{clean_id}"
return redirect(url_for("works_id_get", id=clean_id, **request.args))
elif id.startswith("pmid:"):
id = id.replace("pmid:", "")
clean_pmid = normalize_pmid(id)
openalex_id = openalex_id_from_pmid(clean_pmid)
return redirect(url_for("works_id_get", id=openalex_id, **request.args))
elif id.startswith("doi:") or ("doi" in id):
clean_doi = normalize_doi(id, return_none_if_error=True)
if not clean_doi:
abort(404)
openalex_id = models.openalex_id_from_doi(clean_doi)
if openalex_id:
return redirect(url_for("works_id_get", id=openalex_id, **request.args))
if not obj:
abort(404)
response = obj.to_dict()
return jsonify_fast_no_sort(response)
#### Author
@app.route("/authors/RANDOM")
@app.route("/authors/random")
def authors_random_get():
query = models.Author.query.order_by(func.random())
if ("new" in request.args):
query = query.filter(models.Author.author_id >= MAX_MAG_ID)
obj = query.first()
return jsonify_fast_no_sort(obj.to_dict())
@app.route("/authors/<path:id>")
def authors_id_get(id):
from util import normalize_orcid
obj = None
if is_openalex_id(id):
clean_id = normalize_openalex_id(id)
if clean_id != id:
return redirect(url_for("authors_id_get", id=clean_id, **request.args))
author_id = int(clean_id[1:])
obj = models.author_from_id(author_id)
elif id.startswith("mag:"):
clean_id = id.replace("mag:", "")
clean_id = f"A{clean_id}"
return redirect(url_for("authors_id_get", id=clean_id, **request.args))
elif id.startswith("orcid:") or id.startswith("https://orcid.org"):
clean_orcid = normalize_orcid(id)
openalex_id = models.openalex_id_from_orcid(clean_orcid)
if openalex_id:
return redirect(url_for("authors_id_get", id=openalex_id, **request.args))
if not obj:
abort(404)
response = obj.to_dict()
return jsonify_fast_no_sort(response)
# #### Institution
@app.route("/institutions/RANDOM")
@app.route("/institutions/random")
def institutions_random_get():
query = models.Institution.query.order_by(func.random())
if ("new" in request.args):
query = query.filter(models.Institution.affiliation_id >= MAX_MAG_ID)
obj = query.first()
return jsonify_fast_no_sort(obj.to_dict())
@app.route("/institutions/<path:id>")
def institutions_id_get(id):
from util import normalize_ror
obj = None
if is_openalex_id(id):
clean_id = normalize_openalex_id(id)
if clean_id != id:
return redirect(url_for("institutions_id_get", id=clean_id, **request.args))
clean_id = int(clean_id[1:])
obj = models.institution_from_id(clean_id)
elif id.startswith("mag:"):
clean_id = id.replace("mag:", "")
clean_id = f"I{clean_id}"
return redirect(url_for("institutions_id_get", id=clean_id, **request.args))
elif id.startswith("ror:") or ("ror.org" in id):
clean_ror = normalize_ror(id)
openalex_id = models.openalex_id_from_ror(clean_ror)
if openalex_id:
return redirect(url_for("institutions_id_get", id=openalex_id, **request.args))
if not obj:
abort(404)
response = obj.to_dict()
return jsonify_fast_no_sort(response)
#### Venue
@app.route("/venues/RANDOM")
@app.route("/venues/random")
def venues_random_get():
query = models.Venue.query.order_by(func.random())
if ("new" in request.args):
query = query.filter(models.Venue.journal_id >= MAX_MAG_ID)
obj = query.first()
if not obj:
raise NoResultFound
response = obj.to_dict()
return jsonify_fast_no_sort(response)
@app.route("/venues/<path:id>")
def venues_id_get(id):
from util import normalize_issn
obj = None
if is_openalex_id(id):
clean_id = normalize_openalex_id(id)
if clean_id != id:
return redirect(url_for("venues_id_get", id=clean_id, **request.args))
clean_id = int(clean_id[1:])
obj = models.journal_from_id(clean_id)
elif id.startswith("mag:"):
clean_id = id.replace("mag:", "")
clean_id = f"V{clean_id}"
return redirect(url_for("venues_id_get", id=clean_id, **request.args))
elif id.startswith("issn:"):
clean_issn = normalize_issn(id)
openalex_id = models.openalex_id_from_issn(clean_issn)
if openalex_id:
return redirect(url_for("venues_id_get", id=openalex_id, **request.args))
if not obj:
abort(404)
response = obj.to_dict()
return jsonify_fast_no_sort(response)
#### Concept
@app.route("/concepts/RANDOM")
@app.route("/concepts/random")
def concepts_random_get():
query = models.Concept.query.order_by(func.random())
if ("new" in request.args):
query = query.filter(models.Concept.field_of_study_id >= MAX_MAG_ID)
obj = query.first()
return jsonify_fast_no_sort(obj.to_dict())
@app.route("/concepts/<path:id>")
def concepts_id_get(id):
from util import normalize_wikidata
obj = None
if is_openalex_id(id):
clean_id = normalize_openalex_id(id)
if clean_id != id:
return redirect(url_for("concepts_id_get", id=clean_id, **request.args))
clean_id = int(clean_id[1:])
obj = models.concept_from_id(clean_id)
elif id.startswith("mag:"):
clean_id = id.replace("mag:", "")
clean_id = f"C{clean_id}"
return redirect(url_for("concepts_id_get", id=clean_id, **request.args))
elif id.startswith("wikidata:") or ("wikidata" in id):
clean_wikidata = normalize_wikidata(id)
openalex_id = models.openalex_id_from_wikidata(clean_wikidata)
if openalex_id:
return redirect(url_for("concepts_id_get", id=openalex_id, **request.args))
if not obj:
abort(404)
response = obj.to_dict()
return jsonify_fast_no_sort(response)
@app.route("/concepts/name/<string:name>")
def concepts_name_get(name):
obj = models.concept_from_name(name)
if not obj:
abort(404)
return jsonify_fast_no_sort(obj.to_dict())
######
@app.route('/<path:openalex_id>')
def universal_get(openalex_id):
if not openalex_id:
return {'message': "Don't panic"}, 404
if not is_openalex_id(openalex_id):
return {'message': "OpenAlex ID format not recognized"}, 404
openalex_id = normalize_openalex_id(openalex_id)
if is_work_openalex_id(openalex_id):
return redirect(url_for("works_id_get", id=openalex_id, **request.args))
elif is_author_openalex_id(openalex_id):
return redirect(url_for("authors_id_get", id=openalex_id, **request.args))
elif is_venue_openalex_id(openalex_id):
return redirect(url_for("venues_id_get", id=openalex_id, **request.args))
elif is_institution_openalex_id(openalex_id):
return redirect(url_for("institutions_id_get", id=openalex_id, **request.args))
elif is_concept_openalex_id(openalex_id):
return redirect(url_for("concepts_id_get", id=openalex_id, **request.args))
return {'message': "OpenAlex ID format not recognized"}, 404
@app.route('/loaderio-2dc2634ae02b4016d10e4085686d893d/')
def looderio_verification():
response = make_response("loaderio-2dc2634ae02b4016d10e4085686d893d", 200)
response.mimetype = "text/plain"
return response
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5007))
app.run(host='0.0.0.0', port=port, debug=True, threaded=True)
if False:
print("""
PATH=$(pyenv root)/shims:$PATH; unset PYTHONPATH
echo 'PATH=$(pyenv root)/shims:$PATH' >> ~/.zshrc
/Users/hpiwowar/.pyenv/versions/3.9.5/bin/python3 --version
PYTHONPATH=/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages:
python --version
""")
# unset PYTHONPATH