47 lines
1.1 KiB
Python
47 lines
1.1 KiB
Python
from pathlib import Path
|
|
|
|
import requests
|
|
|
|
|
|
CACHE_DIR = Path(__file__).parent / 'cache'
|
|
|
|
|
|
def get_file(url, filename, force_etag: str = ""):
|
|
if not CACHE_DIR.is_dir():
|
|
CACHE_DIR.mkdir(parents=True)
|
|
|
|
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
|