Procházet zdrojové kódy

fix app_mode setting

subDesTagesMitExtraKaese před 2 roky
rodič
revize
f81a93ede1
2 změnil soubory, kde provedl 14 přidání a 12 odebrání
  1. 7 6
      main.py
  2. 7 6
      pihole.py

+ 7 - 6
main.py

@@ -34,14 +34,15 @@ try:
   # optional Pi-hole authentication
   AUTHENTICATION_TOKEN = os.getenv('PIHOLE_AUTHENTICATION', None)
 
-  # optional App Mode
-  APP_MODE = AppMode(os.getenv('APP_MODE', 'Totals'))
-
 except KeyError as e:
   logger.fatal('Missing environment variable: {}'.format(e))
   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)
 
 if APP_MODE != AppMode.Totals and not AUTHENTICATION_TOKEN:
@@ -56,7 +57,7 @@ def main():
   next_update = time.monotonic()
 
   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
   try:

+ 7 - 6
pihole.py

@@ -6,7 +6,6 @@ from datetime import datetime
 from enum import Enum
 from influxdb_client import Point
 from pandas import DataFrame
-from urllib.parse import urlparse
 
 class QueryStati(Enum):
   Blocked = 1
@@ -16,14 +15,16 @@ class QueryStati(Enum):
   Unknown = 5
 
 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
 
   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):
     """