Python requests: download only if newer
I used the following code, which also takes the timezone into account and makes sure both datetime objects are aware.
import datetime
import requests
from dateutil.parser import parse as parsedate
r = requests.head(url)
url_datetime = parsedate(r.headers['Last-Modified']).astimezone()
file_time = datetime.datetime.fromtimestamp(path.getmtime(dst_file)).astimezone()
if(url_date > file_time):
user_agent = {"User-agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0"}
r = requests.get(url, headers=user_agent)
with open(file, 'wb') as fd:
for chunk in r.iter_content(4096):
fd.write(chunk)
import requests
import datetime
from dateutil.parser import parse as parsedate
r = requests.head(url)
url_time = r.headers['last-modified']
url_date = parsedate(url_time)
file_time = datetime.datetime.fromtimestamp(os.path.getmtime(dstFile))
if url_date > file_time :
download it !