nupes-elections/nupes/data.py

27 lines
602 B
Python

from pathlib import Path
import requests
DATA_DIR = Path(__file__).parent / 'data'
def get_file(url, filename, etag: str = None):
if not DATA_DIR.is_dir():
DATA_DIR.mkdir()
head_response = requests.head(url, allow_redirects=True)
headers = head_response.headers
if not etag:
etag = headers.get('ETag').split('/')[-1].replace('"', '')
file = DATA_DIR / filename.format(etag=etag)
if file.exists():
return file
response = requests.get(url, allow_redirects=True)
with file.open('wb') as f:
f.write(response.content)
return file