diff --git a/blinkpy/api.py b/blinkpy/api.py index f9db8df..723c069 100644 --- a/blinkpy/api.py +++ b/blinkpy/api.py @@ -3,7 +3,7 @@ import logging from json import dumps import blinkpy.helpers.errors as ERROR -from blinkpy.helpers.util import http_req, BlinkException +from blinkpy.helpers.util import http_req, get_time, BlinkException from blinkpy.helpers.constants import DEFAULT_URL _LOGGER = logging.getLogger(__name__) @@ -96,9 +96,11 @@ def request_video_count(blink, headers): return http_get(blink, url) -def request_videos(blink, page=0): +def request_videos(blink, time=None, page=0): """Perform a request for videos.""" - url = "{}/api/v2/videos/page/{}".format(blink.urls.base_url, page) + timestamp = get_time(time) + url = "{}/api/v2/videos/changed?since={}&page/{}".format( + blink.urls.base_url, timestamp, page) return http_get(blink, url) diff --git a/blinkpy/blinkpy.py b/blinkpy/blinkpy.py index 7285465..a9ab54c 100644 --- a/blinkpy/blinkpy.py +++ b/blinkpy/blinkpy.py @@ -166,6 +166,7 @@ class Blink(): for sync_name, sync_module in self.sync.items(): _LOGGER.debug("Attempting refresh of sync %s", sync_name) sync_module.refresh(force_cache=force_cache) + self.last_refresh = int(time.time()) def check_if_ok_to_update(self): """Check if it is ok to perform an http request.""" @@ -174,7 +175,6 @@ class Blink(): if last_refresh is None: last_refresh = 0 if current_time >= (last_refresh + self.refresh_rate): - self.last_refresh = current_time return True return False diff --git a/blinkpy/helpers/constants.py b/blinkpy/helpers/constants.py index c48ea3f..ce319f6 100644 --- a/blinkpy/helpers/constants.py +++ b/blinkpy/helpers/constants.py @@ -54,3 +54,8 @@ LOGIN_BACKUP_URL = "https://{}.{}/login".format('rest.piri', BLINK_URL) Dictionaries ''' ONLINE = {'online': True, 'offline': False} + +''' +OTHER +''' +TIMESTAMP_FORMAT = '%Y-%m-%dT%H:%M:%S%Z' diff --git a/blinkpy/helpers/util.py b/blinkpy/helpers/util.py index 4bcc497..6e14cc5 100644 --- a/blinkpy/helpers/util.py +++ b/blinkpy/helpers/util.py @@ -1,14 +1,22 @@ """Useful functions for blinkpy.""" import logging +import time from requests import Request, Session, exceptions -from blinkpy.helpers.constants import BLINK_URL +from blinkpy.helpers.constants import BLINK_URL, TIMESTAMP_FORMAT import blinkpy.helpers.errors as ERROR _LOGGER = logging.getLogger(__name__) +def get_time(time_to_convert=None): + """Create blink-compatible timestamp.""" + if time_to_convert is None: + time_to_convert = time.time() + return time.strftime(TIMESTAMP_FORMAT, time.localtime(time_to_convert)) + + def merge_dicts(dict_a, dict_b): """Merge two dictionaries into one.""" duplicates = [val for val in dict_a if val in dict_b] diff --git a/blinkpy/sync_module.py b/blinkpy/sync_module.py index 75c02c1..4e123f7 100644 --- a/blinkpy/sync_module.py +++ b/blinkpy/sync_module.py @@ -124,19 +124,21 @@ class BlinkSyncModule(): name = camera_config['name'] self.cameras[name].update(camera_config, force_cache=force_cache) - def get_videos(self, start_page=0, end_page=1): + def get_videos(self, start_page=0, end_page=0): """ Retrieve last recorded videos per camera. :param start_page: Page to start reading from on blink servers (defaults to 0) - :param end_page: Page to stop reading from (defaults to 1) + :param end_page: Page to stop reading from (defaults to 0) """ videos = list() all_dates = dict() for page_num in range(start_page, end_page + 1): - this_page = api.request_videos(self.blink, page=page_num) + this_page = api.request_videos(self.blink, + time=self.blink.last_refresh, + page=page_num) if not this_page: break elif 'message' in this_page: @@ -148,18 +150,17 @@ class BlinkSyncModule(): _LOGGER.debug("Getting videos from page %s through %s", start_page, end_page) + for page in videos: - for entry in page: + for entry in page['videos']: try: camera_name = entry['camera_name'] clip_addr = entry['address'] thumb_addr = entry['thumbnail'] except TypeError: - _LOGGER.warning("Could not extract video information.") + _LOGGER.info("No videos since last refresh.") break - clip_date = clip_addr.split('_')[-6:] - clip_date = '_'.join(clip_date) - clip_date = clip_date.split('.')[0] + clip_date = entry['created_at'] try: self.all_clips[camera_name][clip_date] = clip_addr except KeyError: