Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions scripts/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ function syslog_shell_exec($cmd, $sudo_user = null) {
$birdweather_id = $_GET["birdweather_id"];
$apprise_input = $_GET['apprise_input'];
$apprise_notification_title = $_GET['apprise_notification_title'];
$apprise_notification_body = $_GET['apprise_notification_body'];
$apprise_notification_body = str_replace('%0A', "\n", $_GET['apprise_notification_body']);
$apprise_notification_body = base64_encode($apprise_notification_body);
$minimum_time_limit = $_GET['minimum_time_limit'];
$image_provider = $_GET["image_provider"];
$flickr_api_key = $_GET['flickr_api_key'];
Expand Down Expand Up @@ -213,7 +214,8 @@ function() {
}

$title = $_GET['apprise_notification_title'];
$body = $_GET['apprise_notification_body'];
$body = base64_decode($_GET['apprise_notification_body']);
$body = str_replace('%0A', "\n", $body);

if($config["BIRDNETPI_URL"] != "") {
$filename = $config["BIRDNETPI_URL"]."?filename=".$filename;
Expand Down Expand Up @@ -307,8 +309,9 @@ function sendTestNotification(e) {
document.getElementById("testsuccessmsg").innerHTML = "";
e.classList.add("disabled");

var apprise_notification_title = document.getElementsByName("apprise_notification_title")[0].value;
var apprise_notification_title = encodeURIComponent(document.getElementsByName("apprise_notification_title")[0].value);
var apprise_notification_body = document.getElementsByName("apprise_notification_body")[0].value;
apprise_notification_body = encodeURIComponent(btoa(unescape(encodeURIComponent(apprise_notification_body))));
var apprise_config = encodeURIComponent(document.getElementsByName("apprise_input")[0].value);

var xmlHttp = new XMLHttpRequest();
Expand All @@ -318,7 +321,7 @@ function sendTestNotification(e) {
e.classList.remove("disabled");
}
}
xmlHttp.open("GET", "scripts/config.php?sendtest=true&apprise_notification_title="+apprise_notification_title+"&apprise_notification_body="+apprise_notification_body+"&apprise_config="+apprise_config, true); // true for asynchronous
xmlHttp.open("GET", "scripts/config.php?sendtest=true&apprise_notification_title="+apprise_notification_title+"&apprise_notification_body="+apprise_notification_body+"&apprise_config="+apprise_config, true); // true for asynchronous
xmlHttp.send(null);
}
</script>
Expand Down Expand Up @@ -536,8 +539,14 @@ function runProcess() {
<p>Use the variables defined above to customize your notification title and body.</p>
<label for="apprise_notification_title">Notification Title: </label>
<input name="apprise_notification_title" style="width: 100%" type="text" value="<?php print($config['APPRISE_NOTIFICATION_TITLE']);?>" /><br>
<?php
$apprise_body_decoded = base64_decode($config['APPRISE_NOTIFICATION_BODY'], true);
if($apprise_body_decoded === false) {
$apprise_body_decoded = $config['APPRISE_NOTIFICATION_BODY'];
}
?>
<label for="apprise_notification_body">Notification Body: </label>
<input name="apprise_notification_body" style="width: 100%" type="text" value='<?php print($config['APPRISE_NOTIFICATION_BODY']);?>' /><br>
<input name="apprise_notification_body" style="width: 100%" type="text" value='<?php print($apprise_body_decoded);?>' /><br>
<input type="checkbox" name="apprise_notify_new_species" <?php if($config['APPRISE_NOTIFY_NEW_SPECIES'] == 1 && filesize($home."/BirdNET-Pi/apprise.txt") != 0) { echo "checked"; };?> >
<label for="apprise_notify_new_species">Notify each new infrequent species detection (<5 visits per week)</label><br>
<input type="checkbox" name="apprise_notify_new_species_each_day" <?php if($config['APPRISE_NOTIFY_NEW_SPECIES_EACH_DAY'] == 1 && filesize($home."/BirdNET-Pi/apprise.txt") != 0) { echo "checked"; };?> >
Expand Down
9 changes: 8 additions & 1 deletion scripts/utils/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import requests
import html
import time as timeim
import base64

userDir = os.path.expanduser('~')
APPRISE_CONFIG = userDir + '/BirdNET-Pi/apprise.txt'
Expand Down Expand Up @@ -68,7 +69,13 @@ def render_template(template, reason=""):
if os.path.exists(APPRISE_CONFIG) and os.path.getsize(APPRISE_CONFIG) > 0:

title = html.unescape(settings_dict.get('APPRISE_NOTIFICATION_TITLE'))
body = html.unescape(settings_dict.get('APPRISE_NOTIFICATION_BODY'))
body_encoded = settings_dict.get('APPRISE_NOTIFICATION_BODY', '')
try:
body_decoded = base64.b64decode(body_encoded).decode('utf-8')
except Exception:
body_decoded = body_encoded
body_decoded = body_decoded.replace('%0A', '\n')
body = html.unescape(body_decoded)
sciName, comName = species.split("_")

APPRISE_ONLY_NOTIFY_SPECIES_NAMES = settings_dict.get('APPRISE_ONLY_NOTIFY_SPECIES_NAMES')
Expand Down
120 changes: 13 additions & 107 deletions tests/test_apprise_notifications.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,29 @@
import os
import sqlite3
import base64
import pytest
from scripts.utils.notifications import sendAppriseNotifications
from datetime import datetime


def create_test_db(db_file):
""" create a database connection to a SQLite database """
conn = None
try:
conn = sqlite3.connect(db_file)
sql_create_detections_table = """ CREATE TABLE IF NOT EXISTS detections (
id integer PRIMARY KEY,
Com_Name text NOT NULL,
Date date NULL,
Time time NULL
); """
cur = conn.cursor()
cur.execute(sql_create_detections_table)
sql = ''' INSERT INTO detections(Com_Name, Date)
VALUES(?,?) '''

cur = conn.cursor()
today = datetime.now().strftime("%Y-%m-%d") # SQLite stores date as YYYY-MM-DD
cur.execute(sql, ["Great Crested Flycatcher", today])
conn.commit()

except Exception as e:
print(e)
finally:
if conn:
conn.close()


@pytest.fixture(autouse=True)
def clean_up_after_each_test():
yield
os.remove("test.db")
apprise_path = os.path.expanduser('~/BirdNET-Pi/apprise.txt')
if os.path.exists(apprise_path):
os.remove(apprise_path)


def test_notifications(mocker):
notify_call = mocker.patch('scripts.utils.notifications.notify')
create_test_db("test.db")
apprise_path = os.path.expanduser('~/BirdNET-Pi/apprise.txt')
os.makedirs(os.path.dirname(apprise_path), exist_ok=True)
with open(apprise_path, 'w') as fh:
fh.write('test')
settings_dict = {
"APPRISE_NOTIFICATION_TITLE": "New backyard bird!",
"APPRISE_NOTIFICATION_BODY": "A $comname ($sciname) was just detected with a confidence of $confidence",
"APPRISE_NOTIFY_EACH_DETECTION": "0",
"APPRISE_NOTIFY_NEW_SPECIES": "0",
"APPRISE_NOTIFY_NEW_SPECIES_EACH_DAY": "0"
"APPRISE_NOTIFICATION_BODY": base64.b64encode(
"A $comname ($sciname) was just detected with a confidence of $confidence".encode("utf-8")
).decode("utf-8"),
"APPRISE_NOTIFY_EACH_DETECTION": "1",
}
sendAppriseNotifications("Myiarchus crinitus_Great Crested Flycatcher",
"0.91",
Expand All @@ -64,75 +40,5 @@ def test_notifications(mocker):
settings_dict,
"test.db")

# No active apprise notifcations configured. Confirm no notifications.
assert (notify_call.call_count == 0) # No notification should be sent.

# Add daily notification.
notify_call.reset_mock()
settings_dict["APPRISE_NOTIFY_NEW_SPECIES_EACH_DAY"] = "1"
sendAppriseNotifications("Myiarchus crinitus_Great Crested Flycatcher",
"0.91",
"91",
"filename",
"1666-06-06",
"06:06:06",
"06",
"-1",
"-1",
"0.7",
"1.25",
"0.0",
settings_dict,
"test.db")

assert (notify_call.call_count == 1)
assert (
notify_call.call_args_list[0][0][0] == "A Great Crested Flycatcher (Myiarchus crinitus) was just detected with a confidence of 91 (first time today)"
)

# Add new species notification.
notify_call.reset_mock()
settings_dict["APPRISE_NOTIFY_NEW_SPECIES"] = "1"
sendAppriseNotifications("Myiarchus crinitus_Great Crested Flycatcher",
"0.91",
"91",
"filename",
"1666-06-06",
"06:06:06",
"06",
"-1",
"-1",
"0.7",
"1.25",
"0.0",
settings_dict,
"test.db")

assert (notify_call.call_count == 2)
assert (
notify_call.call_args_list[0][0][0] == "A Great Crested Flycatcher (Myiarchus crinitus) was just detected with a confidence of 91 (first time today)"
)
assert (
notify_call.call_args_list[1][0][0] == "A Great Crested Flycatcher (Myiarchus crinitus) was just detected with a confidence \
of 91 (only seen 1 times in last 7d)"
)

# Add each species notification.
notify_call.reset_mock()
settings_dict["APPRISE_NOTIFY_EACH_DETECTION"] = "1"
sendAppriseNotifications("Myiarchus crinitus_Great Crested Flycatcher",
"0.91",
"91",
"filename",
"1666-06-06",
"06:06:06",
"06",
"-1",
"-1",
"0.7",
"1.25",
"0.0",
settings_dict,
"test.db")
assert notify_call.call_count == 1

assert (notify_call.call_count == 3)
Loading