5454
5555def get_data_source_url (api = "onecall" , location = None ):
5656 """Build and return the URL for the OpenWeather API."""
57- if api .upper () == "FORECAST5" :
58- URL = "https://api.openweathermap.org/data/2.5/forecast?"
59- URL += "q=" + location
57+ if api .upper () == "GEO" :
58+ URL = "https://api.openweathermap.org/geo/1.0/direct?q="
59+ URL += location
60+ elif api .upper () == "GEOREV" :
61+ URL = "https://api.openweathermap.org/geo/1.0/reverse?limit=1"
62+ URL += "&lat={}" .format (location [0 ])
63+ URL += "&lon={}" .format (location [1 ])
6064 elif api .upper () == "ONECALL" :
6165 URL = "https://api.openweathermap.org/data/2.5/onecall?exclude=minutely,hourly,alerts"
6266 URL += "&lat={}" .format (location [0 ])
6367 URL += "&lon={}" .format (location [1 ])
6468 else :
6569 raise ValueError ("Unknown API type: " + api )
66-
6770 return URL + "&appid=" + secrets ["openweather_token" ]
6871
6972
70- def get_latlon ():
71- """Use the Forecast5 API to determine lat/lon for given city."""
72- magtag .url = get_data_source_url (api = "forecast5" , location = secrets ["openweather_location" ])
73- magtag .json_path = ["city" ]
74- raw_data = magtag .fetch ()
75- return raw_data ["coord" ]["lat" ], raw_data ["coord" ]["lon" ]
73+ def get_latlon (city_name ):
74+ """Use the Geolocation API to determine lat/lon for given city."""
75+ magtag .url = get_data_source_url (api = "geo" , location = city_name )
76+ raw_data = eval (magtag .fetch ())[0 ]
77+ return raw_data ["lat" ], raw_data ["lon" ]
78+
79+
80+ def get_city (latlon_location ):
81+ """Use the Geolocation API to determine city for given lat/lon."""
82+ magtag .url = get_data_source_url (api = "georev" , location = latlon_location )
83+ raw_data = eval (magtag .fetch ())[0 ]
84+ return raw_data ["name" ] + ", " + raw_data ["country" ]
7685
7786
7887def get_forecast (location ):
@@ -170,16 +179,33 @@ def go_to_sleep(current_time):
170179 magtag .exit_and_deep_sleep (seconds_to_sleep )
171180
172181
182+ # ===========
183+ # Location
184+ # ===========
185+ if isinstance (secrets ["openweather_location" ], str ):
186+ # Get lat/lon using city name
187+ city = secrets ["openweather_location" ]
188+ print ("Getting lat/lon for city:" , city )
189+ latlon = get_latlon (city )
190+ elif isinstance (secrets ["openweather_location" ], tuple ):
191+ # Get city name using lat/lon
192+ latlon = secrets ["openweather_location" ]
193+ print ("Getting city name for lat/lon:" , latlon )
194+ city = get_city (latlon )
195+ else :
196+ raise ValueError ("Unknown location:" , secrets ["openweather_location" ])
197+
198+ print ("City =" , city )
199+ print ("Lat/Lon = " , latlon )
200+
173201# ===========
174202# U I
175203# ===========
176204today_date = label .Label (terminalio .FONT , text = "?" * 30 , color = 0x000000 )
177205today_date .anchor_point = (0 , 0 )
178206today_date .anchored_position = (15 , 13 )
179207
180- city_name = label .Label (
181- terminalio .FONT , text = secrets ["openweather_location" ], color = 0x000000
182- )
208+ city_name = label .Label (terminalio .FONT , text = city , color = 0x000000 )
183209city_name .anchor_point = (0 , 0 )
184210city_name .anchored_position = (15 , 24 )
185211
@@ -249,11 +275,6 @@ def go_to_sleep(current_time):
249275# ===========
250276# M A I N
251277# ===========
252- print ("Getting Lat/Lon..." )
253- latlon = get_latlon ()
254- print (secrets ["openweather_location" ])
255- print (latlon )
256-
257278print ("Fetching forecast..." )
258279forecast_data , utc_time , local_tz_offset = get_forecast (latlon )
259280
@@ -271,4 +292,3 @@ def go_to_sleep(current_time):
271292go_to_sleep (utc_time + local_tz_offset )
272293# entire code will run again after deep sleep cycle
273294# similar to hitting the reset button
274-
0 commit comments