Merge pull request #261 from mcguirepr89/notifications

Apprise Notifications, addresses #228
This commit is contained in:
ehpersonal38
2022-05-10 09:08:06 -04:00
committed by GitHub
8 changed files with 100 additions and 39 deletions
+4 -10
View File
@@ -56,17 +56,11 @@ BIRDNETPI_URL=
RTSP_STREAM= RTSP_STREAM=
#------------------- Mobile Notifications via Pushed.co ---------------------# #----------------------- Apprise Miscellanous Configuration -------------------#
#____________The two variables below enable mobile notifications_______________#
#_____________See https://pushed.co/quick-start-guide to get___________________#
#_________________________these values for your app.___________________________#
# Keep these EMPTY if haven't setup a Pushed.co App yet. # APPRISE_NOTIFICATION_TITLE="New BirdNET-Pi Detection"
APPRISE_NOTIFICATION_BODY="A \$sciname \$comname was just detected with a confidence of \$confidence"
## Pushed.co App Key and App Secret APPRISE_NOTIFY_EACH_DETECTION=false
PUSHED_APP_KEY=
PUSHED_APP_SECRET=
################################################################################ ################################################################################
#-------------------------------- Defaults ----------------------------------# #-------------------------------- Defaults ----------------------------------#
+1
View File
@@ -10,3 +10,4 @@ pandas
seaborn seaborn
streamlit streamlit
plotly plotly
apprise
+54 -11
View File
@@ -9,26 +9,49 @@ $longitude = $_GET["longitude"];
$birdweather_id = $_GET["birdweather_id"]; $birdweather_id = $_GET["birdweather_id"];
$pushed_app_key = $_GET["pushed_app_key"]; $pushed_app_key = $_GET["pushed_app_key"];
$pushed_app_secret = $_GET["pushed_app_secret"]; $pushed_app_secret = $_GET["pushed_app_secret"];
$apprise_input = $_GET['apprise_input'];
$apprise_notification_title = $_GET['apprise_notification_title'];
$apprise_notification_body = $_GET['apprise_notification_body'];
if(isset($_GET['apprise_notify_each_detection'])) {
$apprise_notify_each_detection = 1;
} else {
$apprise_notify_each_detection = 0;
}
$contents = file_get_contents("/etc/birdnet/birdnet.conf"); $contents = file_get_contents("/etc/birdnet/birdnet.conf");
$contents = preg_replace("/LATITUDE=.*/", "LATITUDE=$latitude", $contents); $contents = preg_replace("/LATITUDE=.*/", "LATITUDE=$latitude", $contents);
$contents = preg_replace("/LONGITUDE=.*/", "LONGITUDE=$longitude", $contents); $contents = preg_replace("/LONGITUDE=.*/", "LONGITUDE=$longitude", $contents);
$contents = preg_replace("/BIRDWEATHER_ID=.*/", "BIRDWEATHER_ID=$birdweather_id", $contents); $contents = preg_replace("/BIRDWEATHER_ID=.*/", "BIRDWEATHER_ID=$birdweather_id", $contents);
$contents = preg_replace("/PUSHED_APP_KEY=.*/", "PUSHED_APP_KEY=$pushed_app_key", $contents); $contents = preg_replace("/APPRISE_NOTIFICATION_TITLE=.*/", "APPRISE_NOTIFICATION_TITLE=\"$apprise_notification_title\"", $contents);
$contents = preg_replace("/PUSHED_APP_SECRET=.*/", "PUSHED_APP_SECRET=$pushed_app_secret", $contents); $contents = preg_replace("/APPRISE_NOTIFICATION_BODY=.*/", "APPRISE_NOTIFICATION_BODY=\"$apprise_notification_body\"", $contents);
$contents = preg_replace("/APPRISE_NOTIFY_EACH_DETECTION=.*/", "APPRISE_NOTIFY_EACH_DETECTION=$apprise_notify_each_detection", $contents);
$contents2 = file_get_contents("./scripts/thisrun.txt"); $contents2 = file_get_contents("./scripts/thisrun.txt");
$contents2 = preg_replace("/LATITUDE=.*/", "LATITUDE=$latitude", $contents2); $contents2 = preg_replace("/LATITUDE=.*/", "LATITUDE=$latitude", $contents2);
$contents2 = preg_replace("/LONGITUDE=.*/", "LONGITUDE=$longitude", $contents2); $contents2 = preg_replace("/LONGITUDE=.*/", "LONGITUDE=$longitude", $contents2);
$contents2 = preg_replace("/BIRDWEATHER_ID=.*/", "BIRDWEATHER_ID=$birdweather_id", $contents2); $contents2 = preg_replace("/BIRDWEATHER_ID=.*/", "BIRDWEATHER_ID=$birdweather_id", $contents2);
$contents2 = preg_replace("/PUSHED_APP_KEY=.*/", "PUSHED_APP_KEY=$pushed_app_key", $contents2); $contents2 = preg_replace("/APPRISE_NOTIFICATION_TITLE=.*/", "APPRISE_NOTIFICATION_TITLE=\"$apprise_notification_title\"", $contents2);
$contents2 = preg_replace("/PUSHED_APP_SECRET=.*/", "PUSHED_APP_SECRET=$pushed_app_secret", $contents2); $contents2 = preg_replace("/APPRISE_NOTIFICATION_BODY=.*/", "APPRISE_NOTIFICATION_BODY=\"$apprise_notification_body\"", $contents2);
$contents2 = preg_replace("/APPRISE_NOTIFY_EACH_DETECTION=.*/", "APPRISE_NOTIFY_EACH_DETECTION=$apprise_notify_each_detection", $contents2);
$fh = fopen("/etc/birdnet/birdnet.conf", "w"); $fh = fopen("/etc/birdnet/birdnet.conf", "w");
$fh2 = fopen("./scripts/thisrun.txt", "w"); $fh2 = fopen("./scripts/thisrun.txt", "w");
fwrite($fh, $contents); fwrite($fh, $contents);
fwrite($fh2, $contents2); fwrite($fh2, $contents2);
if(strlen($apprise_input) > 0){
$user = shell_exec("awk -F: '/1000/{print $1}' /etc/passwd");
$home = shell_exec("awk -F: '/1000/{print $6}' /etc/passwd");
$home = trim($home);
$appriseconfig = fopen($home."/BirdNET-Pi/apprise.txt", "w");
fwrite($appriseconfig, $apprise_input);
}
$language = $_GET["language"]; $language = $_GET["language"];
if ($language != "none"){ if ($language != "none"){
$user = shell_exec("awk -F: '/1000/{print $1}' /etc/passwd"); $user = shell_exec("awk -F: '/1000/{print $1}' /etc/passwd");
@@ -54,6 +77,14 @@ if (file_exists('./scripts/thisrun.txt')) {
} elseif (file_exists('./scripts/firstrun.ini')) { } elseif (file_exists('./scripts/firstrun.ini')) {
$config = parse_ini_file('./scripts/firstrun.ini'); $config = parse_ini_file('./scripts/firstrun.ini');
} }
$user = shell_exec("awk -F: '/1000/{print $1}' /etc/passwd");
$home = shell_exec("awk -F: '/1000/{print $6}' /etc/passwd");
$home = trim($home);
if (file_exists($home."/BirdNET-Pi/apprise.txt")) {
$apprise_config = file_get_contents($home."/BirdNET-Pi/apprise.txt");
} else {
$apprise_config = "";
}
$caddypwd = $config['CADDY_PWD']; $caddypwd = $config['CADDY_PWD'];
if (!isset($_SERVER['PHP_AUTH_USER'])) { if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="My Realm"'); header('WWW-Authenticate: Basic realm="My Realm"');
@@ -78,12 +109,24 @@ if (!isset($_SERVER['PHP_AUTH_USER'])) {
<p>Set your Latitude and Longitude to 4 decimal places. Get your coordinates <a href="https://latlong.net" target="_blank">here</a>.</p> <p>Set your Latitude and Longitude to 4 decimal places. Get your coordinates <a href="https://latlong.net" target="_blank">here</a>.</p>
<label for="birdweather_id">BirdWeather ID: </label> <label for="birdweather_id">BirdWeather ID: </label>
<input name="birdweather_id" type="text" value="<?php print($config['BIRDWEATHER_ID']);?>" /><br> <input name="birdweather_id" type="text" value="<?php print($config['BIRDWEATHER_ID']);?>" /><br>
<p><a href="https://app.birdweather.com" target="_blank">BirdWeather.com</a> is a weather map for bird sounds. Stations around the world supply audio and video streams to BirdWeather where they are then analyzed by BirdNET and compared to eBird Grid data. BirdWeather catalogues the bird audio and spectrogram visualizations so that you can listen to, view, and read about birds throughout the world. <a href="mailto:tim@birdweather.com?subject=Request%20BirdWeather%20ID&body=<?php include('./scripts/birdweather_request.php'); ?>" target="_blank">Email Tim</a> to request a BirdWeather ID</p> <p><a href="https://app.birdweather.com" target="_blank">BirdWeather.com</a> is a weather map for bird sounds. Stations around the world supply audio and video streams to BirdWeather where they are then analyzed by BirdNET and compared to eBird Grid data. BirdWeather catalogues the bird audio and spectrogram visualizations so that you can listen to, view, and read about birds throughout the world. <a href="mailto:tim@birdweather.com?subject=Request%20BirdWeather%20ID&body=<?php include('./scripts/birdweather_request.php'); ?>" target="_blank">Email Tim</a> to request a BirdWeather ID</p><br>
<label for="pushed_app_key">Pushed App Key: </label> <h3>Notifications</h3>
<input name="pushed_app_key" type="text" value="<?php print($config['PUSHED_APP_KEY']);?>" /><br> <p><a target="_blank" href="https://github.com/caronc/apprise/wiki">Apprise Notifications</a> can be setup and enabled for 70+ notification services. Each service should be on its own line.</p>
<label for="pushed_app_secret">Pushed App Secret: </label> <label for="apprise_input">Apprise Notifications Configuration: </label>
<input name="pushed_app_secret" type="text" value="<?php print($config['PUSHED_APP_SECRET']);?>" /><br> <textarea placeholder="mailto://{user}:{password}@gmail.com
<p><a target="_blank" href="https://pushed.co/quick-start-guide">Pushed iOS Notifications</a> can be setup and enabled for New Species notifications. Be sure to "Enable" the "Pushed Notifications" in "Tools" > "Services" if you would like to use this feature. Sorry, Android users, this only works on iOS.</p> tgram://{bot_token}/{chat_id}
twitter://{ConsumerKey}/{ConsumerSecret}/{AccessToken}/{AccessSecret}
https://discordapp.com/api/webhooks/{WebhookID}/{WebhookToken}
..." style="vertical-align: top" name="apprise_input" cols="140" rows="5" type="text" ><?php print($apprise_config);?></textarea><br><br>
<label for="apprise_notification_title">Notification Title: </label>
<input name="apprise_notification_title" type="text" value="<?php print($config['APPRISE_NOTIFICATION_TITLE']);?>" /><br>
<label for="apprise_notification_body">Notification Body (use variables $sciname, $comname, or $confidence): </label>
<input name="apprise_notification_body" type="text" value="<?php print($config['APPRISE_NOTIFICATION_BODY']);?>" /><br>
<input type="checkbox" name="apprise_notify_each_species" value="true" disabled checked>
<label for="apprise_notify_each_species">Notify each new species</label><br>
<input type="checkbox" name="apprise_notify_each_detection" <?php if($config['APPRISE_NOTIFY_EACH_DETECTION'] == 1) { echo "checked"; };?> >
<label for="apprise_notify_each_detection">Notify each new detection</label><br><br>
<h3>Localization</h3>
<label for="language">Database Language: </label> <label for="language">Database Language: </label>
<select name="language"> <select name="language">
<option value="none">Select your language</option> <option value="none">Select your language</option>
@@ -120,7 +163,7 @@ if (!isset($_SERVER['PHP_AUTH_USER'])) {
<br><br> <br><br>
<input type="hidden" name="status" value="success"> <input type="hidden" name="status" value="success">
<input type="hidden" name="submit" value="settings"> <input type="hidden" name="submit" value="settings">
<button type="submit" name="view" value="Settings"> <button onclick="if(Boolean(Number(<?php print($config['APPRISE_NOTIFY_EACH_DETECTION']); ?>)) != document.getElementsByName('apprise_notify_each_detection')[0].checked) type="submit" name="view" value="Settings">
<?php <?php
if(isset($_GET['status'])){ if(isset($_GET['status'])){
echo "Success!"; echo "Success!";
+4 -10
View File
@@ -69,17 +69,11 @@ BIRDNETPI_URL=
RTSP_STREAM= RTSP_STREAM=
#------------------- Mobile Notifications via Pushed.co ---------------------# #----------------------- Apprise Miscellanous Configuration -------------------#
#____________The two variables below enable mobile notifications_______________#
#_____________See https://pushed.co/quick-start-guide to get___________________#
#_________________________these values for your app.___________________________#
# Keep these EMPTY if haven't setup a Pushed.co App yet. # APPRISE_NOTIFICATION_TITLE="New BirdNET-Pi Detection"
APPRISE_NOTIFICATION_BODY="A \$sciname \$comname was just detected with a confidence of \$confidence"
## Pushed.co App Key and App Secret APPRISE_NOTIFY_EACH_DETECTION=false
PUSHED_APP_KEY=
PUSHED_APP_SECRET=
################################################################################ ################################################################################
#-------------------------------- Defaults ----------------------------------# #-------------------------------- Defaults ----------------------------------#
+19
View File
@@ -24,6 +24,7 @@ from time import sleep
import pytz import pytz
from tzlocal import get_localzone from tzlocal import get_localzone
from pathlib import Path from pathlib import Path
import apprise
HEADER = 64 HEADER = 64
@@ -223,7 +224,24 @@ def analyzeAudioData(chunks, lat, lon, week, sensitivity, overlap,):
# print('DETECTIONS:::::',detections) # print('DETECTIONS:::::',detections)
return detections return detections
def sendAppriseNotifications(species,confidence):
if os.path.getsize(userDir + '/BirdNET-Pi/apprise.txt') > 0:
with open(userDir + '/BirdNET-Pi/scripts/thisrun.txt', 'r') as f:
this_run = f.readlines()
title = str(str(str([i for i in this_run if i.startswith('APPRISE_NOTIFICATION_TITLE')]).split('=')[1]).split('\\')[0]).replace('"', '')
body = str(str(str([i for i in this_run if i.startswith('APPRISE_NOTIFICATION_BODY')]).split('=')[1]).split('\\')[0]).replace('"', '')
if str(str(str([i for i in this_run if i.startswith('APPRISE_NOTIFY_EACH_DETECTION')]).split('=')[1]).split('\\')[0]) == "1":
apobj = apprise.Apprise()
config = apprise.AppriseConfig()
config.add(userDir + '/BirdNET-Pi/apprise.txt')
apobj.add(config)
apobj.notify(
body=body.replace("$sciname",species.split("_")[0]).replace("$comname",species.split("_")[1]).replace("$confidence",confidence),
title=title,
)
def writeResultsToFile(detections, min_conf, path): def writeResultsToFile(detections, min_conf, path):
@@ -234,6 +252,7 @@ def writeResultsToFile(detections, min_conf, path):
for d in detections: for d in detections:
for entry in detections[d]: for entry in detections[d]:
if entry[1] >= min_conf and ((entry[0] in INCLUDE_LIST or len(INCLUDE_LIST) == 0) and (entry[0] not in EXCLUDE_LIST or len(EXCLUDE_LIST) == 0) ): if entry[1] >= min_conf and ((entry[0] in INCLUDE_LIST or len(INCLUDE_LIST) == 0) and (entry[0] not in EXCLUDE_LIST or len(EXCLUDE_LIST) == 0) ):
sendAppriseNotifications(str(entry[0]),str(entry[1]));
rfile.write(d + ';' + entry[0].replace('_', ';') + ';' + str(entry[1]) + '\n') rfile.write(d + ';' + entry[0].replace('_', ';') + ';' + str(entry[1]) + '\n')
rcnt += 1 rcnt += 1
print('DONE! WROTE', rcnt, 'RESULTS.') print('DONE! WROTE', rcnt, 'RESULTS.')
+2 -8
View File
@@ -1,6 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Sends a notification if a new species is detected # Sends a notification if a new species is detected
#set -x
trap 'rm -f $lastcheck' EXIT trap 'rm -f $lastcheck' EXIT
source /etc/birdnet/birdnet.conf source /etc/birdnet/birdnet.conf
@@ -21,13 +20,8 @@ if ! diff ${IDFILE} ${lastcheck} &> /dev/null;then
echo "Sending the following notification: echo "Sending the following notification:
${NOTIFICATION}" ${NOTIFICATION}"
if [ ! -z ${PUSHED_APP_KEY} ];then if [ ! -s $HOME/BirdNET-Pi/apprise.txt ];then
curl -X POST \ $HOME/BirdNET-Pi/birdnet/bin/apprise -vv -t 'New Species Detected' -b "${NOTIFICATION}" --config=$HOME/BirdNET-Pi/apprise.txt
--form-string "app_key=${PUSHED_APP_KEY}" \
--form-string "app_secret=${PUSHED_APP_SECRET}" \
--form-string "target_type=app" \
--form-string "content=${NOTIFICATION}" \
https://api.pushed.co/1/push
fi fi
fi fi
+14
View File
@@ -19,6 +19,20 @@ if grep privacy ~/BirdNET-Pi/templates/birdnet_server.service &>/dev/null;then
sudo systemctl daemon-reload sudo systemctl daemon-reload
restart_services.sh restart_services.sh
fi fi
if ! grep APPRISE_NOTIFICATION_TITLE /etc/birdnet/birdnet.conf &>/dev/null;then
sudo -u$USER echo "APPRISE_NOTIFICATION_TITLE=\"New BirdNET-Pi Detection\"" >> /etc/birdnet/birdnet.conf
fi
if ! grep APPRISE_NOTIFICATION_BODY /etc/birdnet/birdnet.conf &>/dev/null;then
sudo -u$USER echo "APPRISE_NOTIFICATION_BODY=\"A \$sciname \$comname was just detected with a confidence of \$confidence\"" >> /etc/birdnet/birdnet.conf
fi
if ! grep APPRISE_NOTIFY_EACH_DETECTION /etc/birdnet/birdnet.conf &>/dev/null;then
sudo -u$USER echo "APPRISE_NOTIFY_EACH_DETECTION=false" >> /etc/birdnet/birdnet.conf
fi
if ! which lsof &>/dev/null;then if ! which lsof &>/dev/null;then
sudo apt update && sudo apt -y install lsof sudo apt update && sudo apt -y install lsof
fi fi
apprise_installation_status=$(~/BirdNET-Pi/birdnet/bin/python3 -c 'import pkgutil; print("installed" if pkgutil.find_loader("apprise") else "not installed")')
if [[ "$apprise_installation_status" = "not installed" ]];then
~/BirdNET-Pi/birdnet/bin/pip3 install -U pip
~/BirdNET-Pi/birdnet/bin/pip3 install apprise
fi
+2
View File
@@ -2,4 +2,6 @@
# Update the species list # Update the species list
#set -x #set -x
source /etc/birdnet/birdnet.conf source /etc/birdnet/birdnet.conf
if [ -f $HOME/BirdNET-Pi/scripts/birds.db ];then
sqlite3 $HOME/BirdNET-Pi/scripts/birds.db "SELECT DISTINCT(Com_Name) FROM detections" | sort > ${IDFILE} sqlite3 $HOME/BirdNET-Pi/scripts/birds.db "SELECT DISTINCT(Com_Name) FROM detections" | sort > ${IDFILE}
fi