From ca033f024ed77e23896c78ac88ccdcd4d5566eae Mon Sep 17 00:00:00 2001 From: SpudGunMan Date: Wed, 18 Dec 2024 10:53:44 -0800 Subject: [PATCH] enhanceNews returns a random line from the file --- config.template | 2 ++ modules/filemon.py | 19 ++++++++++++++----- modules/settings.py | 1 + modules/system.py | 2 +- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/config.template b/config.template index 93784e8..7449e82 100644 --- a/config.template +++ b/config.template @@ -185,6 +185,8 @@ file_path = alert.txt broadcastCh = 2 enable_read_news = False news_file_path = news.txt +# only return a single random line from the news file +news_random_line = False [smtp] # enable or disable the SMTP module diff --git a/modules/filemon.py b/modules/filemon.py index e3cdabe..950736d 100644 --- a/modules/filemon.py +++ b/modules/filemon.py @@ -3,22 +3,31 @@ from modules.log import * import asyncio +import random import os trap_list_filemon = ("readnews",) -def read_file(file_monitor_file_path): +def read_file(file_monitor_file_path, random_line_only=False): try: - with open(file_monitor_file_path, 'r') as f: - content = f.read() - return content + if random_line_only: + # read a random line from the file + with open(file_monitor_file_path, 'r') as f: + lines = f.readlines() + return random.choice(lines) + else: + # read the whole file + with open(file_monitor_file_path, 'r') as f: + content = f.read() + return content except Exception as e: logger.warning(f"FileMon: Error reading file: {file_monitor_file_path}") return None def read_news(): # read the news file on demand - return read_file(news_file_path) + return read_file(news_file_path, read_news_enabled) + def write_news(content, append=False): # write the news file on demand diff --git a/modules/settings.py b/modules/settings.py index e34185b..db6446f 100644 --- a/modules/settings.py +++ b/modules/settings.py @@ -224,6 +224,7 @@ try: file_monitor_broadcastCh = config['fileMon'].getint('broadcastCh', 2) # default 2 read_news_enabled = config['fileMon'].getboolean('enable_read_news', False) # default disabled news_file_path = config['fileMon'].get('news_file_path', 'news.txt') # default news.txt + news_random_line_only = config['fileMon'].getboolean('news_random_line', False) # default False # games game_hop_limit = config['messagingSettings'].getint('game_hop_limit', 5) # default 3 hops diff --git a/modules/system.py b/modules/system.py index ecec45b..0871e49 100644 --- a/modules/system.py +++ b/modules/system.py @@ -190,7 +190,7 @@ if file_monitor_enabled or read_news_enabled: from modules.filemon import * # from the spudgunman/meshing-around repo if read_news_enabled: trap_list = trap_list + trap_list_filemon # items readnews - help_message = help_message + ", readmail" + help_message = help_message + ", readnews" # clean up the help message help_message = help_message.split(", ")