-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
80 lines (66 loc) · 1.78 KB
/
app.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
# Std lib import
import os
from pathlib import Path
# Library Imports
import redis
import requests
from flask import Flask, abort
from flask_cors import CORS
from dotenv import load_dotenv
from werkzeug.exceptions import NotFound
# App Modules
from fetch import Fetch
from processor import Processor
## Load ENV
load_dotenv()
isDev = os.getenv('FLASK_ENV') == 'development'
accepted = '*' if isDev else 'https://wilsonj806.github.io/*'
app = Flask(__name__)
app.config['CORS_HEADERS'] = 'Content-Type'
CORS(app, origins = accepted)
fetcher = Fetch()
@app.route('/')
def initial_ping():
return """NYC Tree Data Fetcher; Version: 0.0.0"""
@app.route('/data')
def fetch_all():
json = fetcher.check_cache()
return {
'_data_length': len(json),
'data': json
}
@app.route('/data/count')
def count_per_boro():
try:
json = fetcher.check_cache()
data = Processor.count_per_boro(data = json)
return { 'data': data }
except:
abort(500,'Something went wrong on our end :(')
@app.route('/data/species')
def count_per_species():
try:
json = fetcher.check_cache()
data = Processor.count_species(data = json)
return { 'data': data }
except:
abort(500,'Something went wrong on our end :(')
@app.route('/data/nta')
def count_per_nta():
try:
json = fetcher.check_cache()
data = Processor.count_per_nta(data = json)
return { 'data': data }
except:
abort(500,'Something went wrong on our end :(')
@app.route('/data/<boro>/species')
def quant_boro_species(boro):
try:
json = fetcher.check_cache()
data = Processor.count_by_boro(data = json, boro = boro)
return { 'data': data }
except:
abort(400, 'Bad request, check your query string parameters')
@app.errorhandler(NotFound)
def handle_not_found(e):
return 'Resource not found.', 404