Browse Source

fix app_mode setting

subDesTagesMitExtraKaese 2 years ago
parent
commit
f81a93ede1
2 changed files with 14 additions and 12 deletions
  1. 7 6
      main.py
  2. 7 6
      pihole.py

+ 7 - 6
main.py

@@ -34,14 +34,15 @@ try:
   # optional Pi-hole authentication
   # optional Pi-hole authentication
   AUTHENTICATION_TOKEN = os.getenv('PIHOLE_AUTHENTICATION', None)
   AUTHENTICATION_TOKEN = os.getenv('PIHOLE_AUTHENTICATION', None)
 
 
-  # optional App Mode
-  APP_MODE = AppMode(os.getenv('APP_MODE', 'Totals'))
-
 except KeyError as e:
 except KeyError as e:
   logger.fatal('Missing environment variable: {}'.format(e))
   logger.fatal('Missing environment variable: {}'.format(e))
   sys.exit(1)
   sys.exit(1)
-except ValueError as e:
-  logger.fatal('Invalid environment variable: {}'.format(e))
+
+try:
+  # optional App Mode
+  APP_MODE = AppMode[os.getenv('APP_MODE', 'Totals')]
+except KeyError as e:
+  logger.fatal('{} is not a valid AppMode'.format(e))
   sys.exit(1)
   sys.exit(1)
 
 
 if APP_MODE != AppMode.Totals and not AUTHENTICATION_TOKEN:
 if APP_MODE != AppMode.Totals and not AUTHENTICATION_TOKEN:
@@ -56,7 +57,7 @@ def main():
   next_update = time.monotonic()
   next_update = time.monotonic()
 
 
   logger.info('Starting PiHole Data Logger to InfluxDB')
   logger.info('Starting PiHole Data Logger to InfluxDB')
-  logger.info('AppMode: {}'.format(APP_MODE))
+  logger.info('AppMode: {}'.format(APP_MODE.name))
 
 
   # Test Pi-hole connection
   # Test Pi-hole connection
   try:
   try:

+ 7 - 6
pihole.py

@@ -6,7 +6,6 @@ from datetime import datetime
 from enum import Enum
 from enum import Enum
 from influxdb_client import Point
 from influxdb_client import Point
 from pandas import DataFrame
 from pandas import DataFrame
-from urllib.parse import urlparse
 
 
 class QueryStati(Enum):
 class QueryStati(Enum):
   Blocked = 1
   Blocked = 1
@@ -16,14 +15,16 @@ class QueryStati(Enum):
   Unknown = 5
   Unknown = 5
 
 
 class PiHole:
 class PiHole:
-  def __init__(self, url, token):
-    self.host = url
-    self.url = urlparse(url)
+  def __init__(self, host, token):
+    self.host = host
+    if host.startswith("http"):
+      self.url = host
+    else:
+      self.url = f"http://{host}"
     self.token = token
     self.token = token
 
 
   def query(self, endpoint, params={}):
   def query(self, endpoint, params={}):
-    url = "{}://{}/admin/{}.php".format(self.url.scheme or "http", self.url.netloc, endpoint)
-    return requests.get(url, params=params)
+    return requests.get(f"{self.url}/admin/{endpoint}.php", params=params)
   
   
   def request_all_queries(self, start: float, end: float):
   def request_all_queries(self, start: float, end: float):
     """
     """