diff --git a/unifi_protect_backup/downloader.py b/unifi_protect_backup/downloader.py index 3a43385..ed74f6e 100644 --- a/unifi_protect_backup/downloader.py +++ b/unifi_protect_backup/downloader.py @@ -10,30 +10,28 @@ from pyunifiprotect import ProtectApiClient from pyunifiprotect.data.nvr import Event from pyunifiprotect.data.types import EventType -from unifi_protect_backup.utils import SubprocessException, VideoQueue, get_camera_name, human_readable_size +from unifi_protect_backup.utils import ( + SubprocessException, + VideoQueue, + get_camera_name, + human_readable_size, + run_command, +) logger = logging.getLogger(__name__) async def get_video_length(video: bytes) -> float: """Uses ffprobe to get the length of the video file passed in as a byte stream""" - cmd = 'ffprobe -v quiet -show_streams -select_streams v:0 -of json -' - proc = await asyncio.create_subprocess_shell( - cmd, - stdin=asyncio.subprocess.PIPE, - stdout=asyncio.subprocess.PIPE, - stderr=asyncio.subprocess.PIPE, + returncode, stdout, stderr = await run_command( + 'ffprobe -v quiet -show_streams -select_streams v:0 -of json -', video ) - 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 - json_data = json.loads(stdout.decode()) - return float(json_data['streams'][0]['duration']) + if returncode != 0: + raise SubprocessException(stdout, stderr, returncode) - else: - raise SubprocessException(stdout.decode(), stderr.decode(), proc.returncode) + json_data = json.loads(stdout) + return float(json_data['streams'][0]['duration']) class VideoDownloader: @@ -139,4 +137,3 @@ class VideoDownloader: logger.debug(msg) except SubprocessException as e: logger.warn(" `ffprobe` failed") - logger.exception(e) diff --git a/unifi_protect_backup/uploader.py b/unifi_protect_backup/uploader.py index 87af349..033a243 100644 --- a/unifi_protect_backup/uploader.py +++ b/unifi_protect_backup/uploader.py @@ -8,7 +8,7 @@ import aiosqlite from pyunifiprotect.data.nvr import Event from pyunifiprotect import ProtectApiClient -from unifi_protect_backup.utils import get_camera_name, SubprocessException, VideoQueue +from unifi_protect_backup.utils import get_camera_name, SubprocessException, VideoQueue, run_command logger = logging.getLogger(__name__) @@ -74,19 +74,9 @@ class VideoUploader: Raises: RuntimeError: If rclone returns a non-zero exit code """ - cmd = f'rclone rcat -vv {rclone_args} "{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: - raise SubprocessException(stdout.decode(), stderr.decode(), proc.returncode) + returncode, stdout, stderr = await run_command(f'rclone rcat -vv {rclone_args} "{destination}"', video) + if returncode != 0: + logger.warn(f" Failed to upload file: '{destination}'") async def _update_database(self, event: Event, destination: str): """