This commit is contained in:
frederik
2025-10-19 11:22:44 +02:00
parent f2a2214c6f
commit 771855812f
3 changed files with 89 additions and 36 deletions
+5 -35
View File
@@ -1,12 +1,12 @@
import apprise
import os
import socket
import sqlite3
from datetime import datetime
import requests
import html
import time as timeim
from .db import get_todays_count_for, get_this_weeks_count_for
userDir = os.path.expanduser('~')
APPRISE_CONFIG = userDir + '/BirdNET-Pi/apprise.txt'
APPRISE_BODY = userDir + '/BirdNET-Pi/body.txt'
@@ -44,9 +44,7 @@ def notify(body, title, attached=""):
)
def sendAppriseNotifications(sci_name, com_name, confidence, confidencepct, path,
date, time, week, latitude, longitude, cutoff,
sens, overlap, settings_dict, db_path=DB_PATH):
def sendAppriseNotifications(sci_name, com_name, confidence, confidencepct, path, date, time, week, latitude, longitude, cutoff, sens, overlap, settings_dict):
def render_template(template, reason=""):
ret = template.replace("$sciname", sci_name) \
.replace("$comname", com_name) \
@@ -124,7 +122,7 @@ def sendAppriseNotifications(sci_name, com_name, confidence, confidencepct, path
APPRISE_NOTIFICATION_NEW_SPECIES_DAILY_COUNT_LIMIT = 1 # Notifies the first N per day.
if settings_dict.get('APPRISE_NOTIFY_NEW_SPECIES_EACH_DAY') == "1":
numberDetections = get_todays_count_for(db_path, sci_name)
numberDetections = get_todays_count_for(sci_name)
if 0 < numberDetections <= APPRISE_NOTIFICATION_NEW_SPECIES_DAILY_COUNT_LIMIT:
print("send the notification")
notify_body = render_template(body, "first time today")
@@ -133,7 +131,7 @@ def sendAppriseNotifications(sci_name, com_name, confidence, confidencepct, path
species_last_notified[com_name] = int(timeim.time())
if settings_dict.get('APPRISE_NOTIFY_NEW_SPECIES') == "1":
numberDetections = get_this_weeks_count_for(db_path, sci_name)
numberDetections = get_this_weeks_count_for(sci_name)
if 0 < numberDetections <= 5:
reason = f"only seen {numberDetections} times in last 7d"
notify_body = render_template(body, reason)
@@ -142,33 +140,5 @@ def sendAppriseNotifications(sci_name, com_name, confidence, confidencepct, path
species_last_notified[com_name] = int(timeim.time())
def get_todays_count_for(db_path, sci_name):
today = datetime.now().strftime("%Y-%m-%d")
select_sql = f"SELECT COUNT(*) FROM detections WHERE Date = DATE('{today}') AND Sci_Name = '{sci_name}'"
records = get_records(db_path, select_sql)
return records[0][0] if records else 0
def get_this_weeks_count_for(db_path, sci_name):
today = datetime.now().strftime("%Y-%m-%d")
select_sql = f"SELECT COUNT(*) FROM detections WHERE Date >= DATE('{today}', '-7 day') AND Sci_Name = '{sci_name}'"
records = get_records(db_path, select_sql)
return records[0][0] if records else 0
def get_records(db_path, select_sql):
try:
con = sqlite3.connect(f"file:{db_path}?mode=ro", uri=True)
cur = con.cursor()
cur.execute(select_sql)
records = cur.fetchall()
con.close()
except sqlite3.Error as e:
print(f"Database busy: {e}")
timeim.sleep(2)
records = []
return records
if __name__ == "__main__":
print("notfications")