diff --git a/src/main.py b/src/main.py index d775739..4f7af2f 100644 --- a/src/main.py +++ b/src/main.py @@ -93,11 +93,13 @@ def register(): sqlsession.commit() return f"

Zarejestrowano użytkownika: {username} ({nickname})

" -@app.route("/pogoda") -def pogoda(): - temp, humidity, type, rain = get_weather() - - return render_template("pogoda.html", temp=temp, humid=humidity, weathertype=type, rain=rain) +@app.route("/pogoda/") +def pogoda(city): + result = get_weather(city) + if not result: + return f"

Nie udało się pobrać pogody dla miasta: {city}

", 404 + temp, humidity, type, rain = result + return render_template("pogoda.html", temp=temp, humid=humidity, weathertype=type, rain=rain, city=city) if __name__ == '__main__': app.run(debug=False) \ No newline at end of file diff --git a/src/templates/pogoda.html b/src/templates/pogoda.html index 80cff85..7afeea6 100644 --- a/src/templates/pogoda.html +++ b/src/templates/pogoda.html @@ -10,6 +10,7 @@ DIV ID col-sm-X gdzie x to szerokosc gdzie 12 to 100% strony (mozna podzielic st
+

Pogoda dla miasta: {{ city if city else 'nieznane' }}

Weather: {{ weathertype }}
diff --git a/src/weather_api.py b/src/weather_api.py index e898a33..9aa012f 100644 --- a/src/weather_api.py +++ b/src/weather_api.py @@ -1,3 +1,4 @@ +import os import requests import json @@ -12,15 +13,15 @@ def decode_weather(weather): return temp, humidity, type, rain -def get_weather(): - weather_request = requests.get( - "http://samples.openweathermap.org/data/2.5/weather?q=Warsaw,pl&units=metric" - ) - - # Czyszczenie odpowiedzi... +def get_weather(city): + api_key = os.environ.get("OPENWEATHER_API_KEY") + if not api_key: + raise Exception("Brak klucza API w zmiennej środowiskowej OPENWEATHER_API_KEY") + url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&units=metric&appid={api_key}&lang=pl" + weather_request = requests.get(url) + if weather_request.status_code != 200: + return None weather = weather_request.content.strip() - temp, humidity, type, rain = decode_weather(weather) - return temp, humidity, type, rain