Skip to content

Commit 7fd4d32

Browse files
committed
Fix API key handling and error fallback for Tidbyt app
1 parent 3bd9b12 commit 7fd4d32

File tree

2 files changed

+51
-15
lines changed

2 files changed

+51
-15
lines changed

apps/weatherbard/manifest.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ config: true
88
widgets:
99
- weatherbard.star
1010
preview: weatherbard.gif
11+
secrets:
12+
- weather_api_key
13+

apps/weatherbard/weatherbard.star

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
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
75

6+
load("cache.star", "cache")
7+
load("encoding/json.star", "json")
88
load("http.star", "http")
99
load("math.star", "math")
1010
load("render.star", "render")
1111
load("schema.star", "schema")
12+
load("secret.star", "secret")
1213
load("time.star", "time")
1314

1415
poems = {
@@ -252,7 +253,7 @@ def get_schema():
252253
schema.Text(
253254
id = "location",
254255
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)",
256257
icon = "mapPin",
257258
default = "Washington,DC,US",
258259
),
@@ -271,18 +272,50 @@ def get_schema():
271272
)
272273

273274
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+
)
277298

278299
# 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+
283312
condition = weather_data["weather"][0]["main"].lower()
284313
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
286319

287320
# Select poem locally and rotate lines
288321
poem_lines = poems.get(condition, ["No verse today.", "Sky is silent."])

0 commit comments

Comments
 (0)