-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
82 lines (61 loc) · 2.01 KB
/
app.py
File metadata and controls
82 lines (61 loc) · 2.01 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
import os
import json
import urllib.request
from flask import Flask, request, redirect, abort
app = Flask(__name__)
server = "https://download.brainimagelibrary.org/inventory/"
@app.route("/", methods=["GET"])
def hello():
return redirect("https://www.brainimagelibrary.org")
@app.route("/assets", methods=["GET"])
def list():
# Get the input string from the URL
filename = "list"
# Define the URL where the JSON file is located
path = f"{server}{filename}.json"
print(path)
# Check if the JSON file exists at the given URL
try:
urllib.request.urlopen(path)
except urllib.error.HTTPError as e:
# The file doesn't exist
return "JSON file not found at {}".format(path)
else:
# The file exists, so download it
json_data = urllib.request.urlopen(path).read()
# load it into a python dict
json_dict = json.loads(json_data)
data = json_dict
return data
@app.route("/asset", methods=["GET"])
def get():
# Get the input string from the URL
filename = request.args.get("filename")
query = request.args.get("query")
if filename is None:
abort(404)
# Define the URL where the JSON file is located
path = f"{server}{filename}.json"
print(path)
# Check if the JSON file exists at the given URL
try:
urllib.request.urlopen(path)
except urllib.error.HTTPError as e:
# The file doesn't exist
return "JSON file not found at {}".format(path)
else:
# The file exists, so download it
json_data = urllib.request.urlopen(path).read()
# load it into a python dict
json_dict = json.loads(json_data)
# Extract the json block
if query:
data = json_dict[query]
else:
data = json_dict
# Save the downloaded JSON file in the app's directory
with open(filename + ".json", "wb") as f:
f.write(json_data)
return data
if __name__ == "__main__":
app.run(debug=True)