From 00f1b675b71dd46b9fb9ec12c6b9a28627a1fe2d Mon Sep 17 00:00:00 2001 From: Sebastian Goscik Date: Sat, 19 Feb 2022 21:43:33 +0000 Subject: [PATCH] Refactor upload into its own method --- unifi_protect_backup/unifi_protect_backup.py | 48 ++++++++++++-------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/unifi_protect_backup/unifi_protect_backup.py b/unifi_protect_backup/unifi_protect_backup.py index d3313c7..bc29de4 100644 --- a/unifi_protect_backup/unifi_protect_backup.py +++ b/unifi_protect_backup/unifi_protect_backup.py @@ -257,28 +257,36 @@ class UnifiProtectBackup: logger.exception(e) continue - logger.debug(f"\tSize: {human_readable_size(len(video))}") - - # Upload video - logger.debug("\tUploading video via rclone...") - cmd = f"rclone rcat -vv '{destination}'" - proc = await asyncio.create_subprocess_shell( - cmd, - stdin=asyncio.subprocess.PIPE, - stdout=asyncio.subprocess.PIPE, - stderr=asyncio.subprocess.PIPE, - ) - stdout, stderr = await proc.communicate(video) - if proc.returncode == 0: - logger.extra_debug(f"stdout:\n{stdout.decode()}") # type: ignore - logger.extra_debug(f"stderr:\n{stderr.decode()}") # type: ignore - else: - logger.warn("Failed to download video") - logger.warn(f"stdout:\n{stdout.decode()}") - logger.warn(f"stderr:\n{stderr.decode()}") + try: + await self._upload_video(video, destination) + except RuntimeError: continue - logger.info("Backed up successfully!") + async def _upload_video(self, video: bytes, destination: pathlib.Path): + """ """ + + logger.debug(" Uploading video via rclone...") + logger.debug(f" To: {destination}") + logger.debug(f" Size: {human_readable_size(len(video))}") + + cmd = f"rclone rcat -vv '{destination}'" + proc = await asyncio.create_subprocess_shell( + cmd, + stdin=asyncio.subprocess.PIPE, + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + stdout, stderr = await proc.communicate(video) + if proc.returncode == 0: + logger.extra_debug(f"stdout:\n{stdout.decode()}") # type: ignore + logger.extra_debug(f"stderr:\n{stderr.decode()}") # type: ignore + else: + logger.warn("Failed to download video") + logger.warn(f"stdout:\n{stdout.decode()}") + logger.warn(f"stderr:\n{stderr.decode()}") + raise RuntimeError() + + logger.info("Backed up successfully!") def generate_file_path(self, event): path = pathlib.Path(self.rclone_destination)