skatingP / agents /MoonAgent.py
sp103107's picture
agents
21f953b verified
Raw History Blame Contribute Delete
1.77 kB
import requests
import logging
from swarm import Agent, Swarm
# Configure logging
logging.basicConfig(level=logging.INFO)
def fetch_moon_phase():
"""
Fetch moon phase data from the USGS STAC API.
Retrieves the latest moon phase information and handles errors gracefully.
"""
url = "https://stac.astrogeology.usgs.gov/api/search"
params = {
"collections": ["lunar_orbiter_laser_altimeter"],
"limit": 1
}
try:
# Add timeout to avoid hanging requests
response = requests.post(url, json=params, timeout=10)
# Check for successful response
if response.status_code == 200:
data = response.json()
# Validate the structure of the response
if 'features' in data and len(data['features']) > 0:
phase = data['features'][0]['properties'].get('Phase', 'Unknown phase')
logging.info(f"Fetched Moon Phase: {phase}")
return {"moon_phase": phase}
else:
logging.error("Unexpected response format: Missing 'features'")
return {"error": "Moon phase data unavailable"}
else:
logging.error(f"USGS API error: {response.status_code} - {response.text}")
return {"error": "Moon phase data unavailable"}
except requests.Timeout:
logging.error("USGS API request timed out.")
return {"error": "Request to USGS API timed out."}
except requests.RequestException as e:
logging.error(f"Error fetching moon phase data: {str(e)}")
return {"error": f"Request exception: {str(e)}"}
moon_agent = Agent(
name="MoonAgent",
instructions="Fetch moon phase data from the USGS API.",
functions=[fetch_moon_phase]
)