From f3f618e2a8ef19e0fa488c0033dccaec581c49c7 Mon Sep 17 00:00:00 2001 From: ehpersonal38 <103586016+ehpersonal38@users.noreply.github.com> Date: Wed, 17 Aug 2022 10:14:57 -0400 Subject: [PATCH 1/3] initial commit --- birdnet.conf-defaults | 2 ++ scripts/install_config.sh | 2 ++ scripts/update_birdnet_snippets.sh | 3 +++ scripts/utils/notifications.py | 16 +++++++++++++++- 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/birdnet.conf-defaults b/birdnet.conf-defaults index 239d4d8..6eb87b6 100644 --- a/birdnet.conf-defaults +++ b/birdnet.conf-defaults @@ -63,6 +63,8 @@ APPRISE_NOTIFICATION_BODY="A \$sciname \$comname was just detected with a confid APPRISE_NOTIFY_EACH_DETECTION=0 APPRISE_NOTIFY_NEW_SPECIES=0 APPRISE_WEEKLY_REPORT=1 +APPRISE_NOTIFY_NEW_SPECIES_EACH_DAY=0 +APPRISE_MINIMUM_SECONDS_BETWEEN_NOTIFICATIONS_PER_SPECIES=0 #---------------------- Flickr Images API Configuration -----------------------# ## If FLICKR_API_KEY is set, the web interface will try and display bird images diff --git a/scripts/install_config.sh b/scripts/install_config.sh index db980d7..a7f7991 100755 --- a/scripts/install_config.sh +++ b/scripts/install_config.sh @@ -76,6 +76,8 @@ APPRISE_NOTIFICATION_BODY="A \$sciname \$comname was just detected with a confid APPRISE_NOTIFY_EACH_DETECTION=0 APPRISE_NOTIFY_NEW_SPECIES=0 APPRISE_WEEKLY_REPORT=1 +APPRISE_NOTIFY_NEW_SPECIES_EACH_DAY=0 +APPRISE_MINIMUM_SECONDS_BETWEEN_NOTIFICATIONS_PER_SPECIES=0 #---------------------- Flickr Images API Configuration -----------------------# ## If FLICKR_API_KEY is set, the web interface will try and display bird images diff --git a/scripts/update_birdnet_snippets.sh b/scripts/update_birdnet_snippets.sh index a2c8cea..c5e6ee8 100755 --- a/scripts/update_birdnet_snippets.sh +++ b/scripts/update_birdnet_snippets.sh @@ -47,6 +47,9 @@ fi if ! grep APPRISE_NOTIFY_NEW_SPECIES_EACH_DAY /etc/birdnet/birdnet.conf &>/dev/null;then sudo -u$USER echo "APPRISE_NOTIFY_NEW_SPECIES_EACH_DAY=0 " >> /etc/birdnet/birdnet.conf fi +if ! grep APPRISE_MINIMUM_SECONDS_BETWEEN_NOTIFICATIONS_PER_SPECIES /etc/birdnet/birdnet.conf &>/dev/null;then + sudo -u$USER echo "APPRISE_MINIMUM_SECONDS_BETWEEN_NOTIFICATIONS_PER_SPECIES=0 " >> /etc/birdnet/birdnet.conf +fi # If the config does not contain the DATABASE_LANG setting, we'll want to add it. # Defaults to not-selected, which config.php will know to render as a language option. diff --git a/scripts/utils/notifications.py b/scripts/utils/notifications.py index 8106140..7db8fd4 100644 --- a/scripts/utils/notifications.py +++ b/scripts/utils/notifications.py @@ -4,11 +4,15 @@ import socket import sqlite3 from datetime import datetime import requests +import time userDir = os.path.expanduser('~') APPRISE_CONFIG = userDir + '/BirdNET-Pi/apprise.txt' DB_PATH = userDir + '/BirdNET-Pi/scripts/birds.db' +flickr_images = {} +species_last_notified = {} + def notify(body, title, attached=""): apobj = apprise.Apprise() @@ -37,6 +41,14 @@ def sendAppriseNotifications(species, confidence, path, date, time, week, latitu body = settings_dict.get('APPRISE_NOTIFICATION_BODY') sciName, comName = species.split("_") + print(species_last_notified) + + APPRISE_MINIMUM_SECONDS_BETWEEN_NOTIFICATIONS_PER_SPECIES = settings_dict.get('APPRISE_MINIMUM_SECONDS_BETWEEN_NOTIFICATIONS_PER_SPECIES') + if APPRISE_MINIMUM_SECONDS_BETWEEN_NOTIFICATIONS_PER_SPECIES != "0": + if species_last_notified.get(comName) is not None: + if int(time.time()) - species_last_notified[comName] < int(APPRISE_MINIMUM_SECONDS_BETWEEN_NOTIFICATIONS_PER_SPECIES): + return + try: websiteurl = settings_dict.get('BIRDNETPI_URL') if len(websiteurl) == 0: @@ -46,7 +58,6 @@ def sendAppriseNotifications(species, confidence, path, date, time, week, latitu listenurl = websiteurl+"?filename="+path image_url = "" - flickr_images = {} if len(settings_dict.get('FLICKR_API_KEY')) > 0 and "$flickrimage" in body: if not comName in flickr_images: @@ -93,6 +104,7 @@ def sendAppriseNotifications(species, confidence, path, date, time, week, latitu .replace("$flickrimage", image_url if "{" in body else "")\ .replace("$overlap", overlap) notify(notify_body, notify_title, image_url) + species_last_notified[comName] = int(time.time()) 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": @@ -137,6 +149,7 @@ def sendAppriseNotifications(species, confidence, path, date, time, week, latitu .replace("$overlap", overlap)\ + " (first time today)" notify(notify_body, notify_title, image_url) + species_last_notified[comName] = int(time.time()) con.close() except sqlite3.Error as e: print(e) @@ -184,6 +197,7 @@ def sendAppriseNotifications(species, confidence, path, date, time, week, latitu .replace("$overlap", overlap)\ + " (only seen " + str(int(numberDetections)) + " times in last 7d)" notify(notify_body, notify_title, image_url) + species_last_notified[comName] = int(time.time()) con.close() except sqlite3.Error: print("Database busy") From 167294a41cca30d77cca9236b62d9f9dff8f3a7f Mon Sep 17 00:00:00 2001 From: ehpersonal38 <103586016+ehpersonal38@users.noreply.github.com> Date: Wed, 17 Aug 2022 10:38:01 -0400 Subject: [PATCH 2/3] Update notifications.py --- scripts/utils/notifications.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/scripts/utils/notifications.py b/scripts/utils/notifications.py index 7db8fd4..1374964 100644 --- a/scripts/utils/notifications.py +++ b/scripts/utils/notifications.py @@ -4,7 +4,7 @@ import socket import sqlite3 from datetime import datetime import requests -import time +import time as timeim userDir = os.path.expanduser('~') APPRISE_CONFIG = userDir + '/BirdNET-Pi/apprise.txt' @@ -41,12 +41,10 @@ def sendAppriseNotifications(species, confidence, path, date, time, week, latitu body = settings_dict.get('APPRISE_NOTIFICATION_BODY') sciName, comName = species.split("_") - print(species_last_notified) - APPRISE_MINIMUM_SECONDS_BETWEEN_NOTIFICATIONS_PER_SPECIES = settings_dict.get('APPRISE_MINIMUM_SECONDS_BETWEEN_NOTIFICATIONS_PER_SPECIES') if APPRISE_MINIMUM_SECONDS_BETWEEN_NOTIFICATIONS_PER_SPECIES != "0": if species_last_notified.get(comName) is not None: - if int(time.time()) - species_last_notified[comName] < int(APPRISE_MINIMUM_SECONDS_BETWEEN_NOTIFICATIONS_PER_SPECIES): + if int(timeim.time()) - species_last_notified[comName] < int(APPRISE_MINIMUM_SECONDS_BETWEEN_NOTIFICATIONS_PER_SPECIES): return try: @@ -104,7 +102,7 @@ def sendAppriseNotifications(species, confidence, path, date, time, week, latitu .replace("$flickrimage", image_url if "{" in body else "")\ .replace("$overlap", overlap) notify(notify_body, notify_title, image_url) - species_last_notified[comName] = int(time.time()) + species_last_notified[comName] = int(timeim.time()) 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": @@ -149,7 +147,7 @@ def sendAppriseNotifications(species, confidence, path, date, time, week, latitu .replace("$overlap", overlap)\ + " (first time today)" notify(notify_body, notify_title, image_url) - species_last_notified[comName] = int(time.time()) + species_last_notified[comName] = int(timeim.time()) con.close() except sqlite3.Error as e: print(e) @@ -197,7 +195,7 @@ def sendAppriseNotifications(species, confidence, path, date, time, week, latitu .replace("$overlap", overlap)\ + " (only seen " + str(int(numberDetections)) + " times in last 7d)" notify(notify_body, notify_title, image_url) - species_last_notified[comName] = int(time.time()) + species_last_notified[comName] = int(timeim.time()) con.close() except sqlite3.Error: print("Database busy") From be63d09e621f53fb4310147ed08c1c4480d378d3 Mon Sep 17 00:00:00 2001 From: ehpersonal38 <103586016+ehpersonal38@users.noreply.github.com> Date: Wed, 17 Aug 2022 10:48:28 -0400 Subject: [PATCH 3/3] Update config.php --- scripts/config.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/scripts/config.php b/scripts/config.php index d55c2d0..c581528 100644 --- a/scripts/config.php +++ b/scripts/config.php @@ -26,6 +26,7 @@ if(isset($_GET["latitude"])){ $apprise_input = $_GET['apprise_input']; $apprise_notification_title = $_GET['apprise_notification_title']; $apprise_notification_body = $_GET['apprise_notification_body']; + $minimum_time_limit = $_GET['minimum_time_limit']; $flickr_api_key = $_GET['flickr_api_key']; $flickr_filter_email = $_GET["flickr_filter_email"]; $language = $_GET["language"]; @@ -110,6 +111,7 @@ if(isset($_GET["latitude"])){ $contents = preg_replace("/FLICKR_API_KEY=.*/", "FLICKR_API_KEY=$flickr_api_key", $contents); $contents = preg_replace("/DATABASE_LANG=.*/", "DATABASE_LANG=$language", $contents); $contents = preg_replace("/FLICKR_FILTER_EMAIL=.*/", "FLICKR_FILTER_EMAIL=$flickr_filter_email", $contents); + $contents = preg_replace("/APPRISE_MINIMUM_SECONDS_BETWEEN_NOTIFICATIONS_PER_SPECIES=.*/", "APPRISE_MINIMUM_SECONDS_BETWEEN_NOTIFICATIONS_PER_SPECIES=$minimum_time_limit", $contents); $contents2 = file_get_contents("./scripts/thisrun.txt"); $contents2 = preg_replace("/LATITUDE=.*/", "LATITUDE=$latitude", $contents2); @@ -124,6 +126,7 @@ if(isset($_GET["latitude"])){ $contents2 = preg_replace("/FLICKR_API_KEY=.*/", "FLICKR_API_KEY=$flickr_api_key", $contents2); $contents2 = preg_replace("/DATABASE_LANG=.*/", "DATABASE_LANG=$language", $contents2); $contents2 = preg_replace("/FLICKR_FILTER_EMAIL=.*/", "FLICKR_FILTER_EMAIL=$flickr_filter_email", $contents2); + $contents2 = preg_replace("/APPRISE_MINIMUM_SECONDS_BETWEEN_NOTIFICATIONS_PER_SPECIES=.*/", "APPRISE_MINIMUM_SECONDS_BETWEEN_NOTIFICATIONS_PER_SPECIES=$minimum_time_limit", $contents2); $fh = fopen("/etc/birdnet/birdnet.conf", "w"); @@ -353,7 +356,13 @@ https://discordapp.com/api/webhooks/{WebhookID}/{WebhookToken} >
> -

+
+ +
+ +
+ +