myspace134v / modules /context_enhancer.py
rdune71's picture
new1
ab6d29f
raw
history blame
1.59 kB
import requests
import os
from datetime import datetime
def add_weather_context(location="London"):
"""Add current weather context to the query"""
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"
response = requests.get(url, timeout=5)
response.raise_for_status()
data = 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)}"
def add_space_weather_context():
"""Add space weather context to the query"""
try:
api_key = os.getenv("NASA_API_KEY")
if not api_key:
return "Space weather data unavailable (API key not configured)"
# Using a different NASA endpoint that doesn't require parameters
url = f"https://api.nasa.gov/planetary/apod?api_key={api_key}"
response = requests.get(url, timeout=5)
response.raise_for_status()
data = 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():
"""Add current time context"""
now = datetime.now()
return f"Current date and time: {now.strftime('%Y-%m-%d %H:%M:%S %Z')}"