feat: cached fetch

This commit is contained in:
2026-09-15 19:49:55 +03:00
parent f5f20fdc35
commit b1e36e86a4
2 changed files with 47 additions and 0 deletions
+45
View File
@@ -0,0 +1,45 @@
import json as json_module
import os
from typing import cast
import requests
CACHE_DIR = ".cached"
DEBUG = True
def _escape_path(url: str) -> str:
if not os.path.exists(CACHE_DIR):
os.makedirs(CACHE_DIR)
path = url
for char in [":", "/", "?", "&", "=", "%", ".", " "]:
path = path.replace(char, "_")
return os.path.join(CACHE_DIR, path[:250])
def text(url: str) -> str:
cache_path = _escape_path(url)
if os.path.exists(cache_path):
with open(cache_path, "r", encoding="utf-8") as f:
if DEBUG:
print(f"From cache: {url}")
return f.read()
response = requests.get(url)
response.raise_for_status()
content = response.text
with open(cache_path, "w", encoding="utf-8") as f:
_ = f.write(content)
if DEBUG:
print(f"Fetch: {url}")
return content
type JVal = dict[str, "JVal"] | list["JVal"] | str | int | float | bool | None
def json(url: str) -> JVal:
raw_text = text(url)
return cast(JVal, json_module.loads(raw_text))