Skip to content

A live bird collage card for your Home Assistant dashboard.

#410 Flickr image in notifications

ehpersonal3898130fa3958d
diff --git a/scripts/config.php b/scripts/config.php
index 2aafa7f7..d95a9fc5 100644
--- a/scripts/config.php
+++ b/scripts/config.php
@@ -327,6 +327,8 @@ https://discordapp.com/api/webhooks/{WebhookID}/{WebhookToken}
<dd>Sigmoid Sensitivity set in "Advanced Settings"</dd>
<dt>$overlap</dt>
<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>
</dl>
<p>Use the variables defined above to customize your notification title and body.</p>
<label for="apprise_notification_title">Notification Title: </label>
diff --git a/scripts/utils/notifications.py b/scripts/utils/notifications.py
index 06dbd105..dfed537f 100644
--- a/scripts/utils/notifications.py
+++ b/scripts/utils/notifications.py
@@ -3,21 +3,29 @@ import os
import socket
import sqlite3
from datetime import datetime
+import requests
userDir = os.path.expanduser('~')
APPRISE_CONFIG = userDir + '/BirdNET-Pi/apprise.txt'
DB_PATH = userDir + '/BirdNET-Pi/scripts/birds.db'
-def notify(body, title):
+def notify(body, title, attached=None):
apobj = apprise.Apprise()
config = apprise.AppriseConfig()
config.add(APPRISE_CONFIG)
apobj.add(config)
- apobj.notify(
- body=body,
- title=title,
- )
+ if attached is not None:
+ apobj.notify(
+ body=body,
+ title=title,
+ attach=attached,
+ )
+ else:
+ apobj.notify(
+ body=body,
+ title=title,
+ )
def sendAppriseNotifications(species, confidence, path, date, time, week, latitude, longitude, cutoff, sens, overlap, settings_dict, db_path=DB_PATH):
@@ -37,6 +45,21 @@ def sendAppriseNotifications(species, confidence, path, date, time, week, latitu
websiteurl = "http://"+socket.gethostname()+".local"
listenurl = websiteurl+"?filename="+path
+ image_url = None
+ flickr_images = {}
+
+ if len(settings_dict.get('FLICKR_API_KEY')) > 0 and "$flickrimage" in body:
+ if not comName in flickr_images:
+ # TODO: Make this work with non-english comnames. Implement the "// convert sci name to English name" logic from overview.php here
+ url = 'https://www.flickr.com/services/rest/?method=flickr.photos.search&api_key='+str(settings_dict.get('FLICKR_API_KEY'))+'&text='+str(comName)+'&sort=relevance&per_page=5&media=photos&format=json&license=2%2C3%2C4%2C5%2C6%2C9&nojsoncallback=1'
+ resp = requests.get(url=url)
+ data = resp.json()["photos"]["photo"][0]
+
+ image_url = 'https://farm'+str(data["farm"])+'.static.flickr.com/'+str(data["server"])+'/'+str(data["id"])+'_'+str(data["secret"])+'_n.jpg'
+ flickr_images[comName] = image_url
+ else:
+ image_url = flickr_images[comName]
+
if settings_dict.get('APPRISE_NOTIFY_EACH_DETECTION') == "1":
notify_body = body.replace("$sciname", sciName)\
@@ -50,6 +73,7 @@ def sendAppriseNotifications(species, confidence, path, date, time, week, latitu
.replace("$longitude", longitude)\
.replace("$cutoff", cutoff)\
.replace("$sens", sens)\
+ .replace("$flickrimage", "")\
.replace("$overlap", overlap)
notify_title = title.replace("$sciname", sciName)\
.replace("$comname", comName)\
@@ -62,8 +86,9 @@ def sendAppriseNotifications(species, confidence, path, date, time, week, latitu
.replace("$longitude", longitude)\
.replace("$cutoff", cutoff)\
.replace("$sens", sens)\
+ .replace("$flickrimage", "")\
.replace("$overlap", overlap)
- notify(notify_body, notify_title)
+ notify(notify_body, notify_title, image_url)
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":
@@ -90,6 +115,7 @@ def sendAppriseNotifications(species, confidence, path, date, time, week, latitu
.replace("$longitude", longitude)\
.replace("$cutoff", cutoff)\
.replace("$sens", sens)\
+ .replace("$flickrimage", "")\
.replace("$overlap", overlap)\
+ " (first time today)"
notify_title = title.replace("$sciname", sciName)\
@@ -103,9 +129,10 @@ def sendAppriseNotifications(species, confidence, path, date, time, week, latitu
.replace("$longitude", longitude)\
.replace("$cutoff", cutoff)\
.replace("$sens", sens)\
+ .replace("$flickrimage", "")\
.replace("$overlap", overlap)\
+ " (first time today)"
- notify(notify_body, notify_title)
+ notify(notify_body, notify_title, image_url)
con.close()
except sqlite3.Error as e:
print(e)
@@ -135,6 +162,7 @@ def sendAppriseNotifications(species, confidence, path, date, time, week, latitu
.replace("$longitude", longitude)\
.replace("$cutoff", cutoff)\
.replace("$sens", sens)\
+ .replace("$flickrimage", "")\
.replace("$overlap", overlap)\
+ " (only seen " + str(int(numberDetections)) + " times in last 7d)"
notify_title = title.replace("$sciname", sciName)\
@@ -148,9 +176,10 @@ def sendAppriseNotifications(species, confidence, path, date, time, week, latitu
.replace("$longitude", longitude)\
.replace("$cutoff", cutoff)\
.replace("$sens", sens)\
+ .replace("$flickrimage", "")\
.replace("$overlap", overlap)\
+ " (only seen " + str(int(numberDetections)) + " times in last 7d)"
- notify(notify_body, notify_title)
+ notify(notify_body, notify_title, image_url)
con.close()
except sqlite3.Error:
print("Database busy")