lidarr-dl/lidarr/UnmappedFilesCleaner.bash

61 lines
1.9 KiB
Bash
Raw Normal View History

#!/usr/bin/with-contenv bash
2023-07-20 12:52:24 +02:00
scriptVersion="1.3"
scriptName="UnmappedFilesCleaner"
2023-07-20 12:52:24 +02:00
#### Import Settings
source /config/extended.conf
#### Import Functions
source /config/extended/functions
2023-07-19 16:42:56 +02:00
verifyConfig () {
if [ "$enableUnmappedFilesCleaner" != "true" ]; then
log "Script is not enabled, enable by setting enableUnmappedFilesCleaner to \"true\" by modifying the \"/config/extended.conf\" config file..."
log "Sleeping (infinity)"
sleep infinity
fi
2023-07-16 19:32:42 +02:00
if [ -z "$unmappedFolderCleanerScriptInterval" ]; then
unmappedFolderCleanerScriptInterval="15m"
fi
}
2023-07-16 19:32:42 +02:00
UnmappedFilesCleanerProcess () {
log "Finding UnmappedFiles to purge..."
OLDIFS="$IFS"
IFS=$'\n'
unamppedFilesData="$(curl -s "$arrUrl/api/v1/trackFile?unmapped=true" -H 'Content-Type: application/json' -H "X-Api-Key: $arrApiKey" | jq -r .[])"
unamppedFileIds="$(curl -s "$arrUrl/api/v1/trackFile?unmapped=true" -H 'Content-Type: application/json' -H "X-Api-Key: $arrApiKey" | jq -r .[].id)"
if [ -z "$unamppedFileIds" ]; then
2023-07-16 19:32:42 +02:00
log "No unmapped files to process"
return
fi
for id in $(echo "$unamppedFileIds"); do
unmappedFilePath=$(echo "$unamppedFilesData" | jq -r ". | select(.id==$id)| .path")
unmappedFileName=$(basename "$unmappedFilePath")
unmappedFileDirectory=$(dirname "$unmappedFilePath")
if [ -d "$unmappedFileDirectory" ]; then
log "Deleting \"$unmappedFileDirectory\""
rm -rf "$unmappedFileDirectory"
fi
log "Removing $unmappedFileName ($id) entry from lidarr..."
lidarrCommand=$(curl -s "$arrUrl/api/v1/trackFile/$id" -X DELETE -H "X-Api-Key: $arrApiKey")
2023-07-16 19:32:42 +02:00
done
}
# Loop Script
for (( ; ; )); do
let i++
logfileSetup
log "Script starting..."
verifyConfig
getArrAppInfo
verifyApiAccess
UnmappedFilesCleanerProcess
log "Script sleeping for $unmappedFolderCleanerScriptInterval..."
sleep $unmappedFolderCleanerScriptInterval
done
2023-07-16 19:32:42 +02:00
exit