From 4db7a33ef3cf8b51a3dee177eb249d66767e43d8 Mon Sep 17 00:00:00 2001 From: Kevin Fronczak Date: Sat, 18 May 2019 10:48:58 -0400 Subject: [PATCH] Added changed video download endpoint --- blinkpy/api.py | 4 ++-- blinkpy/blinkpy.py | 35 +++++++++++++++++++++-------------- tests/test_blink_functions.py | 12 ++++++------ 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/blinkpy/api.py b/blinkpy/api.py index 6e41738..77cac8f 100644 --- a/blinkpy/api.py +++ b/blinkpy/api.py @@ -168,8 +168,8 @@ def request_videos(blink, time=None, page=0): :param page: Page number to get videos from. """ timestamp = get_time(time) - url = "{}/api/v2/videos/changed?since={}&page={}".format( - blink.urls.base_url, timestamp, page) + url = "{}/api/v1/accounts/{}/media/changed?since={}&page={}".format( + blink.urls.base_url, blink.account_id, timestamp, page) return http_get(blink, url) diff --git a/blinkpy/blinkpy.py b/blinkpy/blinkpy.py index 9a9b2ae..6518c95 100644 --- a/blinkpy/blinkpy.py +++ b/blinkpy/blinkpy.py @@ -247,7 +247,8 @@ class Blink(): combined = merge_dicts(combined, self.sync[sync].cameras) return combined - def download_videos(self, path, since=None, camera='all', stop=10): + def download_videos(self, path, since=None, + camera='all', stop=10, debug=False): """ Download all videos from server since specified time. @@ -258,6 +259,8 @@ class Blink(): :param camera: Camera name to retrieve. Defaults to "all". Use a list for multiple cameras. :param stop: Page to stop on (~25 items per page. Default page 10). + :param debug: Set to TRUE to prevent downloading of items. + Instead of downloading, entries will be printed to log. """ if since is None: since_epochs = self.last_refresh @@ -275,23 +278,23 @@ class Blink(): response = api.request_videos(self, time=since_epochs, page=page) _LOGGER.debug("Processing page %s", page) try: - result = response['videos'] + result = response['media'] if not result: raise IndexError except (KeyError, IndexError): _LOGGER.info("No videos found on page %s. Exiting.", page) break - self._parse_downloaded_items(result, camera, path) + self._parse_downloaded_items(result, camera, path, debug) - def _parse_downloaded_items(self, result, camera, path): + def _parse_downloaded_items(self, result, camera, path, debug): """Parse downloaded videos.""" for item in result: try: created_at = item['created_at'] - camera_name = item['camera_name'] + camera_name = item['device_name'] is_deleted = item['deleted'] - address = item['address'] + address = item['media'] except KeyError: _LOGGER.info("Missing clip information, skipping...") continue @@ -310,13 +313,17 @@ class Blink(): filename = "{}_{}.mp4".format(camera_name, created_at) filename = os.path.join(path, filename) - if os.path.isfile(filename): - _LOGGER.info("%s already exists, skipping...", filename) - continue + if not debug: + if os.path.isfile(filename): + _LOGGER.info("%s already exists, skipping...", filename) + continue - response = api.http_get(self, url=clip_address, - stream=True, json=False) - with open(filename, 'wb') as vidfile: - copyfileobj(response.raw, vidfile) + response = api.http_get(self, url=clip_address, + stream=True, json=False) + with open(filename, 'wb') as vidfile: + copyfileobj(response.raw, vidfile) - _LOGGER.info("Downloaded video to %s", filename) + _LOGGER.info("Downloaded video to %s", filename) + else: + _LOGGER.info("Camera: %s, Timestamp: %s, Address: %s", + camera_name, created_at, address) diff --git a/tests/test_blink_functions.py b/tests/test_blink_functions.py index 3607a73..3e79195 100644 --- a/tests/test_blink_functions.py +++ b/tests/test_blink_functions.py @@ -121,12 +121,12 @@ class TestBlinkFunctions(unittest.TestCase): blinkpy._LOGGER.setLevel(logging.DEBUG) generic_entry = { 'created_at': '1970', - 'camera_name': 'foo', + 'device_name': 'foo', 'deleted': True, - 'address': '/bar.mp4' + 'media': '/bar.mp4' } result = [generic_entry] - mock_req.return_value = {'videos': result} + mock_req.return_value = {'media': result} blink.last_refresh = 0 formatted_date = get_time(blink.last_refresh) expected_log = [ @@ -147,12 +147,12 @@ class TestBlinkFunctions(unittest.TestCase): blinkpy._LOGGER.setLevel(logging.DEBUG) generic_entry = { 'created_at': '1970', - 'camera_name': 'foo', + 'device_name': 'foo', 'deleted': True, - 'address': '/bar.mp4' + 'media': '/bar.mp4' } result = [generic_entry] - mock_req.return_value = {'videos': result} + mock_req.return_value = {'media': result} blink.last_refresh = 0 formatted_date = get_time(blink.last_refresh) expected_log = [