dveerasa commited on
Commit
eb29762
·
verified ·
1 Parent(s): 5917f31

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py CHANGED
@@ -70,6 +70,62 @@ def get_quote_in_euros(query: str, currency: str = "USD") -> str:
70
 
71
  except Exception as e:
72
  return f"An error occurred: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
  @tool
75
  def get_current_time_in_timezone(timezone: str) -> str:
 
70
 
71
  except Exception as e:
72
  return f"An error occurred: {str(e)}"
73
+
74
+ @tool
75
+ def get_sunrise_sunset_times(city: str) -> str:
76
+ """
77
+ Fetches sunrise and sunset times for a given city, using free OpenStreetMap
78
+ (Nominatim) for geocoding and sunrise-sunset.org for the actual times.
79
+
80
+ Args:
81
+ city: Name of the city (e.g., "Berlin" or "Los Angeles").
82
+
83
+ Returns:
84
+ A human-readable string describing sunrise and sunset times for that city,
85
+ or an error message if something goes wrong.
86
+ """
87
+ try:
88
+ # 1. Geocode the city name to get latitude/longitude
89
+ geocode_url = "https://nominatim.openstreetmap.org/search"
90
+ geocode_params = {
91
+ "q": city,
92
+ "format": "json",
93
+ "limit": 1
94
+ }
95
+ geocode_resp = requests.get(geocode_url, params=geocode_params, timeout=15)
96
+ geocode_data = geocode_resp.json()
97
+
98
+ if not geocode_data:
99
+ return f"Could not find location for '{city}'. Check spelling or try a different query."
100
+
101
+ lat = geocode_data[0]["lat"]
102
+ lon = geocode_data[0]["lon"]
103
+
104
+ # 2. Use sunrise-sunset.org to get sunrise/sunset times
105
+ sun_api_url = "https://api.sunrise-sunset.org/json"
106
+ sun_api_params = {
107
+ "lat": lat,
108
+ "lng": lon,
109
+ "formatted": 0 # return times in ISO 8601, which includes UTC offsets
110
+ }
111
+ sun_resp = requests.get(sun_api_url, params=sun_api_params, timeout=15)
112
+ sun_data = sun_resp.json()
113
+
114
+ if sun_data["status"] != "OK":
115
+ return f"Sunrise-sunset API error for location '{city}'."
116
+
117
+ sunrise = sun_data["results"]["sunrise"]
118
+ sunset = sun_data["results"]["sunset"]
119
+
120
+ return (
121
+ f"For '{city}':\n"
122
+ f" • Sunrise (local time): {sunrise}\n"
123
+ f" • Sunset (local time): {sunset}\n"
124
+ "Times shown in UTC-based format; actual local times may vary by time zone."
125
+ )
126
+
127
+ except Exception as e:
128
+ return f"An error occurred while fetching sunrise/sunset times: {str(e)}"
129
 
130
  @tool
131
  def get_current_time_in_timezone(timezone: str) -> str: