blob: 11497ce2a47ccf87aa5a1c66a0fcaa5df520e9af (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
from pathlib import Path
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, 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
|