aboutsummaryrefslogtreecommitdiffstats
path: root/fnme
diff options
context:
space:
mode:
Diffstat (limited to 'fnme')
-rw-r--r--fnme/data.py32
1 files changed, 27 insertions, 5 deletions
diff --git a/fnme/data.py b/fnme/data.py
index bdec6df..17f30c5 100644
--- a/fnme/data.py
+++ b/fnme/data.py
@@ -1,13 +1,35 @@
-from io import StringIO
+import os
+from pathlib import Path
from typing import Optional
import pandas as pd
import requests
+from platformdirs import user_cache_path
from fnme.constants import ENDPOINT, HEADERS
-def get_latest_data() -> tuple[pd.DataFrame, Optional[str]]:
- response = requests.get(ENDPOINT, headers=HEADERS, timeout=10)
- response.raise_for_status()
- return pd.read_csv(StringIO(response.text)), response.headers.get("Last-Modified")
+def get_latest_data() -> tuple[pd.DataFrame, str | None]:
+ cache_dir = Path(user_cache_path(appname="fnme", appauthor=False))
+ csv_path = cache_dir / "latest_data.csv"
+ timestamp_path = cache_dir / "timestamp.txt"
+
+ cache_dir.mkdir(parents=True, exist_ok=True)
+
+ remote_last_modified = requests.head(
+ ENDPOINT, headers=HEADERS, timeout=10
+ ).headers.get("Last-Modified")
+
+ cached_last_modified = (
+ timestamp_path.read_text() if timestamp_path.exists() else None
+ )
+
+ if not csv_path.exists() or remote_last_modified != cached_last_modified:
+ response = requests.get(ENDPOINT, headers=HEADERS, timeout=10)
+ response.raise_for_status()
+ last_modified = response.headers.get("Last-Modified")
+ csv_path.write_text(response.text, encoding="utf-8")
+ timestamp_path.write_text(last_modified or "")
+ return pd.read_csv(csv_path), last_modified
+
+ return pd.read_csv(csv_path), cached_last_modified
git.ajschof.me — hosted by ajschofield — powered by cgit