-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
121 lines (89 loc) · 4.16 KB
/
Copy pathapp.py
File metadata and controls
121 lines (89 loc) · 4.16 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
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
from flask import Flask, jsonify, json, request
from datetime import datetime as dt
import pymongo
import os
conn = os.environ.get("MONGODB_URI")
client = pymongo.MongoClient(conn)
db = client.videogames
collection = db.surfer_climate
surfer_climate_data = collection.find({}, {"_id": 0})
selected_data = []
#################################################
# Flask Setup
#################################################
app = Flask(__name__)
#################################################
# Flask Routes
#################################################
@app.route('/')
def home():
url = request.base_url
print('Server received request for "Home" page...')
return (
f'<h1> Surfs Up! </h1><br>'
f'Here are the API routes about the Climate and Station Analysis in Honolulu, Hawaii<br>'
f'Last year precipitation API: <a href="{url}api/v1.0/precipitation" target="_blank">{url}api/v1.0/precipitation</a><br>'
f'Stations API: <a href="{url}api/v1.0/stations" target="_blank">{url}api/v1.0/stations</a><br>'
f'Temperature Observations (TOBS) for the previous year API: <a href="{url}api/v1.0/tobs" target="_blank">{url}api/v1.0/tobs</a><br><hr></hr>'
f'<h2> Dynamic API </h2>'
f'<h3>From selected date to the last date:</h3>'
f'Minimum, Maximum and Average of Temperature API : {url}api/v1.0/yyyy-mm-dd<br>'
f'Example : <a href="{url}api/v1.0/2017-01-01" target="_blank">{url}api/v1.0/2017-01-01</a>'
f'<h3>From selected date to the selected date:</h3>'
f'Minimum, Maximum and Average of Temperature API : {url}/api/v1.0/start date/end date<br>'
f'Example : <a href="{url}api/v1.0/2016-10-01/2017-05-01" target="_blank">{url}api/v1.0/2016-10-01/2017-05-01</a><br>'
'<br>Note: format(yyyy-mm-dd)<br>'
'API only contains data from 2016-09-01 to 2017-08-23'
)
@app.route('/api/v1.0/precipitation')
def precip():
return jsonify(surfer_climate_data[1])
@app.route('/api/v1.0/stations')
def stat():
return jsonify(surfer_climate_data[2])
@app.route('/api/v1.0/tobs')
def tem_obs():
return jsonify(surfer_climate_data[0])
@app.route('/api/v1.0/<start>')
def start_date(start):
user_date = dt.strptime(start, '%Y-%m-%d')
if dt.strptime(start, '%Y-%m-%d') < dt.strptime('2016-09-01', '%Y-%m-%d') or dt.strptime(start, '%Y-%m-%d') > dt.strptime('2017-08-23', '%Y-%m-%d'):
return jsonify({'error': f'Date "{start}" not found. '}), 404
else:
precipitation = surfer_climate_data[1]['data']
for row in precipitation:
search_date = row['date']
row_date = dt.strptime(search_date, '%Y-%m-%d')
if row_date >= user_date:
num = row['prcp']
if num is None:
continue
else:
selected_data.append(num)
statics = [{'Max Temperature': max(selected_data), 'Min Temperature': min(selected_data),
'Avg Temperature': round(sum(selected_data)/len(selected_data), 2)}]
return jsonify(max(statics))
@app.route('/api/v1.0/<start>/<end>')
def start_end_date(start, end):
user_date_start = dt.strptime(start, '%Y-%m-%d')
user_date_end = dt.strptime(end, '%Y-%m-%d')
if dt.strptime(start, '%Y-%m-%d') < dt.strptime('2016-09-01', '%Y-%m-%d') or dt.strptime(start, '%Y-%m-%d') > dt.strptime('2017-08-23', '%Y-%m-%d'):
return jsonify({'error': f'Date "{start}" not found. '}), 404
else:
precipitation = surfer_climate_data[1]['data']
for row in precipitation:
search_date = row['date']
row_date = dt.strptime(search_date, '%Y-%m-%d')
if row_date >= user_date_start and row_date <= user_date_end:
num = row['prcp']
if num is None:
continue
else:
selected_data.append(num)
else:
continue
statics = [{'Max Temperature': max(selected_data), 'Min Temperature': min(selected_data),
'Avg Temperature': round(sum(selected_data)/len(selected_data), 2)}]
return jsonify(max(statics))
if __name__ == "__main__":
app.run(debug=True)