Skip to content

Commit ff306b8

Browse files
authored
Merge pull request #439 from fronzbot/dev
0.17.1
2 parents 1ced64d + 9d9d3aa commit ff306b8

File tree

6 files changed

+47
-10
lines changed

6 files changed

+47
-10
lines changed

CHANGES.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ Changelog
44

55
A list of changes between each release
66

7+
0.17.1 (2021-02-18)
8+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9+
10+
- Add delay parameter to Blink.download_videos method in order to throttle API during video retrieval (`#437 <https://github.com/fronzbot/blinkpy/pull/437>`__)
11+
- Bump pylint to 2.6.2
12+
13+
714
0.17.0 (2021-02-15)
815
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
916

README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,13 @@ Similar methods exist for individual cameras:
174174
175175
Download videos
176176
----------------
177-
You can also use this library to download all videos from the server. In order to do this, you must specify a ``path``. You may also specifiy a how far back in time to go to retrieve videos via the ``since=`` variable (a simple string such as ``"2017/09/21"`` is sufficient), as well as how many pages to traverse via the ``stop=`` variable. Note that by default, the library will search the first ten pages which is sufficient in most use cases. Additionally, you can specify one or more cameras via the ``camera=`` property. This can be a single string indicating the name of the camera, or a list of camera names. By default, it is set to the string ``'all'`` to grab videos from all cameras.
177+
You can also use this library to download all videos from the server. In order to do this, you must specify a ``path``. You may also specifiy a how far back in time to go to retrieve videos via the ``since=`` variable (a simple string such as ``"2017/09/21"`` is sufficient), as well as how many pages to traverse via the ``stop=`` variable. Note that by default, the library will search the first ten pages which is sufficient in most use cases. Additionally, you can specify one or more cameras via the ``camera=`` property. This can be a single string indicating the name of the camera, or a list of camera names. By default, it is set to the string ``'all'`` to grab videos from all cameras. If you are downloading many items, setting the ``delay`` parameter is advised in order to throttle sequential calls to the API. By default this is set to ``1`` but can be any integer representing the number of seconds to delay between calls.
178178

179-
Example usage, which downloads all videos recorded since July 4th, 2018 at 9:34am to the ``/home/blink`` directory:
179+
Example usage, which downloads all videos recorded since July 4th, 2018 at 9:34am to the ``/home/blink`` directory with a 2s delay between calls:
180180

181181
.. code:: python
182182
183-
blink.download_videos('/home/blink', since='2018/07/04 09:34')
183+
blink.download_videos('/home/blink', since='2018/07/04 09:34', delay=2)
184184
185185
186186
.. |Build Status| image:: https://github.com/fronzbot/blinkpy/workflows/build/badge.svg

blinkpy/blinkpy.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,9 @@ def save(self, file_name):
264264
"""Save login data to file."""
265265
util.json_save(self.auth.login_attributes, file_name)
266266

267-
def download_videos(self, path, since=None, camera="all", stop=10, debug=False):
267+
def download_videos(
268+
self, path, since=None, camera="all", stop=10, delay=1, debug=False
269+
):
268270
"""
269271
Download all videos from server since specified time.
270272
@@ -275,6 +277,7 @@ def download_videos(self, path, since=None, camera="all", stop=10, debug=False):
275277
:param camera: Camera name to retrieve. Defaults to "all".
276278
Use a list for multiple cameras.
277279
:param stop: Page to stop on (~25 items per page. Default page 10).
280+
:param delay: Number of seconds to wait in between subsequent video downloads.
278281
:param debug: Set to TRUE to prevent downloading of items.
279282
Instead of downloading, entries will be printed to log.
280283
"""
@@ -301,9 +304,9 @@ def download_videos(self, path, since=None, camera="all", stop=10, debug=False):
301304
_LOGGER.info("No videos found on page %s. Exiting.", page)
302305
break
303306

304-
self._parse_downloaded_items(result, camera, path, debug)
307+
self._parse_downloaded_items(result, camera, path, delay, debug)
305308

306-
def _parse_downloaded_items(self, result, camera, path, debug):
309+
def _parse_downloaded_items(self, result, camera, path, delay, debug):
307310
"""Parse downloaded videos."""
308311
for item in result:
309312
try:
@@ -351,6 +354,8 @@ def _parse_downloaded_items(self, result, camera, path, debug):
351354
"Address: {address}, Filename: {filename}"
352355
)
353356
)
357+
if delay > 0:
358+
time.sleep(delay)
354359

355360

356361
class BlinkSetupError(Exception):

blinkpy/helpers/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
MAJOR_VERSION = 0
66
MINOR_VERSION = 17
7-
PATCH_VERSION = 0
7+
PATCH_VERSION = 1
88

99
__version__ = f"{MAJOR_VERSION}.{MINOR_VERSION}.{PATCH_VERSION}"
1010

requirements_test.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ coverage==5.4
33
flake8==3.8.4
44
flake8-docstrings==1.5.0
55
pre-commit==2.10.1
6-
pylint==2.6.0
6+
pylint==2.6.2
77
pydocstyle==5.1.1
88
pytest==6.2.2
99
pytest-cov==2.11.1

tests/test_blink_functions.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import unittest
33
from unittest import mock
44
import logging
5+
import time
56

67
from blinkpy import blinkpy
78
from blinkpy.sync_module import BlinkSyncModule
@@ -88,9 +89,33 @@ def test_parse_downloaded_items(self, mock_req):
8889
"DEBUG:blinkpy.blinkpy:foo: /bar.mp4 is marked as deleted.",
8990
]
9091
with self.assertLogs() as dl_log:
91-
blink.download_videos("/tmp", stop=2)
92+
blink.download_videos("/tmp", stop=2, delay=0)
9293
self.assertEqual(dl_log.output, expected_log)
9394

95+
@mock.patch("blinkpy.blinkpy.api.request_videos")
96+
def test_parse_downloaded_throttle(self, mock_req):
97+
"""Test ability to parse downloaded items list."""
98+
generic_entry = {
99+
"created_at": "1970",
100+
"device_name": "foo",
101+
"deleted": False,
102+
"media": "/bar.mp4",
103+
}
104+
result = [generic_entry]
105+
mock_req.return_value = {"media": result}
106+
self.blink.last_refresh = 0
107+
start = time.time()
108+
self.blink.download_videos("/tmp", stop=2, delay=0, debug=True)
109+
now = time.time()
110+
delta = now - start
111+
self.assertTrue(delta < 0.1)
112+
113+
start = time.time()
114+
self.blink.download_videos("/tmp", stop=2, delay=0.1, debug=True)
115+
now = time.time()
116+
delta = now - start
117+
self.assertTrue(delta >= 0.1)
118+
94119
@mock.patch("blinkpy.blinkpy.api.request_videos")
95120
def test_parse_camera_not_in_list(self, mock_req):
96121
"""Test ability to parse downloaded items list."""
@@ -113,7 +138,7 @@ def test_parse_camera_not_in_list(self, mock_req):
113138
"DEBUG:blinkpy.blinkpy:Skipping videos for foo.",
114139
]
115140
with self.assertLogs() as dl_log:
116-
blink.download_videos("/tmp", camera="bar", stop=2)
141+
blink.download_videos("/tmp", camera="bar", stop=2, delay=0)
117142
self.assertEqual(dl_log.output, expected_log)
118143

119144
@mock.patch("blinkpy.blinkpy.api.request_network_update")

0 commit comments

Comments
 (0)