From d83b71aeca013aca3f62f827e110e99c731d0d6b Mon Sep 17 00:00:00 2001 From: mcguirepr89 Date: Sat, 25 Jun 2022 15:04:51 -0400 Subject: [PATCH] accepts all DB variables and JSON objects --- scripts/server.py | 19 ++++---- scripts/utils/notifications.py | 88 +++++++++++++++++++++++++++++----- 2 files changed, 83 insertions(+), 24 deletions(-) diff --git a/scripts/server.py b/scripts/server.py index f7d7fbe..daa7456 100755 --- a/scripts/server.py +++ b/scripts/server.py @@ -32,6 +32,10 @@ ADDR = (SERVER, PORT) FORMAT = 'utf-8' DISCONNECT_MESSAGE = "!DISCONNECT" +userDir = os.path.expanduser('~') +DB_PATH = userDir + '/BirdNET-Pi/scripts/birds.db' + + server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) @@ -43,7 +47,6 @@ except BaseException: # Open most recent Configuration and grab DB_PWD as a python variable -userDir = os.path.expanduser('~') with open(userDir + '/BirdNET-Pi/scripts/thisrun.txt', 'r') as f: this_run = f.readlines() audiofmt = "." + str(str(str([i for i in this_run if i.startswith('AUDIOFMT')]).split('=')[1]).split('\\')[0]) @@ -387,7 +390,7 @@ def handle_client(conn, addr): # Connect to SQLite Database for attempt_number in range(3): try: - con = sqlite3.connect(userDir + '/BirdNET-Pi/scripts/birds.db') + con = sqlite3.connect(DB_PATH) cur = con.cursor() cur.execute("INSERT INTO detections VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (Date, Time, Sci_Name, Com_Name, str(score), Lat, Lon, Cutoff, Week, Sens, Overlap, File_Name)) @@ -402,7 +405,7 @@ def handle_client(conn, addr): # Apprise of detection if not already alerted this run. if not entry[0] in species_apprised_this_run: settings_dict = config_to_settings(userDir + '/BirdNET-Pi/scripts/thisrun.txt') - sendAppriseNotifications(str(entry[0]), str(entry[1]), File_Name, settings_dict) + sendAppriseNotifications(species, str(score), File_Name, Date, Time, Week, Lat, Lon, Cutoff, Sens, Overlap, settings_dict, DB_PATH) species_apprised_this_run.append(entry[0]) print(str(current_date) + @@ -424,14 +427,8 @@ def handle_client(conn, addr): str(args.sensitivity) + ';' + str(args.overlap) + - Com_Name.replace(" ", "_") + - '-' + - str(score) + - '-' + - str(current_date) + - '-birdnet-' + - str(current_time) + - audiofmt + + ';' + + File_Name + '\n') if birdweather_id != "99999": diff --git a/scripts/utils/notifications.py b/scripts/utils/notifications.py index 90e2b7b..79436a2 100644 --- a/scripts/utils/notifications.py +++ b/scripts/utils/notifications.py @@ -21,7 +21,7 @@ def notify(body, title): ) -def sendAppriseNotifications(species, confidence, path, settings_dict, db_path=DB_PATH): +def sendAppriseNotifications(species, confidence, path, date, time, week, latitude, longitude, cutoff, sens, overlap, settings_dict, db_path=DB_PATH): # print(sendAppriseNotifications) # print(settings_dict) if os.path.exists(APPRISE_CONFIG) and os.path.getsize(APPRISE_CONFIG) > 0: @@ -40,8 +40,30 @@ def sendAppriseNotifications(species, confidence, path, settings_dict, db_path=D listenurl = websiteurl+"?filename="+path if settings_dict.get('APPRISE_NOTIFY_EACH_DETECTION') == "1": - notify_body = body.replace("$sciname", sciName).replace("$comname", comName).replace("$confidence", confidence).replace("$listenurl", listenurl) - notify_title = title.replace("$sciname", sciName).replace("$comname", comName).replace("$confidence", confidence).replace("$listenurl", listenurl) + notify_body = body.replace("$sciname", sciName)\ + .replace("$comname", comName)\ + .replace("$confidence", confidence)\ + .replace("$listenurl", listenurl)\ + .replace("$date", date)\ + .replace("$time", time)\ + .replace("$week", week)\ + .replace("$latitude", latitude)\ + .replace("$longitude", longitude)\ + .replace("$cutoff", cutoff)\ + .replace("$sens", sens)\ + .replace("$overlap", overlap) + notify_title = title.replace("$sciname", sciName)\ + .replace("$comname", comName)\ + .replace("$confidence", confidence)\ + .replace("$listenurl", listenurl)\ + .replace("$date", date)\ + .replace("$time", time)\ + .replace("$week", week)\ + .replace("$latitude", latitude)\ + .replace("$longitude", longitude)\ + .replace("$cutoff", cutoff)\ + .replace("$sens", sens)\ + .replace("$overlap", overlap) notify(notify_body, notify_title) APPRISE_NOTIFICATION_NEW_SPECIES_DAILY_COUNT_LIMIT = 1 # Notifies the first N per day. @@ -50,7 +72,7 @@ def sendAppriseNotifications(species, confidence, path, settings_dict, db_path=D con = sqlite3.connect(db_path) cur = con.cursor() today = datetime.now().strftime("%Y-%m-%d") - cur.execute(f"SELECT DISTINCT(Com_Name), count(Com_Name) FROM detections WHERE Date = date('{today}') GROUP BY Com_Name") + cur.execute(f"SELECT DISTINCT(Com_Name), COUNT(Com_Name) FROM detections WHERE Date = DATE('{today}') GROUP BY Com_Name") known_species = cur.fetchall() detections = [d[1] for d in known_species if d[0] == comName.replace("'", "")] numberDetections = 0 @@ -58,11 +80,31 @@ def sendAppriseNotifications(species, confidence, path, settings_dict, db_path=D numberDetections = detections[0] if numberDetections > 0 and numberDetections <= APPRISE_NOTIFICATION_NEW_SPECIES_DAILY_COUNT_LIMIT: print("send the notification") - notify_body = body.replace("$sciname", sciName).replace("$comname", comName)\ - .replace("$confidence", confidence).replace("$listenurl", listenurl)\ + notify_body = body.replace("$sciname", sciName)\ + .replace("$comname", comName)\ + .replace("$confidence", confidence)\ + .replace("$listenurl", listenurl)\ + .replace("$date", date)\ + .replace("$time", time)\ + .replace("$week", week)\ + .replace("$latitude", latitude)\ + .replace("$longitude", longitude)\ + .replace("$cutoff", cutoff)\ + .replace("$sens", sens)\ + .replace("$overlap", overlap)\ + " (first time today)" - notify_title = title.replace("$sciname", sciName).replace("$comname", comName)\ - .replace("$confidence", confidence).replace("$listenurl", listenurl)\ + notify_title = title.replace("$sciname", sciName)\ + .replace("$comname", comName)\ + .replace("$confidence", confidence)\ + .replace("$listenurl", listenurl)\ + .replace("$date", date)\ + .replace("$time", time)\ + .replace("$week", week)\ + .replace("$latitude", latitude)\ + .replace("$longitude", longitude)\ + .replace("$cutoff", cutoff)\ + .replace("$sens", sens)\ + .replace("$overlap", overlap)\ + " (first time today)" notify(notify_body, notify_title) con.close() @@ -76,18 +118,38 @@ def sendAppriseNotifications(species, confidence, path, settings_dict, db_path=D con = sqlite3.connect(db_path) cur = con.cursor() today = datetime.now().strftime("%Y-%m-%d") - cur.execute(f"SELECT DISTINCT(Com_Name), count(Com_Name) FROM detections WHERE Date >= date('{today}', '-7 day') GROUP BY Com_Name") + cur.execute(f"SELECT DISTINCT(Com_Name), COUNT(Com_Name) FROM detections WHERE Date >= DATE('{today}', '-7 day') GROUP BY Com_Name") known_species = cur.fetchall() detections = [d[1] for d in known_species if d[0] == comName.replace("'", "")] numberDetections = 0 if len(detections): numberDetections = detections[0] if numberDetections > 0 and numberDetections <= 5: - notify_body = body.replace("$sciname", sciName).replace("$comname", comName)\ - .replace("$confidence", confidence).replace("$listenurl", listenurl)\ + notify_body = body.replace("$sciname", sciName)\ + .replace("$comname", comName)\ + .replace("$confidence", confidence)\ + .replace("$listenurl", listenurl)\ + .replace("$date", date)\ + .replace("$time", time)\ + .replace("$week", week)\ + .replace("$latitude", latitude)\ + .replace("$longitude", longitude)\ + .replace("$cutoff", cutoff)\ + .replace("$sens", sens)\ + .replace("$overlap", overlap)\ + " (only seen " + str(int(numberDetections)) + " times in last 7d)" - notify_title = title.replace("$sciname", sciName).replace("$comname", comName)\ - .replace("$confidence", confidence).replace("$listenurl", listenurl)\ + notify_title = title.replace("$sciname", sciName)\ + .replace("$comname", comName)\ + .replace("$confidence", confidence)\ + .replace("$listenurl", listenurl)\ + .replace("$date", date)\ + .replace("$time", time)\ + .replace("$week", week)\ + .replace("$latitude", latitude)\ + .replace("$longitude", longitude)\ + .replace("$cutoff", cutoff)\ + .replace("$sens", sens)\ + .replace("$overlap", overlap)\ + " (only seen " + str(int(numberDetections)) + " times in last 7d)" notify(notify_body, notify_title) con.close()