-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathutils.py
37 lines (27 loc) · 1.15 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import os
from datetime import datetime
from email.utils import formatdate, parsedate_to_datetime
import requests
from appdirs import user_cache_dir
def download(url, destination_file):
headers = {}
path = user_cache_dir("promptify")
if not os.path.isdir(path):
os.makedirs(path)
destination_file = os.path.join(path, destination_file)
last_modified = ""
if os.path.exists(destination_file):
mtime = os.path.getmtime(destination_file)
headers["if-modified-since"] = formatdate(mtime, usegmt=True)
response = requests.get(url, headers=headers, stream=True)
response.raise_for_status()
if response.status_code == requests.codes.not_modified:
return
if response.status_code == requests.codes.ok:
with open(destination_file, "wb") as f:
for chunk in response.iter_content(chunk_size=1048576):
f.write(chunk)
if last_modified == response.headers.get("last-modified"):
new_mtime = parsedate_to_datetime(last_modified).timestamp()
os.utime(destination_file, times=(datetime.now().timestamp(), new_mtime))
return destination_file