update Detection signature
This commit is contained in:
@@ -61,7 +61,7 @@ def get_settings(settings_path='/etc/birdnet/birdnet.conf', force_reload=False):
|
||||
|
||||
|
||||
class Detection:
|
||||
def __init__(self, file_date, start_time, stop_time, species, confidence):
|
||||
def __init__(self, file_date, start_time, stop_time, scientific_name, common_name, confidence):
|
||||
self.start = float(start_time)
|
||||
self.stop = float(stop_time)
|
||||
self.datetime = file_date + datetime.timedelta(seconds=self.start)
|
||||
@@ -71,9 +71,9 @@ class Detection:
|
||||
self.week = self.datetime.isocalendar()[1]
|
||||
self.confidence = round(float(confidence), 4)
|
||||
self.confidence_pct = round(self.confidence * 100)
|
||||
self.species = species
|
||||
self.scientific_name = species.split('_')[0]
|
||||
self.common_name = species.split('_')[1]
|
||||
self.species = scientific_name
|
||||
self.scientific_name = scientific_name
|
||||
self.common_name = common_name
|
||||
self.common_name_safe = self.common_name.replace("'", "").replace(" ", "_")
|
||||
self.file_name_extr = None
|
||||
|
||||
|
||||
@@ -44,12 +44,12 @@ def notify(body, title, attached=""):
|
||||
)
|
||||
|
||||
|
||||
def sendAppriseNotifications(species, confidence, confidencepct, path,
|
||||
def sendAppriseNotifications(sci_name, com_name, 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) \
|
||||
ret = template.replace("$sciname", sci_name) \
|
||||
.replace("$comname", com_name) \
|
||||
.replace("$confidencepct", str(confidencepct)) \
|
||||
.replace("$confidence", str(confidence)) \
|
||||
.replace("$listenurl", listenurl) \
|
||||
@@ -74,23 +74,21 @@ def sendAppriseNotifications(species, confidence, confidencepct, path,
|
||||
f = open(APPRISE_BODY, 'r')
|
||||
body = f.read()
|
||||
|
||||
sciName, comName = species.split("_")
|
||||
|
||||
APPRISE_ONLY_NOTIFY_SPECIES_NAMES = settings_dict.get('APPRISE_ONLY_NOTIFY_SPECIES_NAMES')
|
||||
if APPRISE_ONLY_NOTIFY_SPECIES_NAMES is not None and APPRISE_ONLY_NOTIFY_SPECIES_NAMES.strip() != "":
|
||||
if any(bird.lower().replace(" ", "") in comName.lower().replace(" ", "") for bird in APPRISE_ONLY_NOTIFY_SPECIES_NAMES.split(",")):
|
||||
if any(bird.lower().replace(" ", "") in com_name.lower().replace(" ", "") for bird in APPRISE_ONLY_NOTIFY_SPECIES_NAMES.split(",")):
|
||||
return
|
||||
|
||||
APPRISE_ONLY_NOTIFY_SPECIES_NAMES_2 = settings_dict.get('APPRISE_ONLY_NOTIFY_SPECIES_NAMES_2')
|
||||
if APPRISE_ONLY_NOTIFY_SPECIES_NAMES_2 is not None and APPRISE_ONLY_NOTIFY_SPECIES_NAMES_2.strip() != "":
|
||||
if not any(bird.lower().replace(" ", "") in comName.lower().replace(" ", "") for bird in APPRISE_ONLY_NOTIFY_SPECIES_NAMES_2.split(",")):
|
||||
if not any(bird.lower().replace(" ", "") in com_name.lower().replace(" ", "") for bird in APPRISE_ONLY_NOTIFY_SPECIES_NAMES_2.split(",")):
|
||||
return
|
||||
|
||||
APPRISE_MINIMUM_SECONDS_BETWEEN_NOTIFICATIONS_PER_SPECIES = settings_dict.get('APPRISE_MINIMUM_SECONDS_BETWEEN_NOTIFICATIONS_PER_SPECIES')
|
||||
if APPRISE_MINIMUM_SECONDS_BETWEEN_NOTIFICATIONS_PER_SPECIES != "0":
|
||||
if species_last_notified.get(comName) is not None:
|
||||
if species_last_notified.get(com_name) is not None:
|
||||
try:
|
||||
if int(timeim.time()) - species_last_notified[comName] < int(APPRISE_MINIMUM_SECONDS_BETWEEN_NOTIFICATIONS_PER_SPECIES):
|
||||
if int(timeim.time()) - species_last_notified[com_name] < int(APPRISE_MINIMUM_SECONDS_BETWEEN_NOTIFICATIONS_PER_SPECIES):
|
||||
return
|
||||
except Exception as e:
|
||||
print("APPRISE NOTIFICATION EXCEPTION: "+str(e))
|
||||
@@ -109,39 +107,39 @@ def sendAppriseNotifications(species, confidence, confidencepct, path,
|
||||
image_url = ""
|
||||
|
||||
if "$flickrimage" in body or "$image" in body:
|
||||
if comName not in images:
|
||||
if com_name not in images:
|
||||
try:
|
||||
url = f"http://localhost/api/v1/image/{sciName}"
|
||||
url = f"http://localhost/api/v1/image/{sci_name}"
|
||||
resp = requests.get(url=url, timeout=10).json()
|
||||
images[comName] = resp['data']['image_url']
|
||||
images[com_name] = resp['data']['image_url']
|
||||
except Exception as e:
|
||||
print("IMAGE API ERROR: "+str(e))
|
||||
image_url = images.get(comName, "")
|
||||
image_url = images.get(com_name, "")
|
||||
|
||||
if settings_dict.get('APPRISE_NOTIFY_EACH_DETECTION') == "1":
|
||||
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())
|
||||
species_last_notified[com_name] = int(timeim.time())
|
||||
|
||||
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":
|
||||
numberDetections = get_todays_count_for(db_path, sciName)
|
||||
numberDetections = get_todays_count_for(db_path, sci_name)
|
||||
if 0 < numberDetections <= APPRISE_NOTIFICATION_NEW_SPECIES_DAILY_COUNT_LIMIT:
|
||||
print("send the notification")
|
||||
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())
|
||||
species_last_notified[com_name] = int(timeim.time())
|
||||
|
||||
if settings_dict.get('APPRISE_NOTIFY_NEW_SPECIES') == "1":
|
||||
numberDetections = get_this_weeks_count_for(db_path, sciName)
|
||||
numberDetections = get_this_weeks_count_for(db_path, sci_name)
|
||||
if 0 < numberDetections <= 5:
|
||||
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())
|
||||
species_last_notified[com_name] = int(timeim.time())
|
||||
|
||||
|
||||
def get_todays_count_for(db_path, sci_name):
|
||||
|
||||
@@ -156,7 +156,7 @@ def apprise(file: ParseFileName, detections: [Detection]):
|
||||
# Apprise of detection if not already alerted this run.
|
||||
if detection.species not in species_apprised_this_run:
|
||||
try:
|
||||
sendAppriseNotifications(detection.species, str(detection.confidence), str(detection.confidence_pct),
|
||||
sendAppriseNotifications(detection.scientific_name, detection.common_name, str(detection.confidence), str(detection.confidence_pct),
|
||||
os.path.basename(detection.file_name_extr), detection.date, detection.time, str(detection.week),
|
||||
conf['LATITUDE'], conf['LONGITUDE'], conf['CONFIDENCE'], conf['SENSITIVITY'],
|
||||
conf['OVERLAP'], dict(conf), DB_PATH)
|
||||
|
||||
Reference in New Issue
Block a user