file overhaul

fixed filereader enhanced newsread fixed bee
This commit is contained in:
SpudGunMan
2025-11-03 13:58:57 -08:00
parent 1388771cc1
commit a96d57580a
15 changed files with 65 additions and 16 deletions

5
.gitignore vendored
View File

@@ -19,8 +19,8 @@ etc/*.service
# Python cache # Python cache
__pycache__/ __pycache__/
# rag data # data files
data/rag/* data/*.json
# qrz db # qrz db
data/qrz.db data/qrz.db
@@ -31,6 +31,7 @@ data/inventory.db
# fileMonitor test file # fileMonitor test file
bee.txt bee.txt
*news.txt
# .csv files # .csv files
*.csv *.csv

View File

@@ -392,8 +392,10 @@ broadcastCh = 2
# news command will return the contents of a text file # news command will return the contents of a text file
enable_read_news = False enable_read_news = False
news_file_path = ../data/news.txt news_file_path = ../data/news.txt
# only return a single random line from the news file # only return a single random (head)line from the news file
news_random_line = False news_random_line = False
# only return news 'block' (seprated by two newlines) randomly (precidence over news_random_line)
news_block_mode = True
# enable the use of exernal shell commands, this enables some data in `sysinfo` # enable the use of exernal shell commands, this enables some data in `sysinfo`
enable_runShellCmd = False enable_runShellCmd = False

View File

@@ -1 +1,3 @@
database admin tool is in [./etc/db_admin.py](../etc/db_admin.py) database admin tool is in [./etc/db_admin.py](../etc/db_admin.py)
this folder is populated with install.sh
to manually populate ` cp etc/data/* data/. `

View File

@@ -1 +0,0 @@
Today in meshtastic you are looking at the coolest bot on the block.

View File

@@ -1 +0,0 @@
no new news is good news!

View File

@@ -157,6 +157,13 @@ if [[ ! -f modules/custom_scheduler.py ]]; then
printf "\nCustom scheduler template copied to modules/custom_scheduler.py\n" printf "\nCustom scheduler template copied to modules/custom_scheduler.py\n"
fi fi
# copy contents of etc/data to data/
if [[ -d data ]]; then
printf "\nCopying data templates to data/ directory\n"
mkdir -p data
cp etc/data/* data/
fi
# generate config file, check if it exists # generate config file, check if it exists
if [[ -f config.ini ]]; then if [[ -f config.ini ]]; then
printf "\nConfig file already exists, moving to backup config.old\n" printf "\nConfig file already exists, moving to backup config.old\n"
@@ -514,4 +521,4 @@ exit 0
# after install shenannigans # after install shenannigans
# add 'bee = True' to config.ini General section. # add 'bee = True' to config.ini General section.
# wget https://gist.github.com/MattIPv4/045239bc27b16b2bcf7a3a9a4648c08a -O bee.txt # wget https://gist.githubusercontent.com/MattIPv4/045239bc27b16b2bcf7a3a9a4648c08a/raw/2411e31293a35f3e565f61e7490a806d4720ea7e/bee%2520movie%2520script -O bee.txt

View File

@@ -483,15 +483,26 @@ def handle_wxalert(message_from_id, deviceID, message):
def handleNews(message_from_id, deviceID, message, isDM): def handleNews(message_from_id, deviceID, message, isDM):
news = '' news = ''
# if news source is provided pass that to read_news()
if "?" in message.lower(): if "?" in message.lower():
return "returns the news. Add a source e.g. 📰readnews mesh" return "returns the news. Add a source e.g. 📰readnews mesh"
elif "readnews" in message.lower(): elif "readnews" in message.lower():
source = message.lower().replace("readnews", "").strip() source = message.lower().replace("readnews", "").strip()
if source: if source:
news = read_news(source) # if news source is provided pass that to read_news()
if my_settings.news_block_mode:
news = read_news(source=source, news_block_mode=True)
elif my_settings.news_random_line_only:
news = read_news(source=source, random_line_only=True)
else:
news = read_news(source=source)
else: else:
news = read_news() # no source provided, use news.txt
if my_settings.news_block_mode:
news = read_news(news_block_mode=True)
elif my_settings.news_random_line_only:
news = read_news(random_line_only=True)
else:
news = read_news()
if news: if news:
# if not a DM add the username to the beginning of msg # if not a DM add the username to the beginning of msg

View File

@@ -6,6 +6,7 @@ from modules.settings import (
file_monitor_file_path, file_monitor_file_path,
news_file_path, news_file_path,
news_random_line_only, news_random_line_only,
news_block_mode,
allowXcmd, allowXcmd,
bbs_admin_list, bbs_admin_list,
xCmd2factorEnabled, xCmd2factorEnabled,
@@ -23,16 +24,34 @@ trap_list_filemon = ("readnews",)
NEWS_DATA_DIR = os.path.join(os.path.dirname(__file__), '..', 'data') NEWS_DATA_DIR = os.path.join(os.path.dirname(__file__), '..', 'data')
newsSourcesList = [] newsSourcesList = []
def read_file(file_monitor_file_path, random_line_only=False): def read_file(file_monitor_file_path, random_line_only=False, news_block_mode=False):
logger.debug(f"FileMon: Reading file: {file_monitor_file_path} options - random_line_only: {random_line_only}, news_block_mode: {news_block_mode}")
try: try:
if not os.path.exists(file_monitor_file_path): if not os.path.exists(file_monitor_file_path):
if file_monitor_file_path == "bee.txt": if file_monitor_file_path == "bee.txt":
return "🐝buzz 💐buzz buzz🍯" return "🐝buzz 💐buzz buzz🍯"
if random_line_only: if news_block_mode:
# read a random block (separated by 2+ blank lines, robust to line endings)
with open(file_monitor_file_path, 'r', encoding='utf-8') as f:
content = f.read().replace('\r\n', '\n').replace('\r', '\n')
blocks = []
block = []
for line in content.split('\n'):
if line.strip() == '':
if block:
blocks.append('\n'.join(block).strip())
block = []
else:
block.append(line)
if block:
blocks.append('\n'.join(block).strip())
blocks = [b for b in blocks if b]
return random.choice(blocks) if blocks else None
elif random_line_only:
# read a random line from the file # read a random line from the file
with open(file_monitor_file_path, 'r', encoding='utf-8') as f: with open(file_monitor_file_path, 'r', encoding='utf-8') as f:
lines = f.readlines() lines = [line.strip() for line in f if line.strip()]
return random.choice(lines) return random.choice(lines) if lines else None
else: else:
# read the whole file # read the whole file
with open(file_monitor_file_path, 'r', encoding='utf-8') as f: with open(file_monitor_file_path, 'r', encoding='utf-8') as f:
@@ -42,13 +61,19 @@ def read_file(file_monitor_file_path, random_line_only=False):
logger.warning(f"FileMon: Error reading file: {file_monitor_file_path}") logger.warning(f"FileMon: Error reading file: {file_monitor_file_path}")
return None return None
def read_news(source=None): def read_news(source=None, random_line_only=False, news_block_mode=False):
# Reads the news file. If a source is provided, reads {source}_news.txt. # Reads the news file. If a source is provided, reads {source}_news.txt.
if source: if source:
file_path = os.path.join(NEWS_DATA_DIR, f"{source}_news.txt") file_path = os.path.join(NEWS_DATA_DIR, f"{source}_news.txt")
else: else:
file_path = os.path.join(NEWS_DATA_DIR, news_file_path) file_path = os.path.join(NEWS_DATA_DIR, news_file_path)
return read_file(file_path, news_random_line_only) # Block mode takes precedence over line mode
if news_block_mode:
return read_file(file_path, random_line_only=False, news_block_mode=True)
elif random_line_only:
return read_file(file_path, random_line_only=True, news_block_mode=False)
else:
return read_file(file_path)
def write_news(content, append=False): def write_news(content, append=False):
# write the news file on demand # write the news file on demand

View File

@@ -457,6 +457,9 @@ try:
read_news_enabled = config['fileMon'].getboolean('enable_read_news', False) # default disabled read_news_enabled = config['fileMon'].getboolean('enable_read_news', False) # default disabled
news_file_path = config['fileMon'].get('news_file_path', '../data/news.txt') # default ../data/news.txt news_file_path = config['fileMon'].get('news_file_path', '../data/news.txt') # default ../data/news.txt
news_random_line_only = config['fileMon'].getboolean('news_random_line', False) # default False news_random_line_only = config['fileMon'].getboolean('news_random_line', False) # default False
news_block_mode = config['fileMon'].getboolean('news_block_mode', False) # default False
if news_random_line_only and news_block_mode:
news_random_line_only = False
enable_runShellCmd = config['fileMon'].getboolean('enable_runShellCmd', False) # default False enable_runShellCmd = config['fileMon'].getboolean('enable_runShellCmd', False) # default False
allowXcmd = config['fileMon'].getboolean('allowXcmd', False) # default False allowXcmd = config['fileMon'].getboolean('allowXcmd', False) # default False
xCmd2factorEnabled = config['fileMon'].getboolean('twoFactor_enabled', True) # default True xCmd2factorEnabled = config['fileMon'].getboolean('twoFactor_enabled', True) # default True