fix: don't send unsolicited stuff: this might break json

This commit is contained in:
frederik
2024-03-12 18:06:56 +01:00
parent 6c6247cf19
commit 3246b61c41
4 changed files with 33 additions and 89 deletions
+4
View File
@@ -307,6 +307,7 @@ if(isset($_GET['sendtest']) && $_GET['sendtest'] == "true") {
$title = str_replace("\$sens", $sens, $title);
$title = str_replace("\$overlap", $overlap, $title);
$title = str_replace("\$flickrimage", $exampleimage, $title);
$title = str_replace("\$reason", 'Test message', $title);
$body = str_replace("\$sciname", $sciname, $body);
$body = str_replace("\$comname", $comname, $body);
@@ -322,6 +323,7 @@ if(isset($_GET['sendtest']) && $_GET['sendtest'] == "true") {
$body = str_replace("\$sens", $sens, $body);
$body = str_replace("\$overlap", $overlap, $body);
$body = str_replace("\$flickrimage", $exampleimage, $body);
$body = str_replace("\$reason", 'Test message', $body);
$temp = tmpfile();
$tpath = stream_get_meta_data($temp)['uri'];
@@ -565,6 +567,8 @@ https://discordapp.com/api/webhooks/{WebhookID}/{WebhookToken}
<dd>Overlap set in "Advanced Settings"</dd>
<dt>$flickrimage</dt>
<dd>A preview image of the detected species from Flickr. Set your API key below.</dd>
<dt>$reason</dt>
<dd>The reason a message was sent</dd>
</dl>
<p>Use the variables defined above to customize your notification title and body.</p>
<label for="apprise_notification_title">Notification Title: </label>
+1 -1
View File
@@ -100,7 +100,7 @@ RTSP_STREAM_TO_LIVESTREAM="0"
#----------------------- Apprise Miscellanous Configuration -------------------#
APPRISE_NOTIFICATION_TITLE="New BirdNET-Pi Detection"
APPRISE_NOTIFICATION_BODY="A \$comname (\$sciname) was just detected with a confidence of \$confidence"
APPRISE_NOTIFICATION_BODY="A \$comname (\$sciname) was just detected with a confidence of \$confidence (\$reason)"
APPRISE_NOTIFY_EACH_DETECTION=0
APPRISE_NOTIFY_NEW_SPECIES=0
APPRISE_WEEKLY_REPORT=1
+4
View File
@@ -69,6 +69,10 @@ if ! grep -E '^RTSP_STREAM_TO_LIVESTREAM=' /etc/birdnet/birdnet.conf &>/dev/null
echo "RTSP_STREAM_TO_LIVESTREAM=\"0\"" >> /etc/birdnet/birdnet.conf
fi
SRC='^APPRISE_NOTIFICATION_BODY="A \$comname \(\$sciname\) was just detected with a confidence of \$confidence"$'
DST='APPRISE_NOTIFICATION_BODY="A \$comname (\$sciname) was just detected with a confidence of \$confidence (\$reason)"'
sed -i -E "s/$SRC/$DST/" /etc/birdnet/birdnet.conf
[ -d $RECS_DIR/StreamData ] || sudo_with_user mkdir -p $RECS_DIR/StreamData
[ -L ${EXTRACTED}/spectrogram.png ] || sudo_with_user ln -sf ${RECS_DIR}/StreamData/spectrogram.png ${EXTRACTED}/spectrogram.png
+24 -88
View File
@@ -43,6 +43,23 @@ def notify(body, title, attached=""):
def sendAppriseNotifications(species, confidence, confidencepct, path,
date, time, week, latitude, longitude, cutoff,
sens, overlap, settings_dict, db_path=DB_PATH):
def render_template(template, reason=""):
ret = template.replace("$sciname", sciName) \
.replace("$comname", comName) \
.replace("$confidencepct", confidencepct) \
.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("$flickrimage", image_url if "{" in body else "") \
.replace("$overlap", overlap) \
.replace("$reason", reason)
return ret
# print(sendAppriseNotifications)
# print(settings_dict)
if os.path.exists(APPRISE_CONFIG) and os.path.getsize(APPRISE_CONFIG) > 0:
@@ -103,34 +120,8 @@ def sendAppriseNotifications(species, confidence, confidencepct, path,
image_url = flickr_images[comName]
if settings_dict.get('APPRISE_NOTIFY_EACH_DETECTION') == "1":
notify_body = body.replace("$sciname", sciName)\
.replace("$comname", comName)\
.replace("$confidencepct", confidencepct)\
.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("$flickrimage", image_url if "{" in body else "")\
.replace("$overlap", overlap)
notify_title = title.replace("$sciname", sciName)\
.replace("$comname", comName)\
.replace("$confidencepct", confidencepct)\
.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("$flickrimage", image_url if "{" in body else "")\
.replace("$overlap", overlap)
notify_body = render_template(body, "detection")
notify_title = render_template(title, "detection")
notify(notify_body, notify_title, image_url)
species_last_notified[comName] = int(timeim.time())
@@ -148,36 +139,8 @@ def sendAppriseNotifications(species, confidence, confidencepct, path,
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("$confidencepct", confidencepct)\
.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("$flickrimage", image_url if "{" in body else "")\
.replace("$overlap", overlap)\
+ " (first time today)"
notify_title = title.replace("$sciname", sciName)\
.replace("$comname", comName)\
.replace("$confidencepct", confidencepct)\
.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("$flickrimage", image_url if "{" in body else "")\
.replace("$overlap", overlap)\
+ " (first time today)"
notify_body = render_template(body, "first time today")
notify_title = render_template(title, "first time today")
notify(notify_body, notify_title, image_url)
species_last_notified[comName] = int(timeim.time())
con.close()
@@ -198,36 +161,9 @@ def sendAppriseNotifications(species, confidence, confidencepct, path,
if len(detections):
numberDetections = detections[0]
if numberDetections > 0 and numberDetections <= 5:
notify_body = body.replace("$sciname", sciName)\
.replace("$comname", comName)\
.replace("$confidencepct", confidencepct)\
.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("$flickrimage", image_url if "{" in body else "")\
.replace("$overlap", overlap)\
+ " (only seen " + str(int(numberDetections)) + " times in last 7d)"
notify_title = title.replace("$sciname", sciName)\
.replace("$comname", comName)\
.replace("$confidencepct", confidencepct)\
.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("$flickrimage", image_url if "{" in body else "")\
.replace("$overlap", overlap)\
+ " (only seen " + str(int(numberDetections)) + " times in last 7d)"
reason = f"only seen {numberDetections} times in last 7d"
notify_body = render_template(body, reason)
notify_title = render_template(title, reason)
notify(notify_body, notify_title, image_url)
species_last_notified[comName] = int(timeim.time())
con.close()