Update changeidenfication.sh

This commit is contained in:
Alexandre
2024-05-03 10:20:37 +02:00
committed by GitHub
parent 7cf0367dd9
commit 1989fbf0a7

View File

@@ -1,31 +1,46 @@
#!/bin/bash
# Check if there are at least 2 arguments
if [ $$ -lt 2 ]; then
echo "This script requires at least two arguments!"
exit 1
fi
# This scripts allows to change the identification of a Birdnet-pi detection
set +u #avoid error with apprise variables
#################
# SET VARIABLES #
#################
# shellcheck disable=SC1091
source /etc/birdnet/birdnet.conf
#OLDNAME="Mésange_charbonnière-78-2024-05-02-birdnet-RTSP_1-18:14:08.mp3"
#NEWNAME="Lapinus atricapilla_Lapinu à tête noire"
OLDNAME=""
NEWNAME=""
OLDNAME="" #OLDNAME="Mésange_charbonnière-78-2024-05-02-birdnet-RTSP_1-18:14:08.mp3"
NEWNAME="" #NEWNAME="Lapinus atricapilla_Lapinu à tête noire"
OLDNAME="$1"
NEWNAME="$2"
LABELS_FILE="$HOME/BirdNET-Pi/model/labels.txt"
DB_FILE="$HOME/BirdNET-Pi/scripts/birds.db"
DETECTIONS_TABLE="detections"
###################
# VALIDITY CHECKS #
###################
# Check if arguments are valid
if [[ "$1" != *".mp3" ]]; then
echo "The first argument should be a filename starting with the common name of the bird and finishing by mp3!"
exit 1
elif [[ "$2" != *"_"* ]]; then
echo "The second argument should be in the format : \"scientific name_common name\""
exit 1
fi
# Check if $NEWNAME is found in the file $LABELS_FILE
if ! grep -q "$NEWNAME" "$LABELS_FILE"; then
echo "Error: $NEWNAME not found in $LABELS_FILE"
exit 1
fi
##################
# EXECUTE SCRIPT #
##################
# Get the line where the column "File_Name" matches exactly $OLDNAME
IFS='|' read -r OLDNAME_sciname OLDNAME_comname OLDNAME_date < <(sqlite3 "$DB_FILE" "SELECT Sci_Name, Com_Name, Date FROM $DETECTIONS_TABLE WHERE File_Name = '$OLDNAME';")
@@ -36,6 +51,10 @@ fi
echo "This script will change the identification $OLDNAME from $OLDNAME_comname to $NEWNAME_comname"
########################
# EXECUTE : MOVE FILES #
########################
# Extract the part before the _ from $NEWNAME
NEWNAME_comname="${NEWNAME#*_}"
NEWNAME_sciname="${NEWNAME%%_*}"
@@ -63,6 +82,10 @@ else
echo "Error: File $FILE_PATH does not exist"
fi
###################################
# EXECUTE : UPDATE DATABASE FILES #
###################################
# Update the database
sqlite3 "$DB_FILE" "UPDATE $DETECTIONS_TABLE SET Sci_Name = '$NEWNAME_sciname', Com_Name = '$NEWNAME_comname', File_Name = '$NEWNAME_filename' WHERE File_Name = '$OLDNAME';"