Finish downloading function
This commit is contained in:
@@ -9,8 +9,7 @@ def get_remote_path(box_item: Item):
|
|||||||
if entry.id == '0':
|
if entry.id == '0':
|
||||||
continue
|
continue
|
||||||
p = p / entry.name
|
p = p / entry.name
|
||||||
p = p / box_item.name
|
return p / box_item.name
|
||||||
return str(p)
|
|
||||||
|
|
||||||
|
|
||||||
class Job:
|
class Job:
|
||||||
|
|||||||
@@ -7,13 +7,6 @@ from boxsdk.object.folder import Folder
|
|||||||
from .job import Job
|
from .job import Job
|
||||||
|
|
||||||
|
|
||||||
def dir_get_parent(d):
|
|
||||||
slash = d.rstrip('/').rfind('/')
|
|
||||||
if slash > 0:
|
|
||||||
return d[0:slash]
|
|
||||||
return ''
|
|
||||||
|
|
||||||
|
|
||||||
def human_readable_bytes(b):
|
def human_readable_bytes(b):
|
||||||
"""Return the given bytes as a human friendly KB, MB, GB, or TB string"""
|
"""Return the given bytes as a human friendly KB, MB, GB, or TB string"""
|
||||||
b = float(b)
|
b = float(b)
|
||||||
@@ -46,7 +39,7 @@ class Worker:
|
|||||||
while not self.interrupt_flag['exit']:
|
while not self.interrupt_flag['exit']:
|
||||||
job = self.queue.get()
|
job = self.queue.get()
|
||||||
|
|
||||||
if (job is None) or (any(fnmatch(job.remote_path, x) for x in self.blacklist)):
|
if (job is None) or (any(fnmatch(str(job.remote_path), x) for x in self.blacklist)):
|
||||||
logging.info('A worker thread exited')
|
logging.info('A worker thread exited')
|
||||||
break
|
break
|
||||||
elif isinstance(job.box_item, Folder):
|
elif isinstance(job.box_item, Folder):
|
||||||
@@ -74,27 +67,26 @@ class Worker:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
def do_file(self, job: Job):
|
def do_file(self, job: Job):
|
||||||
print(f'Supposed to download file {job.remote_path} to {job.local_path}')
|
|
||||||
return None
|
|
||||||
try:
|
try:
|
||||||
if os.path.isfile(job.local):
|
if job.local_path.is_file():
|
||||||
local_size = os.path.getsize(job.local)
|
local_size = job.local_path.stat().st_size
|
||||||
if local_size == job.file_size:
|
if local_size == job.box_item.size:
|
||||||
logging.info('Skipping file [%s], already exists', job.remote)
|
logging.info('Skipping file [%s], already exists', job.remote_path)
|
||||||
return
|
return
|
||||||
logging.warning(
|
logging.warning(
|
||||||
'Downloading file [%s], due to different size (%s | %s)',
|
'Downloading file [%s], due to different size (%s | %s)',
|
||||||
job.remote,
|
job.remote_path,
|
||||||
human_readable_bytes(job.file_size),
|
human_readable_bytes(job.box_item.size),
|
||||||
human_readable_bytes(local_size))
|
human_readable_bytes(local_size))
|
||||||
else:
|
else:
|
||||||
logging.warning('Downloading file [%s]', job.remote)
|
logging.warning('Downloading file [%s]', job.remote_path)
|
||||||
|
|
||||||
parent_dir = dir_get_parent(job.local)
|
parent_dir = job.local_path.parent
|
||||||
if parent_dir != '':
|
if parent_dir != '':
|
||||||
os.makedirs(parent_dir, exist_ok=True)
|
os.makedirs(parent_dir, exist_ok=True)
|
||||||
self.drive.download_file(job)
|
with open(job.local_path, 'wb') as f:
|
||||||
self.downloaded.add(dir_get_parent(job.remote))
|
job.box_item.download_to(f)
|
||||||
|
self.downloaded.add(job.remote_path.parent)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error('Fail to Download [%s]: %s: %s', job.remote, type(e).__name__, e)
|
logging.error('Fail to Download [%s]: %s: %s', job.remote_path, type(e).__name__, e)
|
||||||
self.errors.append(job.remote)
|
self.errors.append(job.remote_path)
|
||||||
|
|||||||
Reference in New Issue
Block a user