Skip to content

Instantly share code, notes, and snippets.

@drudge
Last active December 29, 2021 04:50
Show Gist options
  • Save drudge/263fd18f381bcb610146b14b1dd86298 to your computer and use it in GitHub Desktop.
Save drudge/263fd18f381bcb610146b14b1dd86298 to your computer and use it in GitHub Desktop.
Public IP Address Applet for Tidbyt
# Public IP Tidbyt App
# MIT License
# by Nicholas Penree, Dec 28 2021
load("render.star", "render")
load("http.star", "http")
load("cache.star", "cache")
IPIFY_JSON_URL = "https://api.ipify.org/?format=json"
MOCK = False
def get_ip():
if MOCK:
return "123.123.123.123"
else:
ip_cached = cache.get("ip")
if ip_cached != None:
ip = ip_cached
else:
rep = http.get(IPIFY_JSON_URL)
if rep.status_code != 200:
fail("HTTP request failed with status %d", rep.status_code)
ip = rep.json()["ip"]
cache.set("ip", ip, ttl_seconds=240)
return ip
def main(config):
ip = get_ip()
return render.Root(
render.Box(
child = render.Column(
expanded = True,
main_align = "center",
cross_align = "center",
children = [
render.Text(
color = config.get("color", "#72A5E8"),
content = config.get("label", "Public IP"),
font = "Dina_r400-6",
),
render.Box(color = "#666", height = 1),
render.Box(color = "#000", height = 1),
render.Text(content = ip, font = "tom-thumb"),
render.Box(color = "#666", height = 1),
],
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment