41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
import httpx
|
|
import datetime
|
|
import os
|
|
from pathlib import Path
|
|
from logger import get_worker_thread_logger
|
|
from urllib.parse import urlparse
|
|
|
|
def get_datestring() -> str:
|
|
return datetime.datetime.now().strftime("%d.%m.%y")
|
|
|
|
|
|
def cache_cheatsheet(url, outdir: str) -> str | None:
|
|
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("cache", f"{url_base_name}.pdf")
|
|
|
|
if not os.path.exists(os.path.join(outdir, "cache")):
|
|
os.mkdir(os.path.join(outdir, "cache"))
|
|
|
|
with open(os.path.join(outdir, filesname), "wb") as f:
|
|
f.write(data)
|
|
|
|
logger.info(f"Saved file to {filesname}")
|
|
|
|
return filesname
|