diff --git a/root/scripts/MetadataPostProcess.bash b/root/scripts/MetadataPostProcess.bash index 1bcaacc..8fa2409 100644 --- a/root/scripts/MetadataPostProcess.bash +++ b/root/scripts/MetadataPostProcess.bash @@ -1,7 +1,15 @@ #!/usr/bin/env bash -scriptVersion=1.0.010 -if [ -z "$lidarr_artist_path" ]; then - lidarr_artist_path="$1" +scriptVersion=1.0.011 +if [ -z "$lidarrUrl" ] || [ -z "$lidarrApiKey" ]; then + lidarrUrlBase="$(cat /config/config.xml | xq | jq -r .Config.UrlBase)" + if [ "$lidarrUrlBase" == "null" ]; then + lidarrUrlBase="" + else + lidarrUrlBase="/$(echo "$lidarrUrlBase" | sed "s/\///g")" + fi + lidarrApiKey="$(cat /config/config.xml | xq | jq -r .Config.ApiKey)" + lidarrPort="$(cat /config/config.xml | xq | jq -r .Config.Port)" + lidarrUrl="http://127.0.0.1:${lidarrPort}${lidarrUrlBase}" fi # auto-clean up log file to reduce space usage @@ -23,49 +31,63 @@ if [ "$lidarr_eventtype" == "Test" ]; then exit 0 fi -log "Processing :: $lidarr_trackfile_path" -albumFolder=$(dirname "$lidarr_trackfile_path") -if [ -d "$albumFolder" ]; then - if [ ! -f "$albumFolder/folder.jpg" ]; then - ffmpeg -i "$lidarr_trackfile_path" -an -vcodec copy "$albumFolder/folder.jpg" &> /dev/null - if [ -f "$albumFolder/folder.jpg" ]; then - log "Processing :: $albumFolder :: Album Artwork Extracted to: $albumFolder/folder.jpg" - chmod 666 "$albumFolder/folder.jpg" +getAlbumArtist="$(curl -s "$lidarrUrl/api/v1/album/$lidarr_album_id" -H "X-Api-Key: ${lidarrApiKey}" | jq -r .artist.artistName)" +getAlbumArtistPath="$(curl -s "$lidarrUrl/api/v1/album/$lidarr_album_id" -H "X-Api-Key: ${lidarrApiKey}" | jq -r .artist.path)" +getTrackPath="$(curl -s "$lidarrUrl/api/v1/trackFile?albumId=$lidarr_album_id" -H "X-Api-Key: ${lidarrApiKey}" | jq -r .[].path | head -n1)" +getFolderPath="$(dirname "$getTrackPath")" + +if echo "$getFolderPath" | grep "$getAlbumArtistPath" | read; then + if [ ! -d "$getFolderPath" ]; then + log "ERROR :: \"$getFolderPath\" Folder is missing :: Exiting..." + exit + fi +else + log "ERROR :: $getAlbumArtistPath not found within \"$getFolderPath\" :: Exiting..." + exit +fi + +find "$getFolderPath" -type f -regex ".*/.*\.\(flac\|opus\|m4a\|mp3\)" -print0 | while IFS= read -r -d '' file; do + fileName=$(basename -- "$file") + fileExt="${fileName##*.}" + + if [ ! -f "$getFolderPath/folder.jpg" ] && [ ! -f "$getFolderPath/folder.jpeg" ]; then + ffmpeg -i "$file" -an -vcodec copy "$getFolderPath/folder.jpg" &> /dev/null + if [ -f "$getFolderPath/folder.jpg" ] && [ -f "$getFolderPath/folder.jpeg" ]; then + log "Processing :: $getFolderPath :: Album Artwork Extracted to: $getFolderPath/folder.jpg" + chmod 666 "$getFolderPath/folder.jpg" fi fi -else - log "Processing :: ERROR :: $albumFolder :: folder missing..." -fi -lrcFile="${lidarr_trackfile_path%.*}.lrc" -if [ -f "$lrcFile" ]; then - rm "$lrcFile" -fi -fileName=$(basename -- "$lidarr_trackfile_path") -fileExt="${fileName##*.}" + if ls "$getFolderPath" | grep "lrc" | read; then + log "Removing existing lrc files" + find "$getFolderPath" -type f -iname "*.lrc" -delete + fi -if [ "$fileExt" == "flac" ]; then - getLyrics="$(ffprobe -loglevel 0 -print_format json -show_format -show_streams "$lidarr_trackfile_path" | jq -r ".format.tags.LYRICS" | sed "s/null//g" | sed "/^$/d")" - getArtistCredit="$(ffprobe -loglevel 0 -print_format json -show_format -show_streams "$lidarr_trackfile_path" | jq -r ".format.tags.ARTIST_CREDIT" | sed "s/null//g" | sed "/^$/d")" -fi + if [ "$fileExt" == "flac" ]; then + getLyrics="$(ffprobe -loglevel 0 -print_format json -show_format -show_streams "$file" | jq -r ".format.tags.LYRICS" | sed "s/null//g" | sed "/^$/d")" + getArtistCredit="$(ffprobe -loglevel 0 -print_format json -show_format -show_streams "$file" | jq -r ".format.tags.ARTIST_CREDIT" | sed "s/null//g" | sed "/^$/d")" + fi -if [ "$fileExt" == "opus" ]; then - getLyrics="$(ffprobe -loglevel 0 -print_format json -show_format -show_streams "$lidarr_trackfile_path" | jq -r ".streams[].tags.LYRICS" | sed "s/null//g" | sed "/^$/d")" -fi + if [ "$fileExt" == "opus" ]; then + getLyrics="$(ffprobe -loglevel 0 -print_format json -show_format -show_streams "$file" | jq -r ".streams[].tags.LYRICS" | sed "s/null//g" | sed "/^$/d")" + fi -if [ ! -z "$getLyrics" ]; then - log "Processing :: $lidarr_trackfile_path :: Extracting Lyrics..." - echo -n "$getLyrics" > "$lrcFile" - log "Processing :: $lidarr_trackfile_path :: Lyrics extracted to: $lrcFile" - chmod 666 "$lrcFile" -fi + if [ ! -z "$getLyrics" ]; then + lrcFile="${file%.*}.lrc" + log "Processing :: $file :: Extracting Lyrics..." + echo -n "$getLyrics" > "$lrcFile" + log "Processing :: $file :: Lyrics extracted to: $lrcFile" + chmod 666 "$lrcFile" + fi -if [ "$fileExt" == "flac" ]; then - if [ ! -z "$getArtistCredit" ]; then - log "Processing :: $lidarr_trackfile_path :: Setting ARTIST tag to match ARTIST_CREDIT tag..." - metaflac --remove-tag=ARTIST "$lidarr_trackfile_path" - metaflac --set-tag=ARTIST="$getArtistCredit" "$lidarr_trackfile_path" + if [ "$fileExt" == "flac" ]; then + if [ ! -z "$getArtistCredit" ]; then + log "Processing :: $file :: Setting ARTIST tag to match ARTIST_CREDIT tag..." + metaflac --remove-tag=ARTIST "$file" + metaflac --set-tag=ARTIST="$getArtistCredit" "$file" + fi fi -fi + +done exit