Finish downloading function

This commit is contained in:
2019-09-28 23:39:50 +01:00
parent e8ffe8cb78
commit 2b4503cc56
2 changed files with 15 additions and 24 deletions

View File

@@ -9,8 +9,7 @@ def get_remote_path(box_item: Item):
if entry.id == '0':
continue
p = p / entry.name
p = p / box_item.name
return str(p)
return p / box_item.name
class Job:

View File

@@ -7,13 +7,6 @@ from boxsdk.object.folder import Folder
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):
"""Return the given bytes as a human friendly KB, MB, GB, or TB string"""
b = float(b)
@@ -46,7 +39,7 @@ class Worker:
while not self.interrupt_flag['exit']:
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')
break
elif isinstance(job.box_item, Folder):
@@ -74,27 +67,26 @@ class Worker:
return None
def do_file(self, job: Job):
print(f'Supposed to download file {job.remote_path} to {job.local_path}')
return None
try:
if os.path.isfile(job.local):
local_size = os.path.getsize(job.local)
if local_size == job.file_size:
logging.info('Skipping file [%s], already exists', job.remote)
if job.local_path.is_file():
local_size = job.local_path.stat().st_size
if local_size == job.box_item.size:
logging.info('Skipping file [%s], already exists', job.remote_path)
return
logging.warning(
'Downloading file [%s], due to different size (%s | %s)',
job.remote,
human_readable_bytes(job.file_size),
job.remote_path,
human_readable_bytes(job.box_item.size),
human_readable_bytes(local_size))
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 != '':
os.makedirs(parent_dir, exist_ok=True)
self.drive.download_file(job)
self.downloaded.add(dir_get_parent(job.remote))
with open(job.local_path, 'wb') as f:
job.box_item.download_to(f)
self.downloaded.add(job.remote_path.parent)
except Exception as e:
logging.error('Fail to Download [%s]: %s: %s', job.remote, type(e).__name__, e)
self.errors.append(job.remote)
logging.error('Fail to Download [%s]: %s: %s', job.remote_path, type(e).__name__, e)
self.errors.append(job.remote_path)