From e66aa4fa902614b47ccb5dd304ba8c51d26855cc Mon Sep 17 00:00:00 2001 From: Alexandre <44178713+alexbelgium@users.noreply.github.com> Date: Tue, 13 Aug 2024 11:42:26 +0200 Subject: [PATCH] Add flickrimage to mqtt https://github.com/alexbelgium/hassio-addons/issues/1516 --- birdnet-pi/rootfs/helpers/birdnet_to_mqtt.py | 28 +++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/birdnet-pi/rootfs/helpers/birdnet_to_mqtt.py b/birdnet-pi/rootfs/helpers/birdnet_to_mqtt.py index a87b62d45..1b6e74963 100644 --- a/birdnet-pi/rootfs/helpers/birdnet_to_mqtt.py +++ b/birdnet-pi/rootfs/helpers/birdnet_to_mqtt.py @@ -10,7 +10,7 @@ import time import re -import dateparser +import dateparser import datetime import json import logging @@ -85,7 +85,7 @@ mqttc.loop_start() # call the generator function and process each line that is returned for row in file_row_generator(syslog): # bird found above confidence level found, process it - if re_high_clean.search(row) : + if re_high_clean.search(row) : # this slacker regular expression work, extracts the data about the bird found from the log line # I do the parse in two passes, because I did not know the re to do it in one! @@ -112,10 +112,32 @@ for row in file_row_generator(syslog): bird['Confidence'] = high_bird_fields[4] bird['SpeciesCode'] = get_bird_code(high_bird_fields[2]) bird['ClipName'] = high_bird_fields[11] - + # build a url from scientific name of bird that can be used to lookup info about bird bird['url'] = bird_lookup_url_base + high_bird_fields[2].replace(' ', '_') + # Flickimage + image_url = "" + if len(settings_dict.get('FLICKR_API_KEY')) > 0: + if comName not in flickr_images: + try: + headers = {'User-Agent': 'Python_Flickr/1.0'} + url = ('https://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=' + str(settings_dict.get('FLICKR_API_KEY')) + + '&text=' + str(comName) + ' bird&sort=relevance&per_page=5&media=photos&format=json&license=2%2C3%2C4%2C5%2C6%2C9&nojsoncallback=1') + resp = requests.get(url=url, headers=headers, timeout=10) + + resp.encoding = "utf-8" + 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 + except Exception as e: + print("FLICKR API ERROR: "+str(e)) + image_url = "" + else: + image_url = flickr_images[comName] + bird['Image'] = image_url + # convert to json string we can sent to mqtt json_bird = json.dumps(bird)