interstellar_ai/py/weather.py

45 lines
1.7 KiB
Python
Raw Normal View History

2024-09-24 14:18:57 +02:00
import python_weather
2024-10-09 18:35:38 +02:00
import asyncio
2024-09-24 14:18:57 +02:00
class Weather:
@staticmethod
2024-10-09 18:35:38 +02:00
def getweather(unit_type, city):
# Define an inner asynchronous function
async def fetch_weather(unit_type):
if unit_type == "imperial":
unit_type = python_weather.IMPERIAL
elif unit_type == "metric":
unit_type = python_weather.METRIC
2024-09-24 14:18:57 +02:00
2024-10-09 18:35:38 +02:00
async with python_weather.Client(unit=unit_type) as client:
weather = await client.get(city)
2024-09-24 14:18:57 +02:00
2024-10-09 18:35:38 +02:00
data = {
"temperature": weather.temperature,
"humidity": weather.humidity,
"unit": weather.unit,
"datetime": weather.datetime,
"coordinates": weather.coordinates,
"country": weather.country,
"daily_forecasts": weather.daily_forecasts,
"description": weather.description,
"feels_like": weather.feels_like,
"kind": weather.kind,
"local_population": weather.local_population,
"locale": weather.locale,
"location": weather.location,
"precipitation": weather.precipitation,
"pressure": weather.pressure,
"region": weather.region,
"ultraviolet": weather.ultraviolet,
"visibility": weather.visibility,
"wind_direction": weather.wind_direction,
"wind_speed": weather.wind_speed,
}
2024-09-24 14:18:57 +02:00
2024-10-09 18:35:38 +02:00
return data
2024-09-24 14:18:57 +02:00
2024-10-09 18:35:38 +02:00
# Run the asynchronous function and return the result
return asyncio.run(fetch_weather(unit_type))