about summary refs log tree commit diff
diff options
context:
space:
mode:
authorStarfall <us@starfall.systems>2023-01-09 20:21:10 -0600
committerStarfall <us@starfall.systems>2023-01-09 20:21:10 -0600
commit6636a4a0a59dc73cdd4a0e123363794574415df5 (patch)
treea47e9150d76bb5fa3db40b4730d480f7568db4cb
parentdb89bdad5ed68a31828d578ff794982e08f30fdc (diff)
parse cache-control max-age for caching
-rw-r--r--README.md1
-rw-r--r--fscache.py1
-rwxr-xr-xmeteor.py14
3 files changed, 9 insertions, 7 deletions
diff --git a/README.md b/README.md
index 95e9bc3..05a35a9 100644
--- a/README.md
+++ b/README.md
@@ -17,7 +17,6 @@ TODO / Issues
 
 - Geocoding would make this a little more usable. [NWS lists several potential services](https://weather-gov.github.io/api/general-faqs#geocoding)
 - Round decimals to 4 places ourselves to avoid errors
-- Cache responses appropriately (the API uses HTTP caching headers correctly, TBA if we want to drop in requests-cache or do it ourselves)
 - Use the /gridpoints/gridId/gridX,gridY endpoint to generate our own forecast with things like probabilityOfPrecipitation
 - Fancy output like wttr.in
 - Display the names of the zones (requires extra API calls - cacheable for a long duration, though)
diff --git a/fscache.py b/fscache.py
index 234e659..e549e92 100644
--- a/fscache.py
+++ b/fscache.py
@@ -30,6 +30,7 @@ class Cache:
             raise CacheMiss
 
         if time.time() > loc.stat().st_mtime:
+            loc.unlink()
             raise CacheMiss
         
         return loc.read_text()
diff --git a/meteor.py b/meteor.py
index 8b7ab2c..140eddb 100755
--- a/meteor.py
+++ b/meteor.py
@@ -1,7 +1,7 @@
 #!/usr/bin/env python3
 from argparse import ArgumentParser
 import requests
-import fscache as fs
+import fscache
 
 base_url = 'https://api.weather.gov'
 headers = {
@@ -50,9 +50,11 @@ def print_forecast(point):
 
     try: 
         print(cache.read(cache_key))
-    except fs.CacheMiss:
-        forecast_url = point.get('forecast')
-        forecast = requests.get(forecast_url, headers=headers).json()
+    except fscache.CacheMiss:
+        response = requests.get(point.get('forecast'), headers=headers)
+        # ugly cache_control parsing honestly, break out later
+        ttl = int(response.headers['cache-control'].split('max-age=')[-1].split(',')[0])
+        forecast = response.json()
 
         buffer = f'{term.BOLD}forecast for {point.get("relativeLocation").get("city")}, {point.get("relativeLocation").get("state")} ({cache_key})'
         buffer += '\n' + f'===================={term.RESET}'
@@ -66,13 +68,13 @@ def print_forecast(point):
         for period in periods[2:]:
             buffer += '\n' + f'{term.BOLD}{period.get("name").ljust(15)}{term.RESET}: {period.get("shortForecast")} - {period.get("temperature")}°{period.get("temperatureUnit")}, {period.get("windSpeed")} {period.get("windDirection")}'
 
-        cache.write(cache_key, buffer)
+        cache.write(cache_key, buffer, ttl)
         print(buffer)
 
 def main():
     args = handle_args()
     global cache
-    cache = fs.Cache('meteor.py')
+    cache = fscache.Cache('meteor.py')
 
     point = requests.get(base_url + f'/points/{args.lat},{args.lon}',
                          headers=headers).json()