Automated recordings
Sample automation of recording start/stop.
#import necessary libraries
import urllib3,requests,json
import time
# quick and dirty method for looking up cameras from name
def getCamByName(jsonobj, name):
for dict in jsonobj:
if name in dict['Hostname']:
return dict
# get the cam id # from name
def getCamIdByName(jsonobj, name):
return getCamByName(jsonobj, name)['Id']
# (optional) Disable the "insecure requests" warning for https certs
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# what watchtower url to control
watchtowerurl = 'https://localhost:4343'
cameraname = 'e3v8100'
# login and obtain API token
username = 'whitematter'
password = 'test'
r = requests.post(watchtowerurl+'/api/login', data = {'username': username, 'password': password}, verify=False)
j = json.loads(r.text)
apit = j['apitoken']
# Start saving
requests.post(watchtowerurl+'/api/cameras/action', data = {'IdGroup[]': [camid], 'Action': 'RECORDGROUP', 'apitoken': apit}, verify=False)
print("Started saving, 30 seconds")
time.sleep(30)
# Stop saving
requests.post(watchtowerurl+'/api/cameras/action', data = {'IdGroup[]': [camid], 'Action': 'STOPRECORDGROUP', 'apitoken': apit}, verify=False)
print("Stopped saving")
time.sleep(2) # let file writing on-disk clean-up
# normally this wait would not be necessary unless you're IMMEDIATELY disconnecting afterwards.
# Disconnect from camera
requests.post(watchtowerurl+'/api/cameras/action', data = {'Id': camid, 'Action': 'DISCONNECT', 'Iface': interface, 'apitoken': apit}, verify=False)
print("Disconnected")
Last modified March 20, 2020