2024-06-08 11:08:22 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
|
2024-06-09 15:51:24 +00:00
|
|
|
CACHE_DIR = Path(__file__).parent.parent / 'cache'
|
2024-06-08 11:08:22 +00:00
|
|
|
|
|
|
|
|
2024-06-08 17:38:59 +00:00
|
|
|
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 11:08:22 +00:00
|
|
|
|
2024-06-08 21:06:41 +00:00
|
|
|
file_dir = CACHE_DIR / filename
|
2024-06-08 17:38:59 +00:00
|
|
|
if not file_dir.is_dir():
|
|
|
|
file_dir.mkdir(parents=True)
|
2024-06-08 11:08:22 +00:00
|
|
|
|
2024-06-08 17:38:59 +00:00
|
|
|
symlink_file = file_dir / 'latest'
|
2024-06-08 11:08:22 +00:00
|
|
|
|
2024-06-08 17:38:59 +00:00
|
|
|
etag = ""
|
|
|
|
if symlink_file.exists():
|
|
|
|
file = symlink_file.resolve()
|
|
|
|
etag = file.name
|
2024-06-08 11:08:22 +00:00
|
|
|
|
2024-06-08 17:38:59 +00:00
|
|
|
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
|