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