File size: 882 Bytes
bf06bdc |
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 |
import requests
import os
from dotenv import load_dotenv
# Load your API key from .env file
load_dotenv()
API_KEY = os.getenv("OPENWEATHER_API_KEY")
def get_weather(city):
"""
This function takes an city name as input, then uses the Open Weather API to
retrieve temp in celsius, humidity, and description
"""
url = "https://api.openweathermap.org/data/2.5/weather"
params = {
"q": city,
"appid": API_KEY,
"units": "metric" # temperature in Celsius
}
response = requests.get(url, params=params)
data = response.json()
humidity = data["main"]["humidity"]
temp = data["main"]["temp"]
description = data["weather"][0]["description"]
return temp, humidity, description
# print(f"City = {city}")
# print(f"Temperature = {temp}")
# print(f"Humidity = {humidity}")
# print(f"Description = {description}") |