static-pages/songs.sh
Naomi Carrigan 00dc40ba47
Some checks failed
Code Analysis / SonarQube (push) Failing after 50s
feat: add books too
2025-03-24 20:07:24 -07:00

39 lines
1.3 KiB
Bash
Executable File

IFS=$'\n'
# Initialize an empty string to hold the list of songs in JSON-like format
songs=""
filecount=$(find /home/naomi/music -type f | wc -l)
echo "Found $filecount files."
current=0
# Loop over each file found by find
for file in $(find /home/naomi/music -type f -print0 | tr '\0' '\n'); do
current=$((current + 1))
echo -ne "Processing $current/$filecount\r"
title=$(id3v2 -l "$file" | grep "TIT2" | cut -d ":" -f 2 | sed -e 's/^[[:space:]]*//')
artist=$(id3v2 -l "$file" | grep "TPE1" | cut -d ":" -f 2 | sed -e 's/^[[:space:]]*//')
if [ -z "$title" ]; then
title=$(id3v2 -l "$file" | grep "TT2" | cut -d ":" -f 2 | sed -e 's/^[[:space:]]*//')
fi
if [ -z "$artist" ]; then
artist=$(id3v2 -l "$file" | grep "TP1" | cut -d ":" -f 2 | sed -e 's/^[[:space:]]*//')
fi
if [ -z "$title" ]; then
# remove .mp3 from the title
title=$(basename "$file" | sed -e 's/\.mp3//g')
fi
if [ -z "$artist" ]; then
artist="Unknown Artist"
fi
# use jq to add the song to the list
songs="$songs$(jq -n --arg title "$title" --arg artist "$artist" '{title: $title, artist: $artist}'),"
done
# Remove trailing comma and add square brackets to complete the list
songs="[${songs%,}]"
# Write to ./music/songs.json
echo "$songs" > ./music/songs.json
echo -ne "Done!\r"