Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: preserve GDrive's last modified time when downloading #351

Merged
merged 2 commits into from
May 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions gdown/download.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import email.utils
import os
import os.path as osp
import re
Expand Down Expand Up @@ -76,6 +77,17 @@ 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 email.utils.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 +278,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 +374,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
Loading