import httpx import datetime import os from pathlib import Path from logger import get_worker_thread_logger from urllib.parse import urlparse from config import get_settings def get_datestring() -> str: return datetime.datetime.now().strftime("%d.%m.%y (%H:%M:%S)") def cache_cheatsheet(url) -> str | None: settings = get_settings() logger = get_worker_thread_logger() logger.info(f"Caching cheatsheet from {url}") try: with httpx.Client() as client: r = client.get(url, timeout=5.0) if not r.is_success and r.headers.get("Content-Type") != "application/pdf": logger.error(f"Failed to fetch URL: {url} (status code: {r.status_code})") return None except httpx.TimeoutException: logger.error(f"Timeout fetching URL: {url}") return None data = r.content url_base_name = Path(urlparse(url).path).stem filesname = os.path.join(f"{url_base_name}.pdf") if not os.path.exists(os.path.join(settings.paths.cache)): os.mkdir(os.path.join(settings.paths.cache)) if not os.path.exists(os.path.join(settings.paths.output, "cache")): os.mkdir(os.path.join(settings.paths.output, "cache")) with open(os.path.join(settings.paths.output, "cache", filesname), "wb") as f: f.write(data) with open(os.path.join(settings.paths.cache, filesname), "wb") as f: f.write(data) logger.info(f"Saved file to {filesname}") return os.path.join("cache", filesname)