jaidesign commited on
Commit
ffefc82
·
verified ·
1 Parent(s): 97e2f08

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -16
app.py CHANGED
@@ -56,24 +56,37 @@ def get_current_time_in_timezone(timezone: str) -> str:
56
  except Exception as e:
57
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
58
 
 
59
  @tool
60
- def get_weather(location: str = None, zipcode: int = None) -> str:
61
- """Fetches the current weather for a specified location or zip code.
62
-
63
- Args:
64
- location: A string representing a valid city name or region (e.g., 'New York').
65
- zipcode: An integer representing a valid zip code (e.g., 10001 for NYC).
66
-
67
- Returns:
68
- A string describing the weather conditions, temperature, and humidity.
69
- """
70
- temperature = data["main"]["temp"]
71
- conditions = data["weather"][0]["description"]
72
- humidity = data["main"]["humidity"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
- return (f"The current Weather in {location} is: {conditions.capitalize()},"
75
- f"Temperature: {temperature}°C, Humidity: {humidity}%")
76
-
77
 
78
  @tool
79
  def getMarsWeather() -> str:
 
56
  except Exception as e:
57
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
58
 
59
+
60
  @tool
61
+ import requests
62
+ import re
63
+
64
+ def fetch_weather(city):
65
+ try:
66
+ url = f"https://www.weather.com/weather/today/l/{city}"
67
+ response = requests.get(url)
68
+
69
+ if response.status_code != 200:
70
+ return "Failed to fetch weather data."
71
+
72
+ # Extract temperature and condition using regex
73
+ temp_match = re.search(r'"temperature":(\d+)', response.text)
74
+ condition_match = re.search(r'"wxPhrase":"(.*?)"', response.text)
75
+
76
+ if temp_match and condition_match:
77
+ temperature = f"{temp_match.group(1)}°C"
78
+ condition = condition_match.group(1)
79
+ return f"Current weather in {city}: {temperature}, {condition}"
80
+
81
+ return "Weather data not found."
82
+
83
+ except Exception as e:
84
+ return f"Error fetching weather: {str(e)}"
85
+
86
+ # Example usage
87
+ city_code = "San Francisco, CA"
88
+ print(fetch_weather(city_code))
89
 
 
 
 
90
 
91
  @tool
92
  def getMarsWeather() -> str: