be a bit more image provider agnostic

This commit is contained in:
frederik
2025-08-26 17:06:45 +02:00
parent 9db52daa8c
commit 29af308d7d
2 changed files with 19 additions and 12 deletions
+13 -7
View File
@@ -224,9 +224,15 @@ if(isset($_GET['sendtest']) && $_GET['sendtest'] == "true") {
$friendlyfilename = "[Listen here](".$filename.")"; $friendlyfilename = "[Listen here](".$filename.")";
$attach=""; $attach="";
$exampleimage = "https://live.staticflickr.com/7430/27545810581_8bfa8289a3_c.jpg"; if ($config["IMAGE_PROVIDER"] === 'FLICKR') {
if (strpos($body, '$flickrimage') !== false) { $image_provider = new Flickr();
$attach = "--attach ".$exampleimage; } else {
$image_provider = new Wikipedia();
}
$exampleimage = htmlspecialchars_decode($image_provider->get_image($sciname)['image_url'], ENT_QUOTES);
if (strpos($body, '$image') !== false) {
$attach = "--attach '$exampleimage'";
} }
if (strpos($body, '{') === false) { if (strpos($body, '{') === false) {
$exampleimage = ""; $exampleimage = "";
@@ -246,7 +252,7 @@ if(isset($_GET['sendtest']) && $_GET['sendtest'] == "true") {
$title = str_replace("\$cutoff", $cutoff, $title); $title = str_replace("\$cutoff", $cutoff, $title);
$title = str_replace("\$sens", $sens, $title); $title = str_replace("\$sens", $sens, $title);
$title = str_replace("\$overlap", $overlap, $title); $title = str_replace("\$overlap", $overlap, $title);
$title = str_replace("\$flickrimage", $exampleimage, $title); $title = str_replace("\$image", $exampleimage, $title);
$title = str_replace("\$reason", 'Test message', $title); $title = str_replace("\$reason", 'Test message', $title);
$body = str_replace("\$sciname", $sciname, $body); $body = str_replace("\$sciname", $sciname, $body);
@@ -263,7 +269,7 @@ if(isset($_GET['sendtest']) && $_GET['sendtest'] == "true") {
$body = str_replace("\$cutoff", $cutoff, $body); $body = str_replace("\$cutoff", $cutoff, $body);
$body = str_replace("\$sens", $sens, $body); $body = str_replace("\$sens", $sens, $body);
$body = str_replace("\$overlap", $overlap, $body); $body = str_replace("\$overlap", $overlap, $body);
$body = str_replace("\$flickrimage", $exampleimage, $body); $body = str_replace("\$image", $exampleimage, $body);
$body = str_replace("\$reason", 'Test message', $body); $body = str_replace("\$reason", 'Test message', $body);
$temp = tmpfile(); $temp = tmpfile();
@@ -522,8 +528,8 @@ https://discordapp.com/api/webhooks/{WebhookID}/{WebhookToken}
<dd>Sigmoid Sensitivity set in "Advanced Settings"</dd> <dd>Sigmoid Sensitivity set in "Advanced Settings"</dd>
<dt>$overlap</dt> <dt>$overlap</dt>
<dd>Overlap set in "Advanced Settings"</dd> <dd>Overlap set in "Advanced Settings"</dd>
<dt>$flickrimage</dt> <dt>$image</dt>
<dd>A preview image of the detected species from Flickr. Set your API key below.</dd> <dd>An image of the detected species from a photo source, see below.</dd>
<dt>$reason</dt> <dt>$reason</dt>
<dd>The reason a notification was sent</dd> <dd>The reason a notification was sent</dd>
</dl> </dl>
+6 -5
View File
@@ -11,7 +11,7 @@ userDir = os.path.expanduser('~')
APPRISE_CONFIG = userDir + '/BirdNET-Pi/apprise.txt' APPRISE_CONFIG = userDir + '/BirdNET-Pi/apprise.txt'
DB_PATH = userDir + '/BirdNET-Pi/scripts/birds.db' DB_PATH = userDir + '/BirdNET-Pi/scripts/birds.db'
flickr_images = {} images = {}
species_last_notified = {} species_last_notified = {}
@@ -59,6 +59,7 @@ def sendAppriseNotifications(species, confidence, confidencepct, path,
.replace("$cutoff", cutoff) \ .replace("$cutoff", cutoff) \
.replace("$sens", sens) \ .replace("$sens", sens) \
.replace("$flickrimage", image_url if "{" in body else "") \ .replace("$flickrimage", image_url if "{" in body else "") \
.replace("$image", image_url if "{" in body else "") \
.replace("$overlap", overlap) \ .replace("$overlap", overlap) \
.replace("$reason", reason) .replace("$reason", reason)
return ret return ret
@@ -102,17 +103,17 @@ def sendAppriseNotifications(species, confidence, confidencepct, path,
friendlyurl = "[Listen here]("+listenurl+")" friendlyurl = "[Listen here]("+listenurl+")"
image_url = "" image_url = ""
if "$flickrimage" in body: if "$flickrimage" in body or "$image" in body:
if comName not in flickr_images: if comName not in images:
try: try:
url = f"http://localhost/api/v1/image/{sciName}" url = f"http://localhost/api/v1/image/{sciName}"
resp = requests.get(url=url, timeout=10).json() resp = requests.get(url=url, timeout=10).json()
flickr_images[comName] = resp['data']['image_url'] images[comName] = resp['data']['image_url']
except Exception as e: except Exception as e:
print("FLICKR API ERROR: "+str(e)) print("FLICKR API ERROR: "+str(e))
image_url = "" image_url = ""
else: else:
image_url = flickr_images[comName] image_url = images[comName]
if settings_dict.get('APPRISE_NOTIFY_EACH_DETECTION') == "1": if settings_dict.get('APPRISE_NOTIFY_EACH_DETECTION') == "1":
notify_body = render_template(body, "detection") notify_body = render_template(body, "detection")