moved to asyncd

This commit is contained in:
alexander
2026-01-23 09:21:42 +01:00
parent efc2116de4
commit e93e743f65
11 changed files with 106 additions and 76 deletions

View File

@@ -1,22 +1,32 @@
import hashlib
import requests
import httpx
import datetime
import os
from pathlib import Path
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:
r = requests.get(url)
if not r.ok and r.headers.get("Content-Type") != "application/pdf":
return None
async def cache_cheatsheet(url, outdir: str) -> str | None:
print("Caching cheatsheet from", url)
try:
async with httpx.AsyncClient() as client:
r = await client.get(url, timeout=5.0)
if not r.is_success and r.headers.get("Content-Type") != "application/pdf":
return None
except httpx.TimeoutException:
print("Timeout fetching URL:", url)
return None
data = r.content
hashdata = hashlib.sha256(data)
url_base_name = Path(urlparse(url).path).stem
filesname = os.path.join("cache", f"{hashdata.hexdigest()}.pdf")
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"))