accepts all DB variables and JSON objects

This commit is contained in:
mcguirepr89
2022-06-25 15:04:51 -04:00
parent a148eb776b
commit d83b71aeca
2 changed files with 83 additions and 24 deletions
+8 -11
View File
@@ -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":
+75 -13
View File
@@ -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()