5
5
blinkpy by Kevin Fronczak - A Blink camera Python library
6
6
https://github.com/fronzbot/blinkpy
7
7
Original protocol hacking by MattTW : https://github.com/MattTW/BlinkMonitorProtocol
8
+
8
9
Published under the MIT license - See LICENSE file for more details.
10
+
9
11
"Blink Wire-Free HS Home Monitoring & Alert Systems" is a trademark owned by Immedia Inc., see www.blinkforhome.com for more information.
10
12
I am in no way affiliated with Blink, nor Immedia Inc.
11
13
'''
12
14
15
+ import logging
13
16
import requests
14
17
import getpass
15
18
import json
16
- import errors as ERROR
17
- from constants import (BLINK_URL , LOGIN_URL ,
18
- BASE_URL , DEFAULT_URL ,
19
- HOME_URL , EVENT_URL ,
20
- NETWORK_URL , NETWORKS_URL ,
21
- ONLINE )
19
+
20
+ BLINK_URL = 'immedia-semi.com'
21
+ LOGIN_URL = 'https://prod.' + BLINK_URL + '/login'
22
+ BASE_URL = 'https://prod.' + BLINK_URL
23
+ DEFAULT_URL = 'prod.' + BLINK_URL
24
+
25
+ logger = logging .getLogger ('blinkpy' )
22
26
23
27
24
28
def _request (url , data = None , headers = None , type = 'get' , stream = False , json = True ):
@@ -30,18 +34,18 @@ def _request(url, data=None, headers=None, type='get', stream=False, json=True):
30
34
elif type is 'get' and not json :
31
35
response = requests .get (url , headers = headers , stream = stream )
32
36
else :
33
- raise BlinkException ( ERROR . REQUEST )
37
+ raise ValueError ( "Cannot perform requests of type " + type )
34
38
35
39
if json and 'message' in response .keys ():
36
- raise BlinkAuthenticationException (( response ['code' ], response ['message' ]) )
40
+ raise BlinkAuthenticationException (response ['code' ], response ['message' ])
37
41
38
42
return response
39
43
40
44
41
45
class BlinkException (Exception ):
42
- def __init__ (self , errcode ):
43
- self .id = errcode [ 0 ]
44
- self .message = errcode [ 1 ]
46
+ def __init__ (self , id , message ):
47
+ self .id = id
48
+ self .message = message
45
49
46
50
47
51
class BlinkAuthenticationException (BlinkException ):
@@ -181,7 +185,7 @@ def update(self, values):
181
185
self ._NOTIFICATIONS = values ['notifications' ]
182
186
183
187
def image_refresh (self ):
184
- url = HOME_URL
188
+ url = BASE_URL + '/homescreen'
185
189
response = _request (url , headers = self ._HEADER , type = 'get' )['devices' ]
186
190
for element in response :
187
191
try :
@@ -254,17 +258,18 @@ def region_id(self):
254
258
@property
255
259
def events (self ):
256
260
"""Gets all events on server"""
257
- url = EVENT_URL + self ._NETWORKID
261
+ url = BASE_URL + '/events/network/' + self ._NETWORKID
258
262
headers = self ._AUTH_HEADER
259
263
self ._EVENTS = _request (url , headers = headers , type = 'get' )['event' ]
260
264
return self ._EVENTS
261
265
262
266
@property
263
267
def online (self ):
264
268
"""Returns True or False depending on if sync module is online/offline"""
265
- url = NETWORK_URL + self ._NETWORKID + '/syncmodules'
269
+ url = BASE_URL + 'network/' + self ._NETWORKID + '/syncmodules'
266
270
headers = self ._AUTH_HEADER
267
- return ONLINE [_request (url , headers = headers , type = 'get' )['syncmodule' ]['status' ]]
271
+ online_dict = {'online' : True , 'offline' : False }
272
+ return online_dict [_request (url , headers = headers , type = 'get' )['syncmodule' ]['status' ]]
268
273
269
274
def last_motion (self ):
270
275
"""Finds last motion of each camera"""
@@ -292,7 +297,7 @@ def arm(self, value):
292
297
value_to_append = 'arm'
293
298
else :
294
299
value_to_append = 'disarm'
295
- url = NETWORK_URL + self ._NETWORKID + '/' + value_to_append
300
+ url = BASE_URL + '/network/' + self ._NETWORKID + '/' + value_to_append
296
301
_request (url , headers = self ._AUTH_HEADER , type = 'post' )
297
302
298
303
def refresh (self ):
@@ -313,7 +318,7 @@ def get_summary(self):
313
318
headers = self ._AUTH_HEADER
314
319
315
320
if self ._AUTH_HEADER is None :
316
- raise BlinkException (ERROR . AUTH_TOKEN )
321
+ raise BlinkException (0 , "Authentication header incorrect. Are you sure you logged in and received your token?" )
317
322
318
323
return _request (url , headers = headers , type = 'get' )
319
324
@@ -332,16 +337,16 @@ def get_cameras(self):
332
337
def set_links (self ):
333
338
"""Sets access links and required headers for each camera in system"""
334
339
for name , camera in self ._CAMERAS .items ():
335
- image_url = NETWORK_URL + self ._NETWORKID + '/camera/' + camera .id + '/thumbnail'
336
- arm_url = NETWORK_URL + self ._NETWORKID + '/camera/' + camera .id + '/'
340
+ image_url = BASE_URL + '/network/' + self ._NETWORKID + '/camera/' + camera .id + '/thumbnail'
341
+ arm_url = BASE_URL + '/network/' + self ._NETWORKID + '/camera/' + camera .id + '/'
337
342
camera .image_link = image_url
338
343
camera .arm_link = arm_url
339
344
camera .header = self ._AUTH_HEADER
340
345
341
346
def setup_system (self ):
342
347
"""Method logs in and sets auth token and network ids for future requests"""
343
348
if self ._username is None or self ._password is None :
344
- raise BlinkAuthenticationException (ERROR . AUTHENTICATE )
349
+ raise BlinkAuthenticationException (3 , "Cannot authenticate since either password or username has not been set" )
345
350
346
351
self .get_auth_token ()
347
352
self .get_ids ()
@@ -356,9 +361,9 @@ def login(self):
356
361
def get_auth_token (self ):
357
362
"""Retrieves the authentication token from Blink"""
358
363
if not isinstance (self ._username , str ):
359
- raise BlinkAuthenticationException (ERROR . USERNAME )
364
+ raise BlinkAuthenticationException (0 , "Username must be a string" )
360
365
if not isinstance (self ._password , str ):
361
- raise BlinkAuthenticationException (ERROR . PASSWORD )
366
+ raise BlinkAuthenticationException (0 , "Password must be a string" )
362
367
363
368
headers = {'Host' : DEFAULT_URL ,
364
369
'Content-Type' : 'application/json'
@@ -378,11 +383,11 @@ def get_auth_token(self):
378
383
379
384
def get_ids (self ):
380
385
"""Sets the network ID and Account ID"""
381
- url = NETWORKS_URL
386
+ url = BASE_URL + '/networks'
382
387
headers = self ._AUTH_HEADER
383
388
384
389
if self ._AUTH_HEADER is None :
385
- raise BlinkException (ERROR . AUTH_TOKEN )
390
+ raise BlinkException (0 , "Authentication header incorrect. Are you sure you logged in and received your token?" )
386
391
387
392
response = _request (url , headers = headers , type = 'get' )
388
393
self ._NETWORKID = str (response ['networks' ][0 ]['id' ])
0 commit comments