Skip to content

Commit

Permalink
Scale is now Updating. Also made some small changes. Added .txt file …
Browse files Browse the repository at this point in the history
…for the values
  • Loading branch information
LuisSchuimer committed Nov 21, 2022
1 parent 99dedfb commit ee9368f
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 20 deletions.
26 changes: 19 additions & 7 deletions server/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import requests


def get_data(location):
def get_data(location, message):
TOKEN = "156d754bf9a6f2fe5e9464886ab39463bdf88a06"

URL = f"https://api.waqi.info/feed/{location}/?token={TOKEN}"
print("Getting Data...")
print(f"Getting Data... Called by {message}")
response = requests.get(URL)
response_data = response.text
get_data = json.loads(response_data)
print(get_data)
print("Loading Data...")
pm25 = None
pm10 = None
Expand Down Expand Up @@ -89,12 +88,25 @@ def average_germany(value):
city_list[n] = city_list[n].replace("\n", "")
for city in range(city_list_len):
city_to_get_data_of = city_list[city]
city_data = get_data(city_to_get_data_of)[value]
city_data = get_data(city_to_get_data_of, message="Average Germany Function")[value]
if not city_data == None:
data = data + city_data
else:
city_list_len -= 1
data = data / city_list_len
print(data)
if value == "AQI":
with open("data/avg_aqi", "w") as avg_aqi:
avg_aqi.write(str(data))
elif value == "PM10":
with open("data/avg_pm10", "w") as avg_pm10:
avg_pm10.write(str(data))
elif value == "PM2.5":
with open("data/avg_pm25", "w") as avg_pm25:
avg_pm25.write(str(data))
elif value == "No2":
with open("data/avg_no2", "w") as avg_no2:
avg_no2.write(str(data))
return data
except:
return None
Expand Down Expand Up @@ -138,7 +150,7 @@ def scale_germany(aqi_avg, no2_avg, pm25_avg, pm10_avg, data):
points_forecast += 2

if points_forecast == 0:
forecast_day1_index = "Kein guter Tag"
forecast_day1_index = "Kein Guter Tag"
elif points_forecast == 2:
forecast_day1_index = "Guter Tag"
elif points_forecast == 4:
Expand Down Expand Up @@ -176,9 +188,9 @@ def scale_germany(aqi_avg, no2_avg, pm25_avg, pm10_avg, data):
if points == 6:
index = "Jetzt sofort"

return max_points, points, index, forecast_day1_index, points_forecast
return max_points, points, index, forecast_day1_index


if __name__ == '__main__':
data = get_data("Köln")
data = get_data("Köln", "Main")

1 change: 1 addition & 0 deletions server/data/avg_aqi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
23.166666666666668
1 change: 1 addition & 0 deletions server/data/avg_no2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
17.236363636363635
1 change: 1 addition & 0 deletions server/data/avg_pm10
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
13.916666666666666
1 change: 1 addition & 0 deletions server/data/avg_pm25
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
38.857142857142854
50 changes: 50 additions & 0 deletions server/scaleupdater.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import time
import backend

hour1 = int(time.strftime("%H"))

def average_germany(value):
try:
data = 0
cities = open("cities/cities", "r")
city_list = cities.readlines()
city_list_len = len(city_list)
for n in range(city_list_len):
city_list[n] = city_list[n].replace("\n", "")
for city in range(city_list_len):
city_to_get_data_of = city_list[city]
city_data = backend.get_data(city_to_get_data_of, "ScaleUpdater")[value]
if not city_data == None:
data = data + city_data
else:
city_list_len -= 1
data = data / city_list_len
print(data)
if value == "AQI":
with open("data/avg_aqi", "w") as avg_aqi:
avg_aqi.write(str(data))
elif value == "PM10":
with open("data/avg_pm10", "w") as avg_pm10:
avg_pm10.write(str(data))
elif value == "PM2.5":
with open("data/avg_pm25", "w") as avg_pm25:
avg_pm25.write(str(data))
elif value == "No2":
with open("data/avg_no2", "w") as avg_no2:
avg_no2.write(str(data))
except Exception as error:
print("Error in ScaleUpdater: ", error)

while True:
hour = int(time.strftime("%H"))
if hour == hour1:
average_germany("AQI")
average_germany("PM10")
average_germany("PM2.5")
average_germany("No2")
print("Waiting...")
if hour1 == "23":
hour1 = 0
else:
hour1 += 1

35 changes: 22 additions & 13 deletions server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
def home():
return render_template('main.html')

@app.route("/more")
def was():
return render_template('more.html')


@app.route('/style.css')
def style():
Expand All @@ -41,7 +37,7 @@ def script():
@app.route('/lueften.json')
def lueften():
stadt = request.args.get('stadt')
luft = backend.get_data(stadt)
luft = backend.get_data(stadt, "Server")
return jsonify({'stadt': stadt,
'luftqualitaet': luft})

Expand All @@ -50,28 +46,41 @@ def lueften():
def luft():
try:
country = request.args.get('country')
luft = backend.get_data(country)
luft = backend.get_data(country, "Server")
print(luft)
Stationsname = luft['Station Name']
Updatestatus = luft['Last Update']
quality = luft['Air Quality']
forecast_1_avg_pm25 = luft['Forecast PM2.5 Avg']
forecast_1_avg_pm10 = luft['Forecast PM10 Avg']

max_scale, scale, index, forecast_day1, points_forecast = backend.scale_germany(avg_aqi, avg_no2, avg_pm25, avg_pm10, luft)
with open("data/avg_aqi", "r")as avg_aqi_r:
avg_aqi = float(avg_aqi_r.read())

with open("data/avg_no2", "r")as avg_no2_r:
avg_no2 = float(avg_no2_r.read())

with open("data/avg_pm25", "r")as avg_pm25_r:
avg_pm25 = float(avg_pm25_r.read())

with open("data/avg_pm10", "r")as avg_pm10_r:
avg_pm10 = float(avg_pm10_r.read())

max_scale, scale, index, forecast_day1 = backend.scale_germany(avg_aqi, avg_no2, avg_pm25, avg_pm10, luft)

return render_template('daten.html', Stationsname=Stationsname, Updatestatus=Updatestatus, quality=quality,
lueften=lueften, max_scale=max_scale, scale=scale, index=index,
forecast_day1_index=forecast_day1, forecast_1_avg_pm25=forecast_1_avg_pm25,
forecast_1_avg_pm10=forecast_1_avg_pm10, points_forecast= points_forecast)
except:
forecast_1_avg_pm10=forecast_1_avg_pm10)
except Exception as error:
print(error)
return render_template('main.html', Error=True)


if __name__ == '__main__':
avg_aqi = backend.average_germany("AQI")
avg_pm10 = backend.average_germany("PM10")
avg_pm25 = backend.average_germany("PM2.5")
avg_no2 = backend.average_germany("No2")
backend.average_germany("AQI")
backend.average_germany("PM10")
backend.average_germany("PM2.5")
backend.average_germany("No2")

app.run(debug=True, host="0.0.0.0")

0 comments on commit ee9368f

Please sign in to comment.