Spaces:
Running
Running
File size: 1,561 Bytes
001a1f0 ab6d29f 001a1f0 ab6d29f 001a1f0 ab6d29f 001a1f0 ab6d29f 001a1f0 ab6d29f 001a1f0 ab6d29f 001a1f0 ab6d29f |
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 36 37 38 |
import aiohttp
import os
from datetime import datetime
async def add_weather_context(location="London"):
try:
api_key = os.getenv("OPENWEATHER_API_KEY")
if not api_key:
return "Weather data unavailable (API key not configured)"
url = f"http://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}&units=metric"
async with aiohttp.ClientSession() as session:
async with session.get(url, timeout=5) as response:
response.raise_for_status()
data = await response.json()
return f"Current weather in {location}: {data['weather'][0]['description']}, {data['main']['temp']}°C"
except Exception as e:
return f"Weather data unavailable: {str(e)}"
async def add_space_weather_context():
try:
api_key = os.getenv("NASA_API_KEY")
if not api_key:
return "Space weather data unavailable (API key not configured)"
url = f"https://api.nasa.gov/planetary/apod?api_key={api_key}"
async with aiohttp.ClientSession() as session:
async with session.get(url, timeout=5) as response:
response.raise_for_status()
data = await response.json()
return f"Space context: Astronomy Picture of the Day - {data.get('title', 'N/A')}"
except Exception as e:
return f"Space weather data unavailable: {str(e)}"
def add_time_context():
now = datetime.now()
return f"Current date and time: {now.strftime('%Y-%m-%d %H:%M:%S %Z')}"
|