From e3b8661f1ecd62432fea07a6c15d2b81b80b7865 Mon Sep 17 00:00:00 2001 From: Trevor Steen Date: Tue, 23 Sep 2025 10:35:00 -0600 Subject: [PATCH 1/3] update Data API URL and implement HTTP native caching --- apps/metar/metar.star | 58 ++++++++----------------------------------- 1 file changed, 11 insertions(+), 47 deletions(-) diff --git a/apps/metar/metar.star b/apps/metar/metar.star index 05e9e730b2..48305fb524 100644 --- a/apps/metar/metar.star +++ b/apps/metar/metar.star @@ -12,8 +12,8 @@ load("http.star", "http") load("render.star", "render") load("schema.star", "schema") -ADDS_URL = "https://www.aviationweather.gov/cgi-bin/data/dataserver.php?dataSource=metars&requestType=retrieve&format=csv&stationString=%s&mostrecentforeachstation=constraint&hoursBeforeNow=2" -DEFAULT_AIRPORT = "KJFK, KLGA, KBOS, KDCA" +ADDS_URL = "https://aviationweather.gov/api/data/metar?ids=%s&format=json&hours=2" +DEFAULT_AIRPORT = "KJFK, KLGA, KBOS, KDCA, KBJC, KLMO, KGXY, KAPA" # encryption, schema # fail expired, add timeout to Root @@ -22,63 +22,27 @@ DEFAULT_AIRPORT = "KJFK, KLGA, KBOS, KDCA" MAX_AGE = 60 * 10 def decoded_result_for_airport(airport): - cache_key = "metar_cache_" + airport - cached_result = cache.get(cache_key) - if (cached_result != None): - result = cached_result - else: - rep = http.get(ADDS_URL % airport) - if rep.status_code != 200: - return { - "color": "#000000", - "text": "Received error %s for %s" % (rep.status_code, airport), - "flight_category": "ERR", - } - - result = rep.body() - - # TODO: Determine if this cache call can be converted to the new HTTP cache. - cache.set(cache_key, result, ttl_seconds = 60) - print("fetched for %s" % airport) - - lines = result.strip().split("\n") - - key_line = None - data_line = None - - for line in lines: - if line.startswith("raw_text"): - key_line = line - elif line.startswith(airport + " "): - data_line = line - - if data_line == None: + rep = http.get(ADDS_URL % airport, ttl_seconds = 60) + if rep.status_code != 200: return { "color": "#000000", - "text": "Invalid airport code %s" % airport, + "text": "Received error %s for %s" % (rep.status_code, airport), "flight_category": "ERR", } - if key_line == None: - return { - "color": "#000000", - "text": "Could not parse METAR", - "flight_category": "ERR", - } + result = rep.json()[0] - decoded = {} - for label, value in zip(key_line.split(","), data_line.split(",")): - decoded[label] = value + print("fetched for %s" % airport) response = { - "color": color_for_state(decoded), - "text": decoded["raw_text"], - "flight_category": decoded["flight_category"], + "color": color_for_state(result), + "text": result["rawOb"], + "flight_category": result["fltCat"], } return response def color_for_state(result): - category = result["flight_category"] + category = result["fltCat"] if category == "VFR": return "#00FF00" elif category == "IFR": From 360accd916591545242e4bfa75b1662422c7d0da Mon Sep 17 00:00:00 2001 From: Trevor Steen Date: Tue, 23 Sep 2025 10:36:07 -0600 Subject: [PATCH 2/3] lint --- apps/metar/metar.star | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/metar/metar.star b/apps/metar/metar.star index 48305fb524..8c43ec4e53 100644 --- a/apps/metar/metar.star +++ b/apps/metar/metar.star @@ -7,7 +7,6 @@ Description: Show METAR (aviation weather) text for one airport or flight by commas to display multiple airports. """ -load("cache.star", "cache") load("http.star", "http") load("render.star", "render") load("schema.star", "schema") From adff274ffc7936917426b13ab0b9f2d6abb892fb Mon Sep 17 00:00:00 2001 From: Trevor Steen Date: Tue, 23 Sep 2025 10:39:06 -0600 Subject: [PATCH 3/3] reset default icao codes --- apps/metar/metar.star | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/metar/metar.star b/apps/metar/metar.star index 8c43ec4e53..7909b9d5d1 100644 --- a/apps/metar/metar.star +++ b/apps/metar/metar.star @@ -12,7 +12,7 @@ load("render.star", "render") load("schema.star", "schema") ADDS_URL = "https://aviationweather.gov/api/data/metar?ids=%s&format=json&hours=2" -DEFAULT_AIRPORT = "KJFK, KLGA, KBOS, KDCA, KBJC, KLMO, KGXY, KAPA" +DEFAULT_AIRPORT = "KJFK, KLGA, KBOS, KDCA" # encryption, schema # fail expired, add timeout to Root