N4S4 / synology-api

A Python wrapper around Synology API
MIT License
360 stars 137 forks source link

add explicit parameter to handle recordings download with "video/mp4" mime type #161

Closed fabtesta closed 5 months ago

fabtesta commented 5 months ago

Added the already available "response_json=False" parameter to avoid json unwrapping response for "SYNO.SurveillanceStation.Recording" API call.

fabtesta commented 5 months ago

Sample code to test the change:

recording_export_response = ss.download_recordings(id=event_id)
recording_export_file = __save_recording_to_file__(download_response=recording_export_response, 
                     download_dir=download_dir, 
                     event_id=event_id)
def __save_recording_to_file__(download_response: dict[str, object], download_dir: str, event_id: str) -> str:
    outfile_gif = '{}/{}.mp4'.format(download_dir, event_id)

    with open(outfile_gif, "wb") as f:
        logging.info('Downloading video for event id %i to %s .....', event_id, outfile_gif)
        logging.info('download_response status_code %s', download_response.status_code)

        if download_response.ok:
            total_length = download_response.headers.get('content-length')

            if total_length is None:  # no content length header
                f.write(download_response.content)
            else:
                dl = 0
                total_length = int(total_length)
                for data in download_response.iter_content(chunk_size=4096):
                    dl += len(data)
                    f.write(data)
                    done = int(50 * dl / total_length)
                    sys.stdout.write("\r[%s%s]" % ('=' * done, ' ' * (50 - done)))
                    sys.stdout.flush()
            logging.info('Downloading video for event id %i to %s .....DONE', event_id, outfile_gif)
            return outfile_gif

    return ''