import json as json_module import os 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 def json(url: str): # pyright: ignore[reportAny] raw_text = text(url) return json_module.loads(raw_text) # pyright: ignore[reportAny]