Skip to content

Commit

Permalink
Set on-disk file times to the Last-Modified header returned by GDrive
Browse files Browse the repository at this point in the history
  • Loading branch information
moulins authored and wkentaro committed May 12, 2024
1 parent ca35846 commit cea505a
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions gdown/download.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import email.utils as eutils
import os
import os.path as osp
import re
Expand Down Expand Up @@ -75,6 +76,16 @@ def _get_filename_from_response(response):

return None

def _get_modified_time_from_response(response):
if "Last-Modified" not in response.headers:
return None

raw = response.headers["Last-Modified"]
if raw is None:
return None

return eutils.parsedate_to_datetime(raw)


def _get_session(proxy, use_cookies, user_agent, return_cookies_file=False):
sess = requests.session()
Expand Down Expand Up @@ -266,8 +277,10 @@ def download(
raise FileURLRetrievalError(message)

filename_from_url = None
last_modified_time = None
if gdrive_file_id and is_gdrive_download_link:
filename_from_url = _get_filename_from_response(response=res)
last_modified_time = _get_modified_time_from_response(response=res)
if filename_from_url is None:
filename_from_url = osp.basename(url)

Expand Down Expand Up @@ -360,6 +373,9 @@ def download(
if tmp_file:
f.close()
shutil.move(tmp_file, output)
if output_is_path and last_modified_time:
mtime = last_modified_time.timestamp()
os.utime(output, (mtime, mtime))
finally:
sess.close()

Expand Down

0 comments on commit cea505a

Please sign in to comment.