mirror of
https://github.com/ep1cman/unifi-protect-backup.git
synced 2026-01-03 09:03:50 +00:00
Use run_command in downloader and uploader
This commit is contained in:
@@ -10,30 +10,28 @@ from pyunifiprotect import ProtectApiClient
|
|||||||
from pyunifiprotect.data.nvr import Event
|
from pyunifiprotect.data.nvr import Event
|
||||||
from pyunifiprotect.data.types import EventType
|
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__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def get_video_length(video: bytes) -> float:
|
async def get_video_length(video: bytes) -> float:
|
||||||
"""Uses ffprobe to get the length of the video file passed in as a byte stream"""
|
"""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 -'
|
returncode, stdout, stderr = await run_command(
|
||||||
proc = await asyncio.create_subprocess_shell(
|
'ffprobe -v quiet -show_streams -select_streams v:0 -of json -', video
|
||||||
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
|
|
||||||
|
|
||||||
json_data = json.loads(stdout.decode())
|
if returncode != 0:
|
||||||
return float(json_data['streams'][0]['duration'])
|
raise SubprocessException(stdout, stderr, returncode)
|
||||||
|
|
||||||
else:
|
json_data = json.loads(stdout)
|
||||||
raise SubprocessException(stdout.decode(), stderr.decode(), proc.returncode)
|
return float(json_data['streams'][0]['duration'])
|
||||||
|
|
||||||
|
|
||||||
class VideoDownloader:
|
class VideoDownloader:
|
||||||
@@ -139,4 +137,3 @@ class VideoDownloader:
|
|||||||
logger.debug(msg)
|
logger.debug(msg)
|
||||||
except SubprocessException as e:
|
except SubprocessException as e:
|
||||||
logger.warn(" `ffprobe` failed")
|
logger.warn(" `ffprobe` failed")
|
||||||
logger.exception(e)
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import aiosqlite
|
|||||||
from pyunifiprotect.data.nvr import Event
|
from pyunifiprotect.data.nvr import Event
|
||||||
from pyunifiprotect import ProtectApiClient
|
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__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -74,19 +74,9 @@ class VideoUploader:
|
|||||||
Raises:
|
Raises:
|
||||||
RuntimeError: If rclone returns a non-zero exit code
|
RuntimeError: If rclone returns a non-zero exit code
|
||||||
"""
|
"""
|
||||||
cmd = f'rclone rcat -vv {rclone_args} "{destination}"'
|
returncode, stdout, stderr = await run_command(f'rclone rcat -vv {rclone_args} "{destination}"', video)
|
||||||
proc = await asyncio.create_subprocess_shell(
|
if returncode != 0:
|
||||||
cmd,
|
logger.warn(f" Failed to upload file: '{destination}'")
|
||||||
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)
|
|
||||||
|
|
||||||
async def _update_database(self, event: Event, destination: str):
|
async def _update_database(self, event: Event, destination: str):
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user