First cut at fixing video api

This commit is contained in:
Kevin Fronczak
2019-01-25 13:51:07 -05:00
parent 5c6e4364e8
commit 559cc4a5ac
5 changed files with 29 additions and 13 deletions
+5 -3
View File
@@ -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)
+1 -1
View File
@@ -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
+5
View File
@@ -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'
+9 -1
View File
@@ -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]
+9 -8
View File
@@ -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: