moved to asyncd
This commit is contained in:
@@ -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"))
|
||||
|
||||
Reference in New Issue
Block a user