From b641268d234e08266381745477f5f0713bbafe00 Mon Sep 17 00:00:00 2001 From: MorganBulkeley <45445844+MorganBulkeley@users.noreply.github.com> Date: Wed, 28 Nov 2018 21:23:30 -0700 Subject: [PATCH 1/3] Add support for downloading snapshot from doorbell --- ring_doorbell/__init__.py | 5 +++-- ring_doorbell/const.py | 2 ++ ring_doorbell/doorbot.py | 16 +++++++++++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/ring_doorbell/__init__.py b/ring_doorbell/__init__.py index a05d3d58..3e391364 100644 --- a/ring_doorbell/__init__.py +++ b/ring_doorbell/__init__.py @@ -159,7 +159,8 @@ def query(self, attempts=RETRY_TOKEN, method='GET', raw=False, - extra_params=None): + extra_params=None, + json=None): """Query data from Ring API.""" if self.debug: _LOGGER.debug("Querying %s", url) @@ -189,7 +190,7 @@ def query(self, elif method == 'PUT': req = self.session.put((url), params=urlencode(params)) elif method == 'POST': - req = self.session.post((url), params=urlencode(params)) + req = self.session.post((url), params=urlencode(params), json=json) if self.debug: _LOGGER.debug("_query %s ret %s", loop, req.status_code) diff --git a/ring_doorbell/const.py b/ring_doorbell/const.py index e3e1d2da..ccbba380 100644 --- a/ring_doorbell/const.py +++ b/ring_doorbell/const.py @@ -43,6 +43,8 @@ NEW_SESSION_ENDPOINT = '/clients_api/session' RINGTONES_ENDPOINT = '/ringtones' SIREN_ENDPOINT = DOORBELLS_ENDPOINT + '/siren_{1}' +SNAPSHOT_ENDPOINT = "/clients_api/snapshots/image/{0}" +SNAPSHOT_TIMESTAMP_ENDPOINT = "/clients_api/snapshots/timestamps" TESTSOUND_CHIME_ENDPOINT = CHIMES_ENDPOINT + '/play_sound' URL_DOORBELL_HISTORY = DOORBELLS_ENDPOINT + '/history' URL_RECORDING = '/clients_api/dings/{0}/recording' diff --git a/ring_doorbell/doorbot.py b/ring_doorbell/doorbot.py index 699f8007..15531ef7 100644 --- a/ring_doorbell/doorbot.py +++ b/ring_doorbell/doorbot.py @@ -5,6 +5,7 @@ from datetime import datetime import os import pytz +import time from ring_doorbell.generic import RingGeneric @@ -14,7 +15,7 @@ API_URI, DOORBELLS_ENDPOINT, DOORBELL_VOL_MIN, DOORBELL_VOL_MAX, DOORBELL_EXISTING_TYPE, DINGS_ENDPOINT, FILE_EXISTS, LIVE_STREAMING_ENDPOINT, MSG_BOOLEAN_REQUIRED, MSG_EXISTING_TYPE, - MSG_VOL_OUTBOUND, URL_DOORBELL_HISTORY, URL_RECORDING) + MSG_VOL_OUTBOUND, SNAPSHOT_ENDPOINT, SNAPSHOT_TIMESTAMP_ENDPOINT, URL_DOORBELL_HISTORY, URL_RECORDING) _LOGGER = logging.getLogger(__name__) @@ -342,3 +343,16 @@ def volume(self, value): def connection_status(self): """Return connection status.""" return self._attrs.get('alerts').get('connection') + + def get_snapshot(self, retries=3, delay=2): + """Take a snapshot and download it""" + url = API_URI + SNAPSHOT_TIMESTAMP_ENDPOINT + payload = {"doorbot_ids": [self._attrs.get('id')]} + self._ring.query(url, json=payload) + request_time = time.time() + for _ in range(3): + time.sleep(delay) + response = self._ring.query(url, method="POST", json=payload, raw=1).json() + if response["timestamps"][0]["timestamp"] / 100 > request_time: + return self._ring.query(API_URI + SNAPSHOT_ENDPOINT.format(self._attrs.get('id')), raw=True).content + return False From 5716393133cbde735f93e9a5985a72c1ca24234f Mon Sep 17 00:00:00 2001 From: MorganBulkeley <45445844+MorganBulkeley@users.noreply.github.com> Date: Wed, 5 Dec 2018 21:35:41 -0700 Subject: [PATCH 2/3] Bug fixes and optimizations --- ring_doorbell/doorbot.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ring_doorbell/doorbot.py b/ring_doorbell/doorbot.py index 15531ef7..80e17d3c 100644 --- a/ring_doorbell/doorbot.py +++ b/ring_doorbell/doorbot.py @@ -344,15 +344,15 @@ def connection_status(self): """Return connection status.""" return self._attrs.get('alerts').get('connection') - def get_snapshot(self, retries=3, delay=2): + def get_snapshot(self, retries=3, delay=1): """Take a snapshot and download it""" url = API_URI + SNAPSHOT_TIMESTAMP_ENDPOINT payload = {"doorbot_ids": [self._attrs.get('id')]} self._ring.query(url, json=payload) request_time = time.time() - for _ in range(3): + for _ in range(retries): time.sleep(delay) response = self._ring.query(url, method="POST", json=payload, raw=1).json() - if response["timestamps"][0]["timestamp"] / 100 > request_time: + if response["timestamps"][0]["timestamp"] / 1000 > request_time: return self._ring.query(API_URI + SNAPSHOT_ENDPOINT.format(self._attrs.get('id')), raw=True).content return False From 2ecd6e3edabece33f1b14c43ba81615e886202cf Mon Sep 17 00:00:00 2001 From: MorganBulkeley <45445844+MorganBulkeley@users.noreply.github.com> Date: Thu, 6 Dec 2018 16:15:52 -0700 Subject: [PATCH 3/3] Fix line length --- ring_doorbell/doorbot.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/ring_doorbell/doorbot.py b/ring_doorbell/doorbot.py index 80e17d3c..81695793 100644 --- a/ring_doorbell/doorbot.py +++ b/ring_doorbell/doorbot.py @@ -15,7 +15,8 @@ API_URI, DOORBELLS_ENDPOINT, DOORBELL_VOL_MIN, DOORBELL_VOL_MAX, DOORBELL_EXISTING_TYPE, DINGS_ENDPOINT, FILE_EXISTS, LIVE_STREAMING_ENDPOINT, MSG_BOOLEAN_REQUIRED, MSG_EXISTING_TYPE, - MSG_VOL_OUTBOUND, SNAPSHOT_ENDPOINT, SNAPSHOT_TIMESTAMP_ENDPOINT, URL_DOORBELL_HISTORY, URL_RECORDING) + MSG_VOL_OUTBOUND, SNAPSHOT_ENDPOINT, SNAPSHOT_TIMESTAMP_ENDPOINT, + URL_DOORBELL_HISTORY, URL_RECORDING) _LOGGER = logging.getLogger(__name__) @@ -352,7 +353,9 @@ def get_snapshot(self, retries=3, delay=1): request_time = time.time() for _ in range(retries): time.sleep(delay) - response = self._ring.query(url, method="POST", json=payload, raw=1).json() + response = self._ring.query(url, method="POST", json=payload, + raw=1).json() if response["timestamps"][0]["timestamp"] / 1000 > request_time: - return self._ring.query(API_URI + SNAPSHOT_ENDPOINT.format(self._attrs.get('id')), raw=True).content + return self._ring.query(API_URI + SNAPSHOT_ENDPOINT.format( + self._attrs.get('id')), raw=True).content return False