kolkhi commited on
Commit
b148632
·
verified ·
1 Parent(s): 006ce37

Fetch sunrise/sunset data from API

Browse files
Files changed (1) hide show
  1. app.py +16 -3
app.py CHANGED
@@ -18,9 +18,22 @@ def sunrise_sunset_tool(lat:str, lng:str)-> str: #it's import to specify the ret
18
  lng: A string representing location longitude
19
  """
20
  try:
21
- sunrise = "7:27:02 AM"
22
- sunset = "5:05:55 PM"
23
- return f"The sunrise time is {sunrise} and sunset time is {sunset}"
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  except Exception as e:
25
  return f"Error fetching sunrise/sunset data for location lat={lat} long={lng}"
26
 
 
18
  lng: A string representing location longitude
19
  """
20
  try:
21
+ api_url = "https://api.sunrise-sunset.org/json"
22
+ payload = {'lat': lat, 'lng': lng}
23
+ ret = requests.get(api_url, params=payload)
24
+ js = ret.json()
25
+
26
+ if ret.status_code == 200 and js['status']=='OK':
27
+ sunrise = js["results"]["sunrise"]
28
+ sunset = js["results"]["sunset"]
29
+ solar_noon = js["results"]["solar_noon"]
30
+ time_zone = js["tzid"]
31
+ elif ret.status_code == 200:
32
+ return f"Error fetching sunrise/sunset data for location lat={lat} long={lng}. Error code = {js["status"]}"
33
+ else:
34
+ return f"Error fetching sunrise/sunset data for location lat={lat} long={lng}. Error code = {ret.status_code}, response={js}"
35
+
36
+ return f"The sunrise time is at {sunrise} and sunset time is at {sunset}. Solar noon is at {solar_noon}. Timezone {time_zone}"
37
  except Exception as e:
38
  return f"Error fetching sunrise/sunset data for location lat={lat} long={lng}"
39