Init
This commit is contained in:
29
src/sources/util.py
Normal file
29
src/sources/util.py
Normal file
@@ -0,0 +1,29 @@
|
||||
import hashlib
|
||||
import requests
|
||||
import datetime
|
||||
import os
|
||||
|
||||
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
|
||||
|
||||
data = r.content
|
||||
|
||||
hashdata = hashlib.sha256(data)
|
||||
|
||||
filesname = os.path.join("cache", f"{hashdata.hexdigest()}.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)
|
||||
|
||||
print("Saved file to", filesname)
|
||||
|
||||
return filesname
|
||||
Reference in New Issue
Block a user