/
app.py
189 lines (159 loc) · 5.69 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
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
import settings
from flask import Flask, request, jsonify, abort, url_for, Response, render_template
from flask_cors import CORS
from cache import Cache
from fetcher import Fetcher
from datetime import datetime, timedelta
from regions import regions_list
from regions.quality import AirQuality
from statistics import mean
import json
import threading
import re
import logging
from flask_apscheduler import APScheduler
from datetime import datetime
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
app = Flask(__name__)
app.secret_key = settings.flask['SECRET_KEY']
# Setup cors header
CORS(app)
cache = Cache()
fetcher = Fetcher(cache)
def absolute_url_for(url_name: str, date: datetime, **values) -> str:
"""
Calculate the absolute url for url_name joined with values
:param url_name: The url name
:param **values: The values required by url_for
:return: The absolute url
"""
with app.app_context():
day = date.strftime('%d')
month = date.strftime('%m')
year = date.strftime('%Y')
return request.url_root[:-1] + url_for(url_name, day=day, month=month, year=year, **values)
def filter_regions(regions: list, wanted_regions: list) -> list:
"""
Filters out the unwanted regions
:param regions: All the regions
:param wanted_regions: A list of wanted regions
:return: The air quality of the wanted regions
"""
wanted_regions_names = [x.name.lower() for x in wanted_regions]
filtered = [x for x in regions if x['name'].lower() in wanted_regions_names]
return filtered
def append_absolute_url(regions: list, date:datetime) -> list:
"""
Append the absolute url to each region
:param regions: The list of region
:param date: The date of interest
:return: A new list with the newly added informations
"""
for region in regions:
region['href'] = absolute_url_for('regional_data',
date,
region_name=region['name'])
for province in region['provinces']:
province['href'] = absolute_url_for('provincial_data',
date,
region_name=region['name'],
province_name=province['short'])
return regions
# Scheduled jobs
scheduler = APScheduler()
scheduler.init_app(app)
scheduler.start()
scheduler_last_run = None
@scheduler.task('interval', id='refresh_data', seconds=settings.data['REFRESH_INTERVAL'])
def refresh_regions_data():
"""
Refresh data about today, yesterday and the day before yesterday
"""
scheduler_last_run = datetime.now()
days = [
datetime.now(),
datetime.now() - timedelta(days=1),
datetime.now() - timedelta(days=2)
]
logging.info('Refreshing data')
for day in days:
fetcher.fetch_day_high_priority(day)
@app.route('/', methods=['GET'])
def index():
"""
Index HTML page with informations about the status
"""
context = {
'available_dates': [datetime.strptime(x, '%Y%m%d') for x in sorted(cache.keys)],
'available_regions': sorted([x.name for x in regions_list]),
'last_automatic_update': scheduler_last_run,
'pending_updates': fetcher.pending
}
return render_template('index.html', **context)
@app.route('/api/v1/dates', methods=['GET'])
def dates():
"""
Endpoint available dates
"""
return jsonify([datetime.strptime(x, '%Y%m%d').strftime('%Y-%m-%d') for x in cache.keys])
@app.route('/api/v1/<int:year>/<int:month>/<int:day>', methods=['GET'])
def national_data(year, month, day):
"""
Endpoint for national data
"""
date = datetime(year=year, month=month, day=day)
# get quality of all region
try:
regions = append_absolute_url(fetcher.fetch_day(date), date)
except cache.NotInCacheException:
return Response(status=202)
except ValueError:
abort(400)
return jsonify(regions)
@app.route('/api/v1/<int:year>/<int:month>/<int:day>/<string:region_name>')
def regional_data(year, month, day, region_name):
"""
Endpoint for regional data
"""
date = datetime(year=year, month=month, day=day)
region_name = region_name.lower()
chosen_region = next((x for x in regions_list if x.name.lower() == region_name), None)
if chosen_region is None:
abort(404)
try:
regions = append_absolute_url(fetcher.fetch_day(date), date)
region = filter_regions(regions, [chosen_region])[0]
except cache.NotInCacheException:
return Response(status=202)
except ValueError:
abort(400)
return jsonify(region)
@app.route('/api/v1/<int:year>/<int:month>/<int:day>/<string:region_name>/<string:province_name>')
def provincial_data(year, month, day, region_name, province_name):
"""
Endpoint for province data
"""
date = datetime(year=year, month=month, day=day)
region_name = region_name.lower()
chosen_region = next((x for x in regions_list if x.name.lower() == region_name), None)
if chosen_region is None:
abort(404)
# Get the quality of the region
try:
regions = append_absolute_url(fetcher.fetch_day(date), date)
region = filter_regions(regions, [chosen_region])[0]
except cache.NotInCacheException:
return Response(status=202)
except ValueError:
abort(400) # date is in the future
province = next(
(p for p in region['provinces']
if p['name'].lower() == province_name.lower() or
p['short'].lower() == province_name.lower()),
None)
if province is None: abort(404)
return jsonify(province)
if __name__ == '__main__':
app.run(debug=settings.DEBUG)