From 96e5891238a095c0936229b9a3ed9257aec9c8eb Mon Sep 17 00:00:00 2001 From: jvteleco <43048057+jvteleco@users.noreply.github.com> Date: Tue, 16 Jun 2020 19:37:32 +0100 Subject: [PATCH] Update pihole.py. Added Defaults init and checked on refresh() that ip_address is not None. Please, feel forward to modify it in the correct way! --- dashmachine/platform/pihole.py | 42 ++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/dashmachine/platform/pihole.py b/dashmachine/platform/pihole.py index a600abe6..c192b8fd 100644 --- a/dashmachine/platform/pihole.py +++ b/dashmachine/platform/pihole.py @@ -28,9 +28,8 @@ def __init__(self, ip_address): self.pw = None def refresh(self): - rawdata = requests.get( - "http://" + self.ip_address + "/admin/api.php?summary" - ).json() + if self.ip_address != None: + rawdata = requests.get("http://" + self.ip_address + "/admin/api.php?summary").json() if self.auth_data != None: topdevicedata = requests.get( @@ -56,19 +55,20 @@ def refresh(self): + self.auth_data.token ).json()["querytypes"] - # Data that is returned is now parsed into vars - self.status = rawdata["status"] - self.domain_count = rawdata["domains_being_blocked"] - self.queries = rawdata["dns_queries_today"] - self.blocked = rawdata["ads_blocked_today"] - self.ads_percentage = rawdata["ads_percentage_today"] - self.unique_domains = rawdata["unique_domains"] - self.forwarded = rawdata["queries_forwarded"] - self.cached = rawdata["queries_cached"] - self.total_clients = rawdata["clients_ever_seen"] - self.unique_clients = rawdata["unique_clients"] - self.total_queries = rawdata["dns_queries_all_types"] - self.gravity_last_updated = rawdata["gravity_last_updated"] + if self.ip_address != None: + # Data that is returned is now parsed into vars + self.status = rawdata["status"] + self.domain_count = rawdata["domains_being_blocked"] + self.queries = rawdata["dns_queries_today"] + self.blocked = rawdata["ads_blocked_today"] + self.ads_percentage = rawdata["ads_percentage_today"] + self.unique_domains = rawdata["unique_domains"] + self.forwarded = rawdata["queries_forwarded"] + self.cached = rawdata["queries_cached"] + self.total_clients = rawdata["clients_ever_seen"] + self.unique_clients = rawdata["unique_clients"] + self.total_queries = rawdata["dns_queries_all_types"] + self.gravity_last_updated = rawdata["gravity_last_updated"] def refreshTop(self, count): if self.auth_data == None: @@ -265,7 +265,15 @@ def __init__(self, *args, **kwargs): for key, value in kwargs.items(): self.__dict__[key] = value - # self.pihole = PiHole(self.host) + + #set defaults + if not hasattr(self, "host"): + self.host = None + if not hasattr(self, "password"): + self.password = "password123" + + + self.pihole = PiHole(self.host) def process(self): self.pihole.refresh()