unifi-video-api icon indicating copy to clipboard operation
unifi-video-api copied to clipboard

Accept fileobj in place of filename

Open Diaoul opened this issue 5 years ago • 5 comments

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.

Diaoul avatar Oct 06 '19 13:10 Diaoul

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.)

yuppity avatar Oct 06 '19 18:10 yuppity

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:

Diaoul avatar Oct 06 '19 18:10 Diaoul

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.

Diaoul avatar Oct 07 '19 22:10 Diaoul

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.

yuppity avatar Oct 08 '19 16:10 yuppity

Accepting pathlib.Path objects would be cool.

mrichmon avatar Sep 10 '20 23:09 mrichmon