From b828a84310f7ad4b9d2db948a7b1c8cc70e1c9a1 Mon Sep 17 00:00:00 2001 From: rhpidfyre Date: Sat, 7 Jun 2025 14:44:20 -0400 Subject: [PATCH] Use local machine time instead of unix for messages and have better timestamp formatting --- src/cli.py | 7 ------- src/cpu.py | 5 ++--- src/main.py | 20 ++++++++++++-------- src/ntfy.py | 9 ++++++--- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/cli.py b/src/cli.py index 7023cca..51d4cb8 100644 --- a/src/cli.py +++ b/src/cli.py @@ -1,12 +1,5 @@ import argparse -_ntfy_configure_prompt = """\033[4mPlease configure an ntfy url before starting.\033[0m -Examples: -\033[32mpython3 main.py 10.0.13.37:42069 -python3 main.py ntfy.domain.com\033[0m - -Use \033[32m-h\033[0m or \033[32m--help\033[0m for a full list of options.""" - def Interface(): parser = argparse.ArgumentParser() parser.add_argument("server_address", help="The ntfy server address.") diff --git a/src/cpu.py b/src/cpu.py index 722d348..32a2a6c 100644 --- a/src/cpu.py +++ b/src/cpu.py @@ -7,9 +7,8 @@ from typing import Optional from ntfy import Ntfy _time_now = time.time() -last_cpu_check_warning: float = _time_now - -last_check_debounce: int = 120 # Seconds +last_cpu_check_warning: float = _time_now +last_check_debounce: int = 120 # Seconds class Tempature: cpu_temp_warning_message: str = "🌡️ CPU is at a high tempature." diff --git a/src/main.py b/src/main.py index b9e42dc..f408a57 100644 --- a/src/main.py +++ b/src/main.py @@ -8,24 +8,28 @@ from datetime import datetime from typing import TypedDict from ntfy import Ntfy -_monitoring_prompt = f"""{datetime.fromtimestamp(time.time())} -Ntfy monitoring software is now listening. - -Source code available at: - -""" - class Config(TypedDict): cpu_temp_check_disabled: bool cpu_warning_temp: int update_interval: int ntfy_server_url: str +def start_prompt(server_url: str) -> str: + return f"""{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} +Listening and sending notifications to: \033[32m{server_url}\033[0m. + +Source code available at: + + +------""" + def start(config: Config): ntfy = Ntfy(config["ntfy_server_url"]) ntfy_cpu_temp_monitor = cpu.Tempature(ntfy, config["cpu_warning_temp"]) - print(_monitoring_prompt) + print(config) + print(start_prompt(config["ntfy_server_url"])) + while True: if not config["cpu_temp_check_disabled"]: ntfy_cpu_temp_monitor.ntfy_check() diff --git a/src/ntfy.py b/src/ntfy.py index fb84731..6b025dd 100644 --- a/src/ntfy.py +++ b/src/ntfy.py @@ -1,15 +1,18 @@ import requests -import time from datetime import datetime +def print_t(out: str): + t = datetime.now() + print(f"({t.strftime('%Y-%m-%d')})[{t.strftime('%H:%M:%S')}]: " + out) + class Ntfy: def __init__(self, server: str): self.server = server def send(self, message: str): - print(f"[{datetime.fromtimestamp(time.time())}]: {message}") + print_t(message) try: requests.post(self.server, data=message) except Exception as err: - print(f"Ntfy failed. {err}") + print_t(f"Ntfy failed. \033[31m{err}\033[0m")