From 117f74acb837f1c044ae4b54c5b50cfc406b55ab Mon Sep 17 00:00:00 2001 From: jake Date: Wed, 9 Aug 2023 11:30:23 -0700 Subject: [PATCH] resume downloads automatically --- internetarchive/__version__.py | 2 +- internetarchive/files.py | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/internetarchive/__version__.py b/internetarchive/__version__.py index 863d4694..0736c156 100644 --- a/internetarchive/__version__.py +++ b/internetarchive/__version__.py @@ -1 +1 @@ -__version__ = '3.6.0.dev1' +__version__ = '3.6.0.dev2' diff --git a/internetarchive/files.py b/internetarchive/files.py index c8029469..5dc3e55a 100644 --- a/internetarchive/files.py +++ b/internetarchive/files.py @@ -204,6 +204,7 @@ def download(self, file_path=None, verbose=None, ignore_existing=None, no_change_timestamp = no_change_timestamp or False params = params or None timeout = 12 if not timeout else timeout + headers = {} self.item.session.mount_http_adapter(max_retries=retries) file_path = file_path or self.name @@ -244,6 +245,8 @@ def download(self, file_path=None, verbose=None, ignore_existing=None, if verbose: print(f' {msg}', file=sys.stderr) return + else: + headers = {"Range": f"bytes={st.st_size}-{self.size}"} parent_dir = os.path.dirname(file_path) try: @@ -254,7 +257,8 @@ def download(self, file_path=None, verbose=None, ignore_existing=None, stream=True, timeout=timeout, auth=self.auth, - params=params) + params=params, + headers=headers) response.raise_for_status() if return_responses: return response @@ -274,7 +278,10 @@ def download(self, file_path=None, verbose=None, ignore_existing=None, if stdout: fileobj = os.fdopen(sys.stdout.fileno(), "wb", closefd=False) if not fileobj: - fileobj = open(file_path.encode('utf-8'), 'wb') + if "Range" in headers: + fileobj = open(file_path.encode('utf-8'), 'ab') + else: + fileobj = open(file_path.encode('utf-8'), 'wb') with fileobj, progress_bar as bar: for chunk in response.iter_content(chunk_size=chunk_size):