uep-asi/weather_api.py
Mikołaj Kaczmarek 8c7c4ceb19
init
2025-06-15 21:49:12 +02:00

27 lines
668 B
Python

import requests
import json
def decode_weather(weather):
weather_from_json = json.loads(weather)
temp = weather_from_json["main"]["temp"]
humidity = weather_from_json["main"]["humidity"]
type = weather_from_json["weather"][0]["main"]
rain = "RAIN" if type == "rain" else "NO RAIN"
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...
weather = weather_request.content.strip()
temp, humidity, type, rain = decode_weather(weather)
return temp, humidity, type, rain