Kserus commited on
Commit
bd3468f
·
verified ·
1 Parent(s): 46557f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py CHANGED
@@ -54,6 +54,71 @@ def get_weather(location: str) -> str:
54
  except Exception as e:
55
  return f"Error fetching weather data: {str(e)}"
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  @tool
58
  def get_current_time_in_timezone(timezone: str) -> str:
59
  """A tool that fetches the current local time in a specified timezone.
 
54
  except Exception as e:
55
  return f"Error fetching weather data: {str(e)}"
56
 
57
+ @tool
58
+ def get_weather_forecast(location: str, days: int = 3) -> str:
59
+ """Get weather forecast for a specified location for the next few days.
60
+ Args:
61
+ location: City name and optionally country code (e.g., 'London' or 'London,UK')
62
+ days: Number of days for forecast (1-5, default 3)
63
+ """
64
+ try:
65
+ # Validate days input
66
+ days = max(1, min(5, int(days)))
67
+
68
+ API_KEY = "de1f560f2d4bea4e02dcdbe2e6f197a6"
69
+ base_url = "http://api.openweathermap.org/data/2.5/forecast"
70
+
71
+ params = {
72
+ 'q': location,
73
+ 'appid': API_KEY,
74
+ 'units': 'metric',
75
+ 'cnt': days * 8 # 8 forecasts per day (every 3 hours)
76
+ }
77
+
78
+ response = requests.get(base_url, params=params)
79
+ data = response.json()
80
+
81
+ if response.status_code == 200:
82
+ forecast = []
83
+ current_date = ""
84
+
85
+ for item in data['list']:
86
+ date = item['dt_txt'].split()[0]
87
+ if date != current_date:
88
+ if current_date:
89
+ forecast.append(day_forecast)
90
+ current_date = date
91
+ day_forecast = {
92
+ 'date': date,
93
+ 'temps': [],
94
+ 'weathers': []
95
+ }
96
+
97
+ day_forecast['temps'].append(item['main']['temp'])
98
+ day_forecast['weathers'].append(item['weather'][0]['description'])
99
+
100
+ # Add the last day
101
+ if current_date:
102
+ forecast.append(day_forecast)
103
+
104
+ # Format the response
105
+ result = [f"Weather forecast for {location}:"]
106
+ for day in forecast[:days]: # Limit to requested days
107
+ avg_temp = sum(day['temps']) / len(day['temps'])
108
+ main_weather = max(set(day['weathers']), key=day['weathers'].count)
109
+
110
+ result.append(
111
+ f"{day['date']}: {main_weather}, average temp {avg_temp:.1f}°C "
112
+ f"(min {min(day['temps']):.1f}°C, max {max(day['temps']):.1f}°C)"
113
+ )
114
+
115
+ return "\n".join(result)
116
+ else:
117
+ return f"Error getting forecast: {data.get('message', 'Unknown error')}"
118
+
119
+ except Exception as e:
120
+ return f"Error fetching weather forecast: {str(e)}"
121
+
122
  @tool
123
  def get_current_time_in_timezone(timezone: str) -> str:
124
  """A tool that fetches the current local time in a specified timezone.