1
- """
2
- Applet: Weatherbard
3
- Summary: Weather + Poem
4
- Description: Shows current weather and a poetic line in the style of Mary Oliver.
5
- Author: mgtkach
6
- """
1
+ # Applet: Weatherbard
2
+ # Summary: Weather + Poem
3
+ # Description: Shows current weather and a poetic line in the style of Mary Oliver.
4
+ # Author: mgtkach
7
5
6
+ load ("cache.star" , "cache" )
7
+ load ("encoding/json.star" , "json" )
8
8
load ("http.star" , "http" )
9
9
load ("math.star" , "math" )
10
10
load ("render.star" , "render" )
11
11
load ("schema.star" , "schema" )
12
+ load ("secret.star" , "secret" )
12
13
load ("time.star" , "time" )
13
14
14
15
poems = {
@@ -252,7 +253,7 @@ def get_schema():
252
253
schema .Text (
253
254
id = "location" ,
254
255
name = "Location" ,
255
- desc = "Enter your city and country code (e.g. London,UK) or zip/postal code " ,
256
+ desc = "Enter your city and country code (e.g. London,UK)" ,
256
257
icon = "mapPin" ,
257
258
default = "Washington,DC,US" ,
258
259
),
@@ -271,18 +272,50 @@ def get_schema():
271
272
)
272
273
273
274
def main (ctx ):
274
- location = ctx .get ("location" , "Washington,DC,US" )
275
- units = ctx .get ("units" , "imperial" )
276
- weather_api_key = "e23111dae660742eb27ed3fdccccc7d3"
275
+ location = ctx .get ("location" )
276
+ if location == None :
277
+ location = "Washington,DC,US"
278
+ else :
279
+ location = str (location )
280
+
281
+ units = ctx .get ("units" )
282
+ if units == None :
283
+ units = "imperial"
284
+ else :
285
+ units = str (units )
286
+
287
+ encrypted_key = ctx .get ("weather_api_key" )
288
+ weather_api_key = None
289
+ if encrypted_key != None :
290
+ weather_api_key = secret .decrypt (encrypted_key )
291
+ else :
292
+ weather_api_key = ctx .get ("dev_api_key" )
293
+
294
+ if weather_api_key == None :
295
+ return render .Root (
296
+ child = render .Text ("Missing API key" , font = "6x13" ),
297
+ )
277
298
278
299
# Fetch weather
279
- weather_data = http .get (
280
- "https://api.openweathermap.org/data/2.5/weather?q=" + location +
281
- "&appid=" + weather_api_key + "&units=" + units ,
282
- ).json ()
300
+ cache_key = "weatherbard:" + location + ":" + units
301
+ weather_json = cache .get (cache_key )
302
+
303
+ if weather_json == None :
304
+ weather_data = http .get (
305
+ "https://api.openweathermap.org/data/2.5/weather?q=" + location +
306
+ "&appid=" + weather_api_key + "&units=" + units ,
307
+ ).json ()
308
+ cache .set (cache_key , json .encode (weather_data ), ttl_seconds = 600 )
309
+ else :
310
+ weather_data = json .decode (weather_json )
311
+
283
312
condition = weather_data ["weather" ][0 ]["main" ].lower ()
284
313
unit_label = "°F" if units == "imperial" else "°C"
285
- temp = str (int (weather_data ["main" ]["temp" ])) + unit_label
314
+ raw_temp = weather_data ["main" ].get ("temp" )
315
+ if raw_temp != None :
316
+ temp = str (int (raw_temp )) + unit_label
317
+ else :
318
+ temp = "--" + unit_label
286
319
287
320
# Select poem locally and rotate lines
288
321
poem_lines = poems .get (condition , ["No verse today." , "Sky is silent." ])
0 commit comments