interstellar_ai/py/weather.py

45 lines
1.7 KiB
Python
Raw Permalink 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-11 10:15:11 +02:00
# Collect weather data
2024-10-09 18:35:38 +02:00
data = {
"temperature": weather.temperature,
"humidity": weather.humidity,
2024-10-09 20:20:04 +02:00
"unit": str(weather.unit),
2024-10-09 18:35:38 +02:00
"datetime": weather.datetime,
"coordinates": weather.coordinates,
"country": weather.country,
"description": weather.description,
"feels_like": weather.feels_like,
2024-10-09 20:20:04 +02:00
"kind": str(weather.kind),
2024-10-09 18:35:38 +02:00
"local_population": weather.local_population,
2024-10-09 20:20:04 +02:00
"locale": str(weather.locale),
2024-10-09 18:35:38 +02:00
"location": weather.location,
"precipitation": weather.precipitation,
"pressure": weather.pressure,
"region": weather.region,
2024-10-09 20:20:04 +02:00
"ultraviolet": str(weather.ultraviolet),
2024-10-09 18:35:38 +02:00
"visibility": weather.visibility,
2024-10-09 20:20:04 +02:00
"wind_direction": str(weather.wind_direction),
2024-10-09 18:35:38 +02:00
"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))