From 870b937bf4dc78dd899b9a94da56147d1c7b443e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kaczmarek?= <12432719+AN0DA@users.noreply.github.com> Date: Sun, 15 Jun 2025 22:12:02 +0200 Subject: [PATCH] Zadanie 5 --- src/main.py | 12 +++++++----- src/templates/pogoda.html | 1 + src/weather_api.py | 17 +++++++++-------- 3 files changed, 17 insertions(+), 13 deletions(-) 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"
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