-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzm.py
84 lines (68 loc) · 2.75 KB
/
zm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Simple ZoneMinder API handler - more like a stub
#
# Copyright (C) 2017 Tadeusz Magura-Witkowski
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import http.cookiejar, urllib.request, urllib.parse, urllib.error
import json
import logging
class ZoneMinderAPILoginError(Exception):
def __init__(self, *args, **kwargs):
super(ZoneMinderAPILoginError, self).__init__(*args, **kwargs)
class ZoneMinderAPI(object):
def __init__(self, server, login, password):
super(ZoneMinderAPI, self).__init__()
self.server = server
self.login = login
self.password = password
self.logger = logging.getLogger('zoneminder.ZoneMinderAPI')
cj = http.cookiejar.CookieJar()
self.__opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
self.__url = 'http://{}/zm'.format(server)
def __request(self, url, tries=5):
self.logger.info('Request URL: {}, tries: {}'.format(url, tries))
if tries == 0:
raise ZoneMinderAPILoginError('Login tries exceed')
try:
url_to_open = '{}/api/{}'.format(self.__url, url)
self.logger.debug('Opening URL: {}'.format(url_to_open))
with self.__opener.open(url_to_open) as response:
data = json.loads(response.read().decode())
return data
except urllib.error.HTTPError as e:
self.logger.debug('Response code: {}'.format(e.code))
if e.code == 401:
self.logger.info('Request unauthorized, trying to login')
self.__login()
return self.__request(url, tries - 1)
def __login(self):
self.logger.info('Logging in...')
http_data = {
'username': self.login,
'password': self.password,
'action': 'login',
'view': 'console',
}
url_to_open = '{}/index.php'.format(self.__url)
self.logger.debug('Opening URL: {}'.format(url_to_open))
with self.__opener.open(url_to_open, urllib.parse.urlencode(http_data).encode()) as response:
data = response.read()
if b'ZoneMinder Login' in data:
raise ZoneMinderAPILoginError('Bad login or password')
def get_active_monitors(self, minutes):
data = self.__request('events/consoleEvents/{}%20minute.json/AlarmFrames%20>=:%2010.json'.format(int(minutes)))
results = data['results']
if isinstance(results, dict):
return tuple(map(int, results.keys()))
return ()