nupes-elections/nupes/cache.py

47 lines
1.1 KiB
Python
Raw Normal View History

from pathlib import Path
import requests
2024-06-08 21:06:41 +00:00
CACHE_DIR = Path(__file__).parent / 'cache'
def get_file(url, filename, force_etag: str = ""):
2024-06-08 21:06:41 +00:00
if not CACHE_DIR.is_dir():
CACHE_DIR.mkdir(parents=True)
2024-06-08 21:06:41 +00:00
file_dir = CACHE_DIR / filename
if not file_dir.is_dir():
file_dir.mkdir(parents=True)
symlink_file = file_dir / 'latest'
etag = ""
if symlink_file.exists():
file = symlink_file.resolve()
etag = file.name
response = requests.get(url, headers={"If-None-Match": f'"{etag}'}, allow_redirects=True)
if response.status_code == 404:
print(url)
return None
response.raise_for_status()
if response.status_code != 304:
if force_etag:
etag = force_etag
else:
etag = response.headers.get('ETag').split('/')[-1].replace('"', '')
if symlink_file.exists():
symlink_file.resolve().unlink()
file = file_dir / etag
with file.open('wb') as f:
f.write(response.content)
symlink_file.unlink(missing_ok=True)
symlink_file.symlink_to(file)
return symlink_file