feature: lookup camera hostname just once

pull/106/head
Jeff Culverhouse 10 months ago
parent be99ebe527
commit 493d3e3878

@ -0,0 +1,7 @@
1.0.1
- lookup camera hostnames to get ip at config, so we aren't don't
100k lookups every day (in my 4 camera setup)
1.0.0
- initial release

@ -1 +1 @@
1.0.0 1.0.1

@ -61,7 +61,13 @@ class AmcrestAPI(object):
async def get_device(self, host, device_name): async def get_device(self, host, device_name):
try: try:
camera = self.get_camera(host) # resolve host and setup camera by ip so we aren't making 100k DNS lookups per day
try:
host_ip = get_ip_address(host)
self.logger.info(f'nslookup {host} got us {host_ip}')
camera = self.get_camera(host_ip)
except Exception as err:
self.logger.error(f'Failed to resolve {host} to ip address: {err}')
device_type = camera.device_type.replace('type=', '').strip() device_type = camera.device_type.replace('type=', '').strip()
is_ad110 = device_type == 'AD110' is_ad110 = device_type == 'AD110'
@ -89,6 +95,7 @@ class AmcrestAPI(object):
'camera': camera, 'camera': camera,
'config': { 'config': {
'host': host, 'host': host,
'host_ip': host_ip,
'device_name': device_name, 'device_name': device_name,
'device_type': device_type, 'device_type': device_type,
'device_class': camera.device_class, 'device_class': camera.device_class,

@ -5,7 +5,9 @@
# #
# The software is provided 'as is', without any warranty. # The software is provided 'as is', without any warranty.
import ipaddress
import os import os
import socket
# Helper functions and callbacks # Helper functions and callbacks
def read_file(file_name): def read_file(file_name):
@ -22,3 +24,18 @@ def read_version():
def to_gb(total): def to_gb(total):
return str(round(float(total[0]) / 1024 / 1024 / 1024, 2)) return str(round(float(total[0]) / 1024 / 1024 / 1024, 2))
def is_ipv4(string):
try:
ipaddress.IPv4Network(string)
return True
except ValueError:
return False
def get_ip_address(string):
if is_ipv4(string):
return string
for i in socket.getaddrinfo(string, 0):
if i[0] is socket.AddressFamily.AF_INET and i[1] is socket.SocketKind.SOCK_RAW:
return i[4][0]
raise Exception(f'failed to find ip address for {string}')
Loading…
Cancel
Save