forked from ourresearch/parseland
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
82 lines (66 loc) · 2.06 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
from flask import jsonify, request
from app import app
from exceptions import APIError, BadLandingPageError
from publisher.controller import PublisherController
from publisher.utils import prep_message, check_bad_landing_page
from repository.controller import RepositoryController
@app.route("/")
def home():
return jsonify(
{
"version": "0.1",
"app_name": "parseland",
"msg": "Don't panic",
}
)
@app.route("/parse-publisher")
def parse_publisher():
doi = request.args.get("doi")
pc = PublisherController(doi)
if check_bad_landing_page(pc.soup):
raise BadLandingPageError()
parser = pc.find_parser()
parsed_message = parser.parse()
message = prep_message(parsed_message, parser)
if doi.startswith('http'):
doi = doi.split('doi.org/')[1]
response = {
"message": message,
"metadata": {
"parser": parser.parser_name,
"doi": doi,
"doi_url": f"https://doi.org/{doi}",
},
}
return jsonify(response)
@app.route("/parse-repository")
def parse_repository():
page_id = request.args.get("page-id")
rc = RepositoryController(page_id)
parser = rc.find_parser()
if parser.authors_found():
message = parser.parse()
else:
message = parser.no_authors_output()
response = {
"message": message,
"metadata": {
"parser": parser.parser_name,
"page_id": page_id,
},
}
return jsonify(response)
@app.errorhandler(APIError)
def handle_exception(err):
"""Return custom JSON when APIError or its children are raised"""
response = {"error": err.description, "message": ""}
if len(err.args) > 0:
response["message"] = err.args[0]
# Add some logging so that we can monitor different types of errors
app.logger.error("{}: {}".format(err.description, response["message"]))
return jsonify(response), err.code
@app.route('/debug-sentry')
def trigger_error():
division_by_zero = 1 / 0
if __name__ == "__main__":
app.run()