|
@ -1,5 +1,6 @@ |
|
|
|
|
|
import asyncio |
|
|
from datetime import datetime |
|
|
from datetime import datetime |
|
|
from dvrip import DVRIPCam |
|
|
from asyncio_dvrip import DVRIPCam |
|
|
|
|
|
|
|
|
from nvr_types import File as NvrFile |
|
|
from nvr_types import File as NvrFile |
|
|
from nvr_types import list_local_files |
|
|
from nvr_types import list_local_files |
|
@ -17,47 +18,46 @@ def date_today(begin = True): |
|
|
return datetime.now().strftime("%Y-%m-%d 23:59:59") |
|
|
return datetime.now().strftime("%Y-%m-%d 23:59:59") |
|
|
|
|
|
|
|
|
class NVR: |
|
|
class NVR: |
|
|
def __init__(self, client) -> None: |
|
|
def __init__(self, client, loop) -> None: |
|
|
self.client:DVRIPCam = client |
|
|
self.client:DVRIPCam = client |
|
|
|
|
|
self.loop = loop |
|
|
|
|
|
|
|
|
def login(self): |
|
|
async def login(self): |
|
|
self.client.login() |
|
|
await self.client.login(self.loop) |
|
|
|
|
|
|
|
|
def logout(self): |
|
|
def logout(self): |
|
|
self.client.close() |
|
|
self.client.close() |
|
|
|
|
|
|
|
|
@property |
|
|
async def channels(self): |
|
|
def channels(self): |
|
|
return await self.client.get_command("ChannelTitle", 1048) |
|
|
return self.client.get_channel_titles() |
|
|
|
|
|
|
|
|
|
|
|
def files(self, channel, start = None, end = None, ftype = H264, stype = SECONDARY_STREAM, json = False): |
|
|
async def files(self, channel, start = None, end = None, ftype = H264, stype = SECONDARY_STREAM, json = False): |
|
|
if not start: |
|
|
if not start: |
|
|
start = date_today() |
|
|
start = date_today() |
|
|
if not end: |
|
|
if not end: |
|
|
end = date_today(False) |
|
|
end = date_today(False) |
|
|
print("Search files", start, end) |
|
|
print("Search files", start, end) |
|
|
for raw_file in list_local_files(self.client, startTime=start, endTime=end, filetype=ftype, channel=channel, streamType=stype): |
|
|
for raw_file in await list_local_files(self.client, startTime=start, endTime=end, filetype=ftype, channel=channel, streamType=stype): |
|
|
if json: |
|
|
if json: |
|
|
yield NvrFile(raw_file, channel, stype).json |
|
|
yield NvrFile(raw_file, channel, stype).json |
|
|
else: |
|
|
else: |
|
|
yield NvrFile(raw_file, channel, stype) |
|
|
yield NvrFile(raw_file, channel, stype) |
|
|
|
|
|
|
|
|
def stream_file(self, file: NvrFile): |
|
|
async def stream_file(self, file: NvrFile) -> bytes: |
|
|
return file.generate_bytes(self.client) |
|
|
len_data = await file.generate_first_bytes(self.client) |
|
|
|
|
|
print("len data =",len_data) |
|
|
def save_file(self, file:NvrFile, savePath = "out.unknown"): |
|
|
if (len_data is None): |
|
|
|
|
|
yield b"" |
|
|
|
|
|
else: |
|
|
|
|
|
async for chunk in file.get_file_stream(self.client, len_data): |
|
|
|
|
|
if (chunk == None): |
|
|
|
|
|
break |
|
|
|
|
|
yield chunk |
|
|
|
|
|
|
|
|
|
|
|
async def save_file(self, file:NvrFile, savePath = "out.unknown"): |
|
|
downloaded_bytes = 0 |
|
|
downloaded_bytes = 0 |
|
|
with open(savePath, "wb") as f: |
|
|
with open(savePath, "wb") as f: |
|
|
for byte in file.generate_bytes(self.client): |
|
|
async for byte in file.generate_bytes(self.client): |
|
|
f.write(byte) |
|
|
f.write(byte) |
|
|
downloaded_bytes += len(byte) |
|
|
downloaded_bytes += len(byte) |
|
|
print("\r", downloaded_bytes, "/", file.size) |
|
|
print("\r", downloaded_bytes, "/", file.size) |
|
|
|
|
|
|
|
|
def download_test(self, filename = "testfile.unknown"): |
|
|
|
|
|
download_file = list(self.files(0))[0] |
|
|
|
|
|
downloaded_bytes = 0 |
|
|
|
|
|
#with open(filename, "wb") as f: |
|
|
|
|
|
# for byte in download_file.download_stream(self.client): |
|
|
|
|
|
# downloaded_bytes += len(byte) |
|
|
|
|
|
# f.write(byte) |
|
|
|
|
|
# print("\r", downloaded_bytes, "/", download_file.size) |
|
|
|