File size: 2,721 Bytes
d1a5601
7a1f0f0
d1a5601
60c04c3
b0cf9c2
60c04c3
d1a5601
60c04c3
 
 
b0cf9c2
 
7a1f0f0
 
60c04c3
 
 
d1a5601
 
 
60c04c3
 
 
 
 
 
 
d1a5601
60c04c3
d1a5601
60c04c3
 
 
 
 
7a1f0f0
60c04c3
d1a5601
60c04c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1a5601
60c04c3
 
 
 
d1a5601
 
 
60c04c3
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os
import requests
from datetime import datetime
import logging
from config import WEATHER_API_KEY, GROQ_API_KEY
from .weather_agent import WeatherAgent

# Set up logging
logger = logging.getLogger(__name__)

# Initialize the weather agent with API key
weather_agent = WeatherAgent(groq_api_key=GROQ_API_KEY)

def get_weather():
    """Fetch weather data from OpenWeather API"""
    logger.info("Attempting to fetch weather data from OpenWeather API")
    
    # Coordinates for Black Rock Desert, Nevada
    lat = 40.7864
    lon = -119.2065
    
    # Use Current Weather Data API endpoint
    url = f"https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon=-119.2065&units=imperial&appid={WEATHER_API_KEY}"
    
    logger.info(f"Using API key: {WEATHER_API_KEY[:5]}...")
    logger.info(f"Using URL: {url}")
    
    try:
        logger.info("Making request to OpenWeather API")
        response = requests.get(url)
        if response.status_code == 401:
            logger.error(f"OpenWeather API key is invalid or expired. Status: {response.status_code}")
            logger.error(f"Response text: {response.text}")
            return None
        response.raise_for_status()
        data = response.json()
        logger.info("Successfully received weather data")

        # Format current weather data
        forecast = [{
            "date": datetime.fromtimestamp(data['dt']).strftime("%Y-%m-%d"),
            "high": round(data['main']['temp_max']),
            "low": round(data['main']['temp_min']),
            "conditions": data['weather'][0]['main'],
            "description": data['weather'][0]['description'],
            "wind_speed": round(data['wind']['speed']),
            "humidity": data['main']['humidity'],
            "feels_like": round(data['main']['feels_like']),
            "pressure": data['main']['pressure'],
            "visibility": data['visibility']
        }]
        
        logger.info(f"Processed weather data successfully")
        return forecast
    except requests.exceptions.RequestException as e:
        logger.error(f"Error fetching weather data: {str(e)}")
        if hasattr(e.response, 'text'):
            logger.error(f"API Response: {e.response.text}")
        return None

def format_weather_report(forecast):
    """Get AI-enhanced weather report"""
    logger.info("Formatting weather report with AI analysis")
    try:
        report = weather_agent.analyze_weather_data(forecast)
        logger.info("Successfully generated AI weather analysis")
        return report
    except Exception as e:
        logger.error(f"Error in AI analysis, falling back to basic format: {str(e)}")
        return weather_agent.format_basic_forecast(forecast)