unifi-video-api
unifi-video-api copied to clipboard
Accept fileobj in place of filename
In case users don't want to save stuff on the filesystem, the library should accept writable file objects as input and save to them using write
method.
Basically, anywhere you accept filename (as str
), you could accept a fileobj.
Seems reasonable enough, I'll put in some time on this after I've sorted out the PyPI stuff.
This might not be immediately apparent, and this seems like poor design now that think about it, but the existing code should allow for a slight variation of what you're thinking of:
# Provide the filename yourself (as per the README)
uva.get_camera('Garage').snapshot('garage_snapshot.jpg')
# Get the raw response body
uva.get_camera('Garage').snapshot(True)
With the second one, you can do
file_like_object.write(uva.get_gamera('Garage').snapshot(True))
It does store the whole thing in RAM first though.
All download methods, i.e,
-
UnifiVideoCamera.snapshot()
, -
UnifiVideoCamera.recording_between()
, -
UnifiVideoRecording.download()
, - and
UnifiVideoRecording.snapshot()
should return the raw response body if you pass True
as the filename
arg.
(I keep saying should because there's no test for it and I can't verify against my own UniFi Video setup atm.)
That's right, I've read somewhere in the code that filename
is passed down to raw
and this accepts a bool
too.
I'll give this a shot as soon as I can start working on the component :smile:
To my biggest surprise this happens to be exactly what Home Assistant expected as output (raw bytes of the picture of the camera).
Hence I'm fine using this for now (not played with recording manipulation yet). You can close this if you want.
Glad to hear it. It might still make sense to implement this in case anyone wants to do this with a large recording, since you can't really chunk the response as it is atm. I'll keep this open till I've had time to mull it over.
Accepting pathlib.Path objects would be cool.